Skip to content

Commit f799999

Browse files
author
xuxingyuan
committed
sort/search
1 parent d33ed1a commit f799999

File tree

2 files changed

+105
-8
lines changed

2 files changed

+105
-8
lines changed

basic/sorting/MergeSort/MergeSort.java

+36-2
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,12 @@ public class MergeSort {
55
private static void merge(int[] nums, int low, int mid, int high, int[] temp) {
66
int i = low, j = mid + 1, k = low;
77
while (k <= high) {
8+
//处理边界值
89
if (i > mid) {
910
temp[k++] = nums[j++];
1011
} else if (j > high) {
1112
temp[k++] = nums[i++];
13+
//比较low->mid;mid+1->high这两个有序数组并合并
1214
} else if (nums[i] <= nums[j]) {
1315
temp[k++] = nums[i++];
1416
} else {
@@ -35,8 +37,40 @@ private static void mergeSort(int[] nums) {
3537
}
3638

3739
public static void main(String[] args) {
38-
int[] nums = {1, 2, 7, 4, 5, 3};
39-
mergeSort(nums);
40+
int[] nums = {1, 2, 7, 4, 5, 3,3,5,7,9,0};
41+
//mergeSort(nums);
42+
myMergeSort(nums);
4043
System.out.println(Arrays.toString(nums));
4144
}
45+
public static void myMergeSort(int[] nums) {
46+
if (nums == null || nums.length == 1) {
47+
return;
48+
}
49+
myMergeSort(nums, 0, nums.length-1, new int[nums.length]);
50+
}
51+
private static void myMergeSort(int[] nums, int low, int high, int[] temp) {
52+
if (low >= high) {
53+
return;
54+
}
55+
int mid = low + ((high - low) >> 1);
56+
myMergeSort(nums, low, mid, temp);
57+
myMergeSort(nums, mid + 1, high, temp);
58+
myMerge(nums, low, mid, high, temp);
59+
}
60+
private static void myMerge(int[] nums, int low, int mid, int high, int[] temp) {
61+
//k:low ~ high;i:low -mid;j:mid+1-high
62+
int i = low, j = mid + 1, k = low;
63+
while (k <= high) {
64+
if (i > mid) {
65+
temp[k++] = nums[j++];
66+
} else if(j > high) {
67+
temp[k++] = nums[i++];
68+
} else if (nums[i] <= nums[j]) {
69+
temp[k++] = nums[i++];
70+
} else {
71+
temp[k++] = nums[j++];
72+
}
73+
}
74+
System.arraycopy(temp, low, nums, low, high - low + 1);
75+
}
4276
}

basic/sorting/QuickSort/QuickSort.java

+69-6
Original file line numberDiff line numberDiff line change
@@ -10,35 +10,98 @@ private static void quickSort(int[] nums, int low, int high) {
1010
if (low >= high) {
1111
return;
1212
}
13+
//取得中轴元素所在的中轴区间
1314
int[] p = partition(nums, low, high);
1415
quickSort(nums, low, p[0] - 1);
15-
quickSort(nums, p[0] + 1, high);
16+
quickSort(nums, p[1] + 1, high);
1617
}
1718

1819
private static int[] partition(int[] nums, int low, int high) {
19-
int less = low - 1, more = high;
20+
int less = low - 1, more = high, pivotal = nums[high];
2021
while (low < more) {
21-
if (nums[low] < nums[high]) {
22+
if (nums[low] < pivotal) {
2223
swap(nums, ++less, low++);
23-
} else if (nums[low] > nums[high]) {
24+
} else if (nums[low] > pivotal) {
2425
swap(nums, --more, low);
2526
} else {
2627
++low;
2728
}
2829
}
2930
swap(nums, more, high);
31+
//返回中轴元素所对应的区间
3032
return new int[] {less + 1, more};
3133
}
3234

3335
private static void swap(int[] nums, int i, int j) {
36+
if (i == j) {
37+
return;
38+
}
3439
int t = nums[i];
3540
nums[i] = nums[j];
3641
nums[j] = t;
3742
}
3843

3944
public static void main(String[] args) {
40-
int[] nums = {1, 2, 7, 4, 5, 3};
41-
quickSort(nums);
45+
int[] nums = {1, 2, 7, 4, 5, 3,3,4,5,6,7,3,0};
46+
//quickSort(nums);
47+
myQuickSort(nums);
48+
System.out.println(Arrays.toString(nums));
49+
}
50+
public static void myQuickSort(int[] nums) {
51+
if (nums == null || nums.length == 1) {
52+
return;
53+
}
54+
myQuickSort(nums, 0,nums.length-1);
55+
56+
}
57+
private static void myQuickSort(int[] nums, int low, int high) {
58+
if (low >= high) {
59+
return;
60+
}
61+
int[] partition = myPartition(nums, low, high);
62+
System.out.println("low:"+low+",high:"+high+"本次分区后的数组为");
4263
System.out.println(Arrays.toString(nums));
64+
myQuickSort(nums, low, partition[0]-1);
65+
myQuickSort(nums, partition[1]+1, high);
66+
}
67+
private static int[] myPartition(int[] nums, int low, int high) {
68+
int k=low + (high-low)/2;
69+
int less = low, more = high, pivotal = nums[k];
70+
while (low <= more) {
71+
if (nums[low] < pivotal) {
72+
swap(nums, less++, low++);
73+
} else if (nums[low] > pivotal) {
74+
swap(nums, more--, low);
75+
} else {
76+
low++;
77+
}
78+
}
79+
System.out.println("low:"+low+",high:"+high+"本次分区后的数组为"+"less:"+less+",more:"+more);
80+
if(low <=more){
81+
swap(nums, more, k);
82+
}
83+
return new int[]{less, more};
4384
}
85+
//low:6,high:12本次分区后的数组为less:3,more:5
86+
//low:0,high:12本次分区后的数组为
87+
//[1, 2, 0, 3, 3, 3, 4, 5, 6, 7, 5, 4, 7]
88+
//low:3,high:2本次分区后的数组为less:2,more:2
89+
//low:0,high:2本次分区后的数组为
90+
//[1, 0, 2, 3, 3, 3, 4, 5, 6, 7, 5, 4, 7]
91+
//low:2,high:1本次分区后的数组为less:1,more:1
92+
//low:0,high:1本次分区后的数组为
93+
//[0, 1, 2, 3, 3, 3, 4, 5, 6, 7, 5, 4, 7]
94+
//low:13,high:12本次分区后的数组为less:11,more:12
95+
//low:6,high:12本次分区后的数组为
96+
//[0, 1, 2, 3, 3, 3, 4, 5, 6, 5, 4, 7, 7]
97+
//low:11,high:10本次分区后的数组为less:10,more:10
98+
//low:6,high:10本次分区后的数组为
99+
//[0, 1, 2, 3, 3, 3, 4, 5, 5, 4, 6, 7, 7]
100+
//low:10,high:9本次分区后的数组为less:8,more:9
101+
//low:6,high:9本次分区后的数组为
102+
//[0, 1, 2, 3, 3, 3, 4, 4, 5, 5, 6, 7, 7]
103+
//low:8,high:7本次分区后的数组为less:6,more:7
104+
//low:6,high:7本次分区后的数组为
105+
//[0, 1, 2, 3, 3, 3, 4, 4, 5, 5, 6, 7, 7]
106+
//[0, 1, 2, 3, 3, 3, 4, 4, 5, 5, 6, 7, 7]
44107
}

0 commit comments

Comments
 (0)