-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHeapSort.java
More file actions
83 lines (70 loc) · 1.93 KB
/
HeapSort.java
File metadata and controls
83 lines (70 loc) · 1.93 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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
import java.util.Arrays;
/**
*
* @author Vinayak
*/
public class HeapSort {
public static int heapSize;
public static int leftChild(int i)
{
return i;
}
public static int rightChild(int i)
{
return i;
}
// Create the maxHeapify method
public static void maxHeapify(int A[],int i)
{
int lElement = -1;
int ltChild=leftChild(2*i+1);
int rtChild=rightChild(2*i+2);
if(ltChild<heapSize && A[ltChild]>A[i]){
lElement = ltChild;
}
else{
lElement=i;
}
if(rtChild<heapSize && A[rtChild]>A[lElement]){
lElement = rtChild;
}
if(lElement!=i){
int temp = A[i];
A[i]=A[lElement];
A[lElement]=temp;
maxHeapify(A, lElement);
}
}
// Implement the buildMaxHeap method and the heapSort method.
public static void buildMaxHeap(int A[])
{
heapSize = A.length;
for(int i=A.length/2; i>=0;i--)
{
maxHeapify(A, i);
}
}
public static void heapSort(int A[])
{
buildMaxHeap(A);
for(int i=A.length-1;i>=0;i--)
{
int temp = A[0];
A[0]=A[i];
A[i]=temp;
heapSize = heapSize-1;
maxHeapify(A,0);
}
}
public static void main(String[] args) {
int[] array = new int[]{10, 15, 8, 12, 5, 2, 20, 7, 18};
System.out.println("The original input array");
System.out.println(Arrays.toString(array));
buildMaxHeap(array);
System.out.println("\nThe array after buildMAxHeap method");
System.out.println(Arrays.toString(array));
heapSort(array);
System.out.println("\nThe final sorted array after performing the Heapsort algorithm");
System.out.println(Arrays.toString(array));
}
}