-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHeapSort.java
More file actions
69 lines (58 loc) · 1.45 KB
/
HeapSort.java
File metadata and controls
69 lines (58 loc) · 1.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
//HeapSort takes O(N*logN) time complexity and O(1) space complexity in worst-case(In-place Sorting Algorithm).
public class HeapSort {
//Building a heap starting with largest value
public int[] buildMaxHeap(int[] a, int n) {
for(int k = n/2; k >= 1; k--) {
sink(a,k,n);
}
return a;
}
//Performing sink operation on heap
public int[] sink(int[] a, int k, int n) {
//root node = k;
//left node = 2*k;
//right node = 2*k+1;
while(2*k <= n) {
int j = 2*k;
//When the left child node is less than right child node
if(j<n && less(a,j,j+1)) {
j++;
}
//When the child nodes not greater than parent node
if(!less(a,k,j)) {
break;
}
//Exchanging the greater key(root node) and child node
exch(a,k,j);
k = j;
}
return a;
}
//Sorting the heap
public int[] sort(int[] a) {
int n = a.length-1;
buildMaxHeap(a,n);
while(n>1) {
sink(a,1,n);
exch(a,1,n--);
}
return a;
}
public boolean less(int[] a, int v, int w) {
return a[v]<a[w];
}
//Exchanging
public static void exch(int[] a, int i, int j) {
int temp = a[i];
a[i] = a[j];
a[j] = temp;
}
public static void main(String[] args) {
int[] arr = {1, 2, 3, 6, 9, 5, 10, 4, 11,13, 14, 15, 16, 17, 18, 19, 20};
HeapSort hs = new HeapSort();
int sorted[] = hs.sort(arr);
for(int i =1;i<arr.length;++i) {
System.out.print(sorted[i] + " ");
}
}
}