-
Notifications
You must be signed in to change notification settings - Fork 0
/
HeapSortAlg.java
110 lines (90 loc) · 2.63 KB
/
HeapSortAlg.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package heapsortalg;
import java.util.Random;
/**
*
* @author Keevah
*/
public class HeapSortAlg {
private int[] H;
static int r= 2;
private int arraySize;
static long startTime;
static long endTime;
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
int [] n = {2,10,100,1000,2000,3000,10000,100000};
int [] m= {2,10,100,1000,2000,10000,100000};
for(int i=0; i<n.length;i++){
HeapSortAlg array_inst = new HeapSortAlg(n[i]);
//System.out.println("n is: "+n[i]);
for(int j=0;j<m.length;j++){
startTime = System.nanoTime();
for (int x = 0; x < r; x++) {
array_inst.generateRandomArray(m[j]);
array_inst.heapsort();
array_inst.heapbuilding();
}
endTime = System.nanoTime();
System.out.println("AVG RUN TIME " + (endTime - startTime)/r);
}
}
}
public void heapsort() {
int n = H.length;
int i = n-1;
while(i>0){
swap(H, i, 0);
moveDown(H,0,i);
i--;
}
}
public void heapbuilding() {
int n = H.length;
int i = ((n-1)/2)-1;
while(i>=0){
moveDown(H,i,n);
i--;
}
}
private void moveDown(int[] H, int i, int n) {
int j = 2 * i + 1;
int l = i;
while (j < n) {
// right child exists and is larger than left child
if (j+1 < n && H[j] < H[j + 1]) {
j++;
}
// right child is larger than parent
if (H[j] > H[l]) {
swap(H, j, l);
// move down to largest child
l = j;
j = 2 * l + 1;
} else {
j=n;
}
}
}
private void swap(int[] H, int a, int b) {
int tmp = H[a];
H[a] = H[b];
H[b] = tmp;
}
HeapSortAlg(int size) {
arraySize = size;
H = new int[size];
}
public void generateRandomArray(int m) {
for (int i = 0; i < arraySize; i++) {
Random rand = new Random();
H[i] = rand.nextInt(m);
}
}
}