-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathHeap_tree.java
More file actions
47 lines (37 loc) · 1.01 KB
/
Heap_tree.java
File metadata and controls
47 lines (37 loc) · 1.01 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
public class Heap_tree {
public static void heapify(int a[], int n , int i){
int larg = i;
int left = 2*i+1;
int right = 2*i+2;
if(left<n && a[left]>a[larg]){
larg = left;
}
if(right<n && a[right]>a[larg]){
larg = right;
}
if(larg!=i){
int temp = a[i];
a[i] =a[larg];
a[larg] = temp;
}
}
public static void heapSort(int a[],int n){
for(int i=n/2 -1;i>=0;i--){
heapify(a,n,i);
}
for(int i=n-1;i>=0;i--){
int temp = a[0];
a[0] = a[i];
a[i] = temp;
heapify(a,i,0);
}
}
public static void main(String[] args){
int a[] = {81,89,9,11,14,76,54,22};
int n =a.length;
heapSort(a,n);
for(int i=0;i<n;i++){
System.out.println("Sorted array is"+a[i]);
}
}
}