-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHeapSort.java
More file actions
59 lines (45 loc) · 1.64 KB
/
Copy pathHeapSort.java
File metadata and controls
59 lines (45 loc) · 1.64 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
import java.util.Arrays;
public class HeapSort {
public static void heapSort(int[] array) {
int n = array.length;
// Build heap (rearrange array)
for (int i = n / 2 - 1; i >= 0; i--)
heapify(array, n, i);
// One by one extract an element from heap
for (int i = n - 1; i >= 0; i--) {
// Move the current root to the end
int temp = array[0];
array[0] = array[i];
array[i] = temp;
// Call max heapify on the reduced heap
heapify(array, i, 0);
}
}
public static void heapify(int[] array, int n, int i) {
int largest = i; // Initialize largest as root
int left = 2 * i + 1; // left child
int right = 2 * i + 2; // right child
// If left child is larger than root
if (left < n && array[left] > array[largest])
largest = left;
// If right child is larger than largest so far
if (right < n && array[right] > array[largest])
largest = right;
// If largest is not root
if (largest != i) {
int swap = array[i];
array[i] = array[largest];
array[largest] = swap;
// Recursively heapify the affected sub-tree
heapify(array, n, largest);
}
}
public static void main(String[] args){
int[] arr = {3, 69, 45, 88, 24, 1, 5};
System.out.println("Original array: ");
System.out.println(Arrays.toString(arr));
heapSort(arr);
System.out.println("Sorted array: ");
System.out.print(Arrays.toString(arr));
}//end main
}