-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathheapsort.java
More file actions
57 lines (36 loc) · 1.09 KB
/
Copy pathheapsort.java
File metadata and controls
57 lines (36 loc) · 1.09 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
import java.util.Arrays;
public class HeapSort {
public void sort(int arrA[]) {
int size = arrA.length;
for (int i = size / 2 – 1; i >= 0; i–)
heapify(arrA, size, i);
for (int i=size–1; i>=0; i–) {
int x = arrA[0];
arrA[0] = arrA[i];
arrA[i] = x;
heapify(arrA, i, 0);
}
}
void heapify(int arrA[], int heapSize, int i) {
int largest = i; // Initialize largest as root
int leftChildIdx = 2*i + 1; // left = 2*i + 1
int rightChildIdx = 2*i + 2; // right = 2*i + 2
if (leftChildIdx < heapSize && arrA[leftChildIdx ] > arrA[largest])
largest = leftChildIdx ;
if (rightChildIdx < heapSize && arrA[rightChildIdx ] > arrA[largest])
largest = rightChildIdx ;
if (largest != i) {
int swap = arrA[i];
arrA[i] = arrA[largest];
arrA[largest] = swap;
heapify(arrA, heapSize, largest);
}
}
public static void main(String args[]) {
int arrA[] = {9, 10, 5, 3, 1, 2, 6};
System.out.println("Original array is: " + Arrays.toString(arrA));
HeapSort heapSort = new HeapSort();
heapSort.sort(arrA);
System.out.println("Sorted array is (Heap Sort): " + Arrays.toString(arrA));
}
}