Skip to content

Commit e99aa92

Browse files
committed
update
1 parent 3b734ae commit e99aa92

File tree

7 files changed

+134
-11
lines changed

7 files changed

+134
-11
lines changed

Diff for: 3.排序算法/src/sort/exchange/BubbleSort.java

+1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ public static void main(String[] args) {
1515
int[] arr = {4, 12, 2, 8, 453, 1, 59, 33};
1616
for (int i = 0, length = arr.length; i < arr.length - 1; i++) {
1717
for (int j = 0, tempLength = length - 1 - i; j < tempLength; j++) {
18+
//如果当前数大于下一个数那么和下一个数交换位置
1819
if (arr[j] > arr[j + 1]) {
1920
int temp = arr[j];
2021
arr[j] = arr[j + 1];

Diff for: 3.排序算法/src/sort/exchange/CountSort.java

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package sort.exchange;
2+
3+
import java.util.Arrays;
4+
5+
/**
6+
* 类功能简述:计数排序
7+
* 类功能详述:
8+
*
9+
* @author fanxb
10+
* @date 2019/8/6 17:35
11+
*/
12+
public class CountSort {
13+
/**
14+
* Description:
15+
*
16+
* @param arr 待排序数组
17+
* @return void
18+
* @author fanxb
19+
* @date 2019/8/6 17:36
20+
*/
21+
public static void sort(Integer[] arr, Integer minValue, Integer maxValue) {
22+
int range = maxValue - minValue + 1;
23+
Integer[] numCount = new Integer[range];
24+
Arrays.fill(numCount, 0);
25+
for (Integer item : arr) {
26+
item = item - minValue;
27+
numCount[item]++;
28+
}
29+
int count = 0;
30+
for (int i = 0; i < range; i++) {
31+
if (numCount[i] == 0) {
32+
continue;
33+
}
34+
for (int j = 0; j < numCount[i]; j++) {
35+
arr[count] = minValue + i;
36+
count++;
37+
}
38+
}
39+
}
40+
41+
42+
public static void main(String[] args) {
43+
Integer[] arr = {1, 65, 32, 334, 12, 21, 65, 112, 444443};
44+
sort(arr, 1, 444443);
45+
System.out.println(Arrays.toString(arr));
46+
}
47+
}

Diff for: 3.排序算法/src/sort/exchange/InsertSort.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,14 @@ public class InsertSort {
1818

1919
public static void sort(Integer[] arr) {
2020
for (int i = 0, length = arr.length; i < length; i++) {
21-
//有序部分从后向前比较
21+
//有序部分从后向前比较,直到找到合适的位置
2222
int j = i, temp = arr[i];
2323
//如果arr[j-1]<=temp,说明arr[j]需为temp,否则将arr[j-1]向后移动一位
2424
for (; j > 0 && temp < arr[j - 1]; j--) {
2525
arr[j] = arr[j - 1];
2626
}
2727
arr[j] = temp;
28-
System.out.println("当前数组状态为:"+Arrays.toString(arr));
28+
System.out.println("当前数组状态为:" + Arrays.toString(arr));
2929
}
3030
}
3131

Diff for: 3.排序算法/src/sort/exchange/MergeSort.java

+24-7
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package sort.exchange;
22

33
import java.util.ArrayList;
4+
import java.util.Arrays;
45

56
/**
67
* 类功能简述:归并排序
@@ -13,13 +14,25 @@
1314
*/
1415
public class MergeSort {
1516

17+
/**
18+
* Description:
19+
*
20+
* @param arr 待排序数组
21+
* @param start 开始下标
22+
* @param end 结束下标
23+
* @author fanxb
24+
* @date 2019/8/6 9:29
25+
*/
1626
public static void mergeSort(Integer[] arr, int start, int end) {
1727
if (start >= end) {
1828
return;
1929
}
2030
int half = (start + end) / 2;
31+
//归并左边
2132
mergeSort(arr, start, half);
33+
//归并右边
2234
mergeSort(arr, half + 1, end);
35+
//合并
2336
merge(arr, start, half, end);
2437
}
2538

@@ -50,19 +63,23 @@ public static void merge(Integer[] arr, int start, int half, int end) {
5063
j++;
5164
}
5265
} else {
53-
//说明第二个数组卖完了,将第二个数组剩余部分放到tempList中
54-
while (j <= half) {
66+
//说明第二个数组已经完了,将第一个数组剩余部分放到tempList中
67+
while (i <= half) {
5568
//说明第二个数组处理完了
56-
tempList.add(arr[j]);
57-
j++;
69+
tempList.add(arr[i]);
70+
i++;
5871
}
5972
}
60-
73+
//最后将tempList复制到arr中
74+
for (int k = 0, length = tempList.size(); k < length; k++) {
75+
arr[start + k] = tempList.get(k);
76+
}
6177

6278
}
6379

6480
public static void main(String[] args) {
65-
Integer[] arr = {4, 3, 1, 2, 5, 4, 2, 54};
66-
mergeSort(arr, 0, arr.length);
81+
Integer[] arr = {4, 3, 1, 2, 5, 4, 2};
82+
mergeSort(arr, 0, arr.length - 1);
83+
System.out.println(Arrays.toString(arr));
6784
}
6885
}

Diff for: 3.排序算法/src/sort/exchange/RadixSort.java

+58
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package sort.exchange;
2+
3+
import java.util.Arrays;
4+
import java.util.LinkedList;
5+
6+
/**
7+
* 类功能简述:基数排序
8+
* 类功能详述:
9+
*
10+
* @author fanxb
11+
* @date 2019/8/6 14:33
12+
*/
13+
public class RadixSort {
14+
15+
@SuppressWarnings("unchecked")
16+
public static void sort(Integer[] arr) {
17+
//定义桶
18+
LinkedList<Integer>[] buckets = new LinkedList[10];
19+
for (int i = 0; i < 10; i++) {
20+
buckets[i] = new LinkedList<>();
21+
}
22+
int size = arr.length;
23+
//当前处理第几位的数
24+
int count = 0;
25+
while (true) {
26+
//是否继续进位
27+
boolean isContinue = false;
28+
//将数放到桶中
29+
for (int i = 0; i < size; i++) {
30+
int temp = arr[i] / (int) Math.pow(10, count) % 10;
31+
if (!isContinue && temp != 0) {
32+
// 如果存在一个数取的值不为0,说明还要继续循环。
33+
isContinue = true;
34+
}
35+
buckets[temp].addLast(arr[i]);
36+
}
37+
if (!isContinue) {
38+
return;
39+
}
40+
//从桶中取出放到arr中,注意以什么顺序放进去的就要以什么顺序取出来(先进先出)
41+
int index = 0;
42+
for (int i = 0; i < 10; i++) {
43+
Integer item;
44+
while ((item = buckets[i].pollFirst()) != null) {
45+
arr[index++] = item;
46+
}
47+
}
48+
//位数+1
49+
count++;
50+
}
51+
}
52+
53+
public static void main(String[] args) {
54+
Integer[] arr = {4, 31, 1, 29, 5, 4, 2};
55+
sort(arr);
56+
System.out.println(Arrays.toString(arr));
57+
}
58+
}

Diff for: 3.排序算法/src/sort/exchange/ShellSort.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ public static void sort(Integer[] arr) {
3434
}
3535
}
3636
n1 /= 2;
37-
} while (n1 > 0);
37+
} while (n1 >= 1);
3838
}
3939

4040
public static void main(String[] args) {

Diff for: 3.排序算法/src/sort/exchange/SimpleSelectSort.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ public static void sort(Integer[] arr) {
3030

3131
public static void main(String[] args) {
3232
Integer[] arr = {1, 65, 32, 12, 21};
33-
ShellSort.sort(arr);
33+
sort(arr);
3434
System.out.println(Arrays.toString(arr));
3535
}
3636
}

0 commit comments

Comments
 (0)