-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHeapSort.java
64 lines (50 loc) · 1.54 KB
/
HeapSort.java
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
//TC: O(nlogn)
//SC: O(1)
import java.util.*;
public class HeapSort {
public static void heapSort(int[] arr) {
int n = arr.length;
//construct a heap
for (int pos=n/2-1; pos>=0; pos--)
heapify(arr, n, pos);
//extarting element from heap, by swapping first with last
for (int i=n-1; i>=0; i--) {
int temp = arr[0];
arr[0] = arr[i];
arr[i] = temp;
//reduce the size of heap
heapify(arr, i, 0);
}
}
//constructing a max heap
public static void heapify(int[] arr, int n, int pos) {
int largest = pos;
int left = 2*pos + 1;
int right = 2*pos + 2;
//if left > root
if (left < n && arr[left] > arr[largest])
largest = left;
//if right > largest
if (right < n && arr[right] > arr[largest])
largest = right;
if (largest != pos) {
int temp = arr[pos];
arr[pos] = arr[largest];
arr[largest] = temp;
//heapify again
heapify(arr, n, largest);
}
}
public static void main(String[] args) {
int[] arr = {45, 12, 34, 12, 1};
System.out.println("Array before sorting:");
for (int x: arr)
System.out.print(x + " ");
System.out.println();
heapSort(arr);
System.out.println("Array after sorting:");
for (int x: arr)
System.out.print(x + " ");
System.out.println();
}
}