-
Notifications
You must be signed in to change notification settings - Fork 91
/
Heapsort.java
68 lines (59 loc) · 1.28 KB
/
Heapsort.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
import java.io.*;
public class HeapSort
{
private static int[] a;
private static int n;
private static int left;
private static int right;
private static int largest;
// Build-Heap Function
public static void buildheap(int []a){
n=a.length-1;
for(int i=n/2;i>=0;i--){
System.out.println(i);
maxheap(a,i);
}
}
// Max-Heap Function
public static void maxheap(int[] a, int i){
left=2*i;
right=2*i+1;
System.out.println(i + " " + left + " " + right);
if(left <= n && a[left] > a[i]){
largest=left;
}
else{
largest=i;
}
if(right <= n && a[right] > a[largest]){
largest=right;
}
if(largest!=i){
exchange(i,largest);
maxheap(a, largest);
}
}
// Exchange Function
public static void exchange(int i, int j){
int t=a[i];
a[i]=a[j];
a[j]=t;
}
// Sort Function
public static void sort(int []a0){
a=a0;
buildheap(a);
for(int i=n;i>0;i--){
exchange(0, i);
n=n-1;
maxheap(a, 0);
}
}
public static void main(String[] args) {
int []a1={3,5,1,2,4};
sort(a1);
for(int i=0;i<a1.length;i++){
System.out.print(a1[i] + " ");
}
}
}