Skip to content

Commit 3acba48

Browse files
committed
[Function add]
1.QuickSort.
1 parent f231d9a commit 3acba48

File tree

2 files changed

+103
-12
lines changed

2 files changed

+103
-12
lines changed

Algorithm(4th_Edition)/algorithm_note.txt

+69-10
Original file line numberDiff line numberDiff line change
@@ -497,23 +497,23 @@
497497
}
498498
}
499499

500-
6. 并归排序MergeSort:
500+
6. 并归排序MergeSort:(NlgN)
501501
并归实现:ca.mcmaster.chapter.two.Sort.Sort#merge(Comparable[], int, int, int)
502502
并归排序的实现是基于当前每个子列均是顺序排列。
503503
public static void merge(Comparable[] a, int lo, int mid, int hi){
504-
int i = lo, j = mid + 1; //将a[lo-mid] 和a[mid+1-hi]并归
505-
Comparable[] aux = new Comparable[a.length];
506-
for (int k = lo; k < aux.length; k++) //复制一份原列表的值
504+
int i = lo, j = mid + 1;
505+
if(less(a[mid], a[mid+1])) return; //如果前一半的最后一个元素已经小于后一半的第一个元素,说明需要归并的两半已经是增序的。
506+
for (int k = lo; k < aux.length; k++)
507507
aux[k] = a[k];
508-
for(int k = lo; k < a.length; k++){
509-
if(i > mid) a[k] = aux[j++]; //若果两个列中的那哪一个已经出完了,就完全使用另一个列
510-
else if(j > hi) a[k] = aux[i++];
511-
else if(less(aux[i], aux[j])) a[k] = aux[i++]; //未出完的情况下比较大小,出小的。
512-
else a[k] = aux[j++];
508+
for(int k = lo; k < a.length; k++){
509+
if(i > mid) a[k] = aux[j++];
510+
else if(j > hi) a[k] = aux[i++];
511+
else if(less(aux[i], aux[j])) a[k] = aux[i++];
512+
else a[k] = aux[j++];
513513
}
514514
}
515515

516-
并归排序实现:ca.mcmaster.chapter.two.Sort.Sort#mergeSort(Comparable[])
516+
自顶向下并归排序实现:ca.mcmaster.chapter.two.Sort.Sort#mergeSort(Comparable[])
517517
并归排序利用了分治的思想。将大的数组拆成两个数进行归并,在对2-2进行归并,对4-4进行归并直到所有的数均被涵盖。
518518
public static void mergeSort(Comparable[] a){
519519
aux = new Comparable[a.length]; //因为不可避免的要进行数组的复制,所以在最外层进行复制以节约空间。
@@ -527,6 +527,65 @@
527527
mergeSortIn(a, mid + 1, hi);
528528
merge(a, lo, mid, hi);
529529
}
530+
531+
自底向上并归排序实现:ca.mcmaster.chapter.two.Sort.Sort#mergeSortBU(Comparable[])
532+
public static void mergeSortBU(Comparable[] a){
533+
int length = a.length;
534+
aux = new Comparable[length];
535+
for(int sz = 1; sz < length; sz *= 2){
536+
for(int lo = 0; lo < length - sz; lo += sz * 2)
537+
merge(a, lo, lo+sz-1, Math.min(lo+sz+sz-1, length-1)); //要做一个取小的操作,因为数组的总量不一定是偶数。
538+
}
539+
}
540+
541+
7. 快速排序QuickSort:
542+
实现:ca.mcmaster.chapter.two.Sort.Sort#quickSort(Comparable[], int, int)
543+
1.随意取出数组中的一个元素(一般都取当前需要sort的第一个元素)。
544+
如果取的是第一个元素:
545+
2.我们从第二个元素开始向右进行遍历,再从右向左进行遍历,如果左边的数字大于该数字,右边的数字小于该数字,则两个数字换位置。
546+
3.再针对左边的数字进行排序,再针对右边的数组进行排序。
547+
public static void quickSort(Comparable[] a, int lo, int hi){
548+
if(lo >= hi) return;
549+
int j = partition(a, lo, hi);
550+
quickSort(a, lo, j-1);
551+
quickSort(a, j+1, hi);
552+
}
553+
public static int partition(Comparable[] a, int lo, int hi){
554+
int i = lo, j = hi + 1;
555+
Comparable v = a[lo];
556+
while(true){
557+
while(less(a[++i], v)) if(i == hi) break; //找到左边第一个小于该元素的数字。
558+
while(less(v, a[--j])) if(j == lo) break; //找到右边第一个大于钙元素的数字。
559+
if(i >= j) break; //退出外部while循环的条件
560+
swap(a, i, j);
561+
//show(a);
562+
}
563+
swap(a, lo, j); //将lo插入左右两个数组之间,使左边的均不大于lo,右边的均不小于hi。
564+
return j;
565+
}
566+
567+
结果:
568+
原数组:18,27,33,55,6,3,23,2,3,5,2,45,1,4,2,5,7,3,7,432,96,7,23,8
569+
每次结果以及最终结果:
570+
18 8 33 55 6 3 23 2 3 5 2 45 1 4 2 5 7 3 7 432 96 7 23 27
571+
18 8 7 55 6 3 23 2 3 5 2 45 1 4 2 5 7 3 7 432 96 33 23 27
572+
18 8 7 7 6 3 23 2 3 5 2 45 1 4 2 5 7 3 55 432 96 33 23 27
573+
18 8 7 7 6 3 3 2 3 5 2 45 1 4 2 5 7 23 55 432 96 33 23 27
574+
18 8 7 7 6 3 3 2 3 5 2 7 1 4 2 5 45 23 55 432 96 33 23 27
575+
5 2 7 7 6 3 3 2 3 5 2 7 1 4 8 18 45 23 55 432 96 33 23 27
576+
5 2 4 7 6 3 3 2 3 5 2 7 1 7 8 18 45 23 55 432 96 33 23 27
577+
5 2 4 1 6 3 3 2 3 5 2 7 7 7 8 18 45 23 55 432 96 33 23 27
578+
5 2 4 1 2 3 3 2 3 5 6 7 7 7 8 18 45 23 55 432 96 33 23 27
579+
3 2 2 1 2 3 3 4 5 5 6 7 7 7 8 18 45 23 55 432 96 33 23 27
580+
3 2 2 1 2 3 3 4 5 5 6 7 7 7 8 18 45 23 55 432 96 33 23 27
581+
2 1 2 2 3 3 3 4 5 5 6 7 7 7 8 18 45 23 55 432 96 33 23 27
582+
1 2 2 2 3 3 3 4 5 5 6 7 7 7 8 18 45 23 55 432 96 33 23 27
583+
1 2 2 2 3 3 3 4 5 5 6 7 7 7 8 18 45 23 27 432 96 33 23 55
584+
1 2 2 2 3 3 3 4 5 5 6 7 7 7 8 18 45 23 27 23 96 33 432 55
585+
1 2 2 2 3 3 3 4 5 5 6 7 7 7 8 18 45 23 27 23 33 96 432 55
586+
1 2 2 2 3 3 3 4 5 5 6 7 7 7 8 18 23 23 27 33 45 96 55 432
587+
1 2 2 2 3 3 3 4 5 5 6 7 7 7 8 18 23 23 27 33 45 55 96 432
588+
530589

531590

532591

Algorithm(4th_Edition)/src/ca/mcmaster/chapter/two/Sort/Sort.java

+34-2
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,15 @@ public static void mergeSortIn(Comparable[] a, int lo, int hi){
4747
merge(a, lo, mid, hi);
4848
}
4949

50+
public static void mergeSortBU(Comparable[] a){
51+
int length = a.length;
52+
aux = new Comparable[length];
53+
for(int sz = 1; sz < length; sz *= 2){
54+
for(int lo = 0; lo < length - sz; lo += sz * 2)
55+
merge(a, lo, lo+sz-1, Math.min(lo+sz+sz-1, length-1));
56+
}
57+
}
58+
5059
public static Boolean less(Comparable a, Comparable b){
5160
return a.compareTo(b) < 0;
5261
}
@@ -68,6 +77,7 @@ public static Boolean isSorted(Comparable[] a){
6877
}
6978
public static void merge(Comparable[] a, int lo, int mid, int hi){
7079
int i = lo, j = mid + 1;
80+
if(less(a[mid], a[mid+1])) return;
7181
for (int k = lo; k < aux.length; k++)
7282
aux[k] = a[k];
7383
for(int k = lo; k < a.length; k++){
@@ -78,14 +88,36 @@ public static void merge(Comparable[] a, int lo, int mid, int hi){
7888
}
7989
}
8090

91+
public static int partition(Comparable[] a, int lo, int hi){
92+
int i = lo, j = hi + 1;
93+
Comparable v = a[lo];
94+
while(true){
95+
while(less(a[++i], v)) if(i == hi) break;
96+
while(less(v, a[--j])) if(j == lo) break;
97+
if(i >= j) break;
98+
swap(a, i, j);
99+
}
100+
swap(a, lo, j);
101+
return j;
102+
}
103+
104+
public static void quickSort(Comparable[] a, int lo, int hi){
105+
if(lo >= hi) return;
106+
int j = partition(a, lo, hi);
107+
quickSort(a, lo, j-1);
108+
quickSort(a, j+1, hi);
109+
}
110+
81111
public static void main(String[] args) {
82-
Integer[] a = new Integer[]{2,3,5,2,45,1,4,2,5,7,3,7,432,96,7,23,8};
112+
// Integer[] a = new Integer[]{18,27,33,55,6,3,23,2,3,5,2,45,1,4,2,5,7,3,7,432,96,7,23,8};
83113
// Integer[] a = new Integer[]{1,3,5,9,2,5,6,7};
114+
Integer[] a = new Integer[]{2,1,3,4,5,6,7,8,9};
84115
// selectionSort(a);
85116
// insertSort(a);
86117
// ShellSort(a);
87118
// merge(a, 0, 3, 7);
88-
mergeSort(a);
119+
// mergeSortBU(a);
120+
quickSort(a, 0, a.length -1);
89121
show(a);
90122
}
91123
}

0 commit comments

Comments
 (0)