Skip to content

Commit bb24570

Browse files
committed
修改注释-修改格式及注释,添加打印输出
1 parent 1efbba8 commit bb24570

File tree

1 file changed

+13
-7
lines changed

1 file changed

+13
-7
lines changed

sort/src/main/java/com/mistray/merge/MergeSort.java

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package com.mistray.merge;
22

3+
import com.mistray.model.SortModel;
4+
35
import java.util.Arrays;
46

57
/**
@@ -15,12 +17,12 @@
1517
public class MergeSort {
1618

1719
public static void main(String[] args) {
18-
int[] array = {35, 24, 86, 12, 95, 58, 35, 42, 21, 73};
20+
Integer[] array = {35, 24, 86, 12, 95, 58, 35, 42, 21, 73};
1921
sort(array, 0, array.length - 1);
2022
System.out.println(Arrays.toString(array));
2123
}
2224

23-
public static void sort(int[] array, int start, int end) {
25+
public static void sort(Integer[] array, Integer start, Integer end) {
2426
if (start >= end) {
2527
return;
2628
}
@@ -33,26 +35,30 @@ public static void sort(int[] array, int start, int end) {
3335
}
3436

3537
// 将两个有序序列归并为一个有序序列(二路归并)
36-
private static void mergerSort(int[] array, int start, int mid, int end) {
37-
int[] arr = new int[end + 1]; // 定义一个临时数组,用来存储排序后的结果
38-
int low = start; // 临时数组的索引
38+
private static void mergerSort(Integer[] array, Integer start, Integer mid, Integer end) {
39+
// 定义一个临时数组,用来存储排序后的结果
40+
int[] arr = new int[end + 1];
41+
// 临时数组的索引
42+
int low = start;
3943
int left = start;
4044

4145
int center = mid + 1;
4246
// 取出最小值放入临时数组中
4347
while (start <= mid && center <= end) {
4448
arr[low++] = array[start] > array[center] ? array[center++] : array[start++];
4549
}
50+
SortModel.show(array);
4651

4752
// 若还有段序列不为空,则将其加入临时数组末尾
48-
4953
while (start <= mid) {
5054
arr[low++] = array[start++];
5155
}
56+
SortModel.show(array);
57+
5258
while (center <= end) {
5359
arr[low++] = array[center++];
5460
}
55-
61+
SortModel.show(array);
5662
// 将临时数组中的值copy到原数组中
5763
for (int i = left; i <= end; i++) {
5864
array[i] = arr[i];

0 commit comments

Comments
 (0)