Skip to content

Commit 05f71d8

Browse files
committed
[Function add]
1.Add heapSort.
1 parent a131fa8 commit 05f71d8

File tree

3 files changed

+72
-4
lines changed

3 files changed

+72
-4
lines changed

Algorithm(4th_Edition)/algorithm_note.txt

+35-4
Original file line numberDiff line numberDiff line change
@@ -555,7 +555,7 @@
555555
Comparable v = a[lo];
556556
while(true){
557557
while(less(a[++i], v)) if(i == hi) break; //找到左边第一个小于该元素的数字。
558-
while(less(v, a[--j])) if(j == lo) break; //找到右边第一个大于钙元素的数字
558+
while(less(v, a[--j])) if(j == lo) break; //找到右边第一个大于该元素的数字
559559
if(i >= j) break; //退出外部while循环的条件
560560
swap(a, i, j);
561561
//show(a);
@@ -654,7 +654,7 @@
654654
pq = (T[]) new Comparable[maxN + 1]; //构建一个存储数量+1长度的数组。
655655
}
656656
public void insert(T t) {
657-
pq[++N] = t; //将最新的数据插入尾部,再听过上分将数据放在正确的位置
657+
pq[++N] = t; //将最新的数据插入尾部,再通过上浮将数据放在正确的位置
658658
swim(N);
659659
}
660660
public T max() {
@@ -680,13 +680,44 @@
680680
private void sink(int k){ //数据下沉,让上部的非有序数字下沉至正确位置。
681681
while(2 * k <= N){
682682
int j = 2 * k; //先将数字下沉一层
683-
if(j < N && less(j, j+1)) j++; //判断是否超过了树的总容量, 再比较a[j]和a[j++]的大小,要比大的那个小即可
683+
if(j < N && less(j, j+1)) j++; //判断是否超过了树的总容量, 再比较a[j]和a[j++]的大小,要比大的那个数即可
684684
if(!less(k, j)) break; //如果当前值比大的那个还要大,已经可以退出循环了。
685685
swap(k, j); //如果小于下一层的大值,则和该值进行交换。
686686
k = j;
687687
}
688688
}
689-
}
689+
}
690+
691+
->堆排序:
692+
实现:ca.mcmaster.chapter.two.queue.HeapSort
693+
public class HeapSort {
694+
private static Boolean less(Comparable[] a,int i, int j){ return a[i].compareTo(a[j]) < 0; }
695+
private static void swap(Comparable[] a, int i, int j){ Comparable temp = a[i]; a[i] = a[j]; a[j] = temp; }
696+
public static void sink(Comparable[] a, int k, int length){
697+
while(2 * k <= length){
698+
int j = 2 * k;
699+
if(j < length && less(a, j, j+1)) j++;
700+
if(!less(a, k, j)) break;
701+
swap(a, k, j);
702+
k = j;
703+
}
704+
}
705+
public static void heapSort(Comparable[] a){
706+
int length = a.length-1;
707+
for(int i = length/2; i >=0; i--) sink(a, i, length);
708+
while(length >= 1){
709+
swap(a, 0, length--);
710+
sink(a, 0, length);
711+
}
712+
}
713+
714+
public static void show(Comparable[] a){
715+
for(int i = 0; i < a.length; i++){
716+
System.out.print(a[i] + " ");
717+
}
718+
System.out.println();
719+
}
720+
}
690721

691722

692723

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package ca.mcmaster.chapter.two.queue;
2+
3+
public class HeapSort {
4+
private static Boolean less(Comparable[] a,int i, int j){ return a[i].compareTo(a[j]) < 0; }
5+
private static void swap(Comparable[] a, int i, int j){ Comparable temp = a[i]; a[i] = a[j]; a[j] = temp; }
6+
public static void sink(Comparable[] a, int k, int length){
7+
while(2 * k <= length){
8+
int j = 2 * k;
9+
if(j < length && less(a, j, j+1)) j++;
10+
if(!less(a, k, j)) break;
11+
swap(a, k, j);
12+
k = j;
13+
}
14+
}
15+
public static void heapSort(Comparable[] a){
16+
int length = a.length-1;
17+
for(int i = length/2; i >=0; i--) sink(a, i, length);
18+
while(length >= 1){
19+
swap(a, 0, length--);
20+
sink(a, 0, length);
21+
}
22+
}
23+
24+
public static void show(Comparable[] a){
25+
for(int i = 0; i < a.length; i++){
26+
System.out.print(a[i] + " ");
27+
}
28+
System.out.println();
29+
}
30+
31+
public static void main(String[] args) {
32+
Integer[] a = new Integer[]{18,27,33,55,6,3,23,2,3,5,2,45,1,4,2,5,7,3,7,456,96,7,23,8};
33+
heapSort(a);
34+
show(a);
35+
}
36+
}

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

+1
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ private void sink(int k){
3939
k = j;
4040
}
4141
}
42+
4243
public static void main(String[] args) {
4344
MaxPriorityQueueBinaryStack<Integer> stack = new MaxPriorityQueueBinaryStack<>(100);
4445
stack.insert(1);

0 commit comments

Comments
 (0)