forked from jainaman224/Algo_Ds_Notes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Heap_Sort.java
72 lines (62 loc) · 1.87 KB
/
Heap_Sort.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
65
66
67
68
69
70
71
72
class Heap_Sort
{
/* Max heapify the node
* Which is , interchanging largest children with the root of those children
* to build Max heaps, its assume that the level below the root to be max-heapified are
* already max heaps
*/
public static void MaxHeapify(int[] array, int root, int size)
{
int left = 2 * root + 1, largest;
int right = left + 1, temp;
if(left < size && array[left] > array[root])
largest = left;
else
largest = root;
if(right < size && array[right] > array[largest])
largest = right;
if(largest != root)
{
temp = array[root];
array[root] = array[largest];
array[largest] = temp;
MaxHeapify(array, largest, size);
}
}
// MaxHeapify that nodes having leaves or max heap nodes as children
public static void Build_Max_Heap(int[] array, int size)
{
/** as leaves are already max heaps , hence iteration starts from n/2 ... 0th index
* n/2 ... n are leaves
**/
for(int i = (size - 1) / 2; i >= 0; i--)
MaxHeapify(array, i, size);
}
public static void HeapSort(int array[], int size)
{
Build_Max_Heap(array, size);
int temp, i;
for(i = size - 1; i > 0; i--)
{
temp = array[0];
array[0] = array[i];
array[i] = temp;
MaxHeapify(array, 0, i);
}
}
// function ro print array
public static void Print_Array(int[] array, int size)
{
for(int i = 0; i < size; i++)
System.out.print(array[i] + " ");
System.out.println();
}
public static void main(String[] args)
{
int[] array = {2, 4, 3, 1, 6, 8, 4};
HeapSort(array, 7);
Print_Array(array, 7);
}
}
// Output
// 1 2 3 4 4 6 8