-
Notifications
You must be signed in to change notification settings - Fork 82
Expand file tree
/
Copy pathHeapSort.java
More file actions
59 lines (49 loc) · 1.21 KB
/
HeapSort.java
File metadata and controls
59 lines (49 loc) · 1.21 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
package sorting;
import java.util.Random;
public class HeapSort {
public void heapSort(int[] arr) {
int N = arr.length-1;
heapify(arr);
while(N>0) {
swap(arr, 0, N--);
sink(arr, 0, N);
}
}
// swap elements such that new array is a max-heap
private void heapify(int[] arr) {
for(int k=(arr.length>>1)-1; k>=0; k--) {
sink(arr, k, arr.length-1);
}
}
// swap parent with either of its two children if needed
private void sink(int[] arr, int index, int N) {
while((index<<1)+1 <= N) {
int j = (index<<1)+1;
if(j<N && arr[j]<arr[j+1]) j++;
if(arr[index]>=arr[j]) break;
swap(arr, index, j);
index = j;
}
}
private void swap(int[] arr, int index1, int index2) {
int temp = arr[index1];
arr[index1] = arr[index2];
arr[index2] = temp;
}
public static void main(String[] args) {
HeapSort heap = new HeapSort();
int N = 10;
int[] arr = new int[N];
Random rm = new Random();
System.out.println("Original array: ");
for(int i=0; i<arr.length; i++) {
arr[i] = rm.nextInt(100);
System.out.print(arr[i] + " ");
}
heap.heapSort(arr);
System.out.println("\n\nSorted array: ");
for(int n : arr)
System.out.print(n + " ");
System.out.println();
}
}