-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHeapsortExample.java
182 lines (158 loc) · 3.76 KB
/
HeapsortExample.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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
/**
* O(n lg n)
*
* A heap sort is an array visualized as a binary tree
* This algorithm will be in terms of a "Max Heap" which means
* given a root node, the two child nodes will have a value less than it's parent
*
* @author Ethan
*
*/
public class HeapsortExample
{
private List<Integer> A;
private ArrayList<Integer> sortedArray;
public HeapsortExample(List<Integer> arr)
{
this.A = arr;
this.sortedArray = new ArrayList<Integer>(arr.size());
}
/**
* O(n lg n)
* @return
*/
public ArrayList<Integer> heapSort()
{
buildMaxHeap();
for(int i = this.A.size() - 1; i > 0; i--)
{
exchange(0, i);
popOffHeapAppendToSortedArray(A.get(i));
maxHeapify(0);
}
maxHeapify(0);
popOffHeapAppendToSortedArray(A.get(0));
return sortedArray;
}
/**
* O(n)
*/
public void buildMaxHeap()
{
for(int i = (int) Math.floor(this.A.size()/2); i >= 0; i--)
{
maxHeapify(i);
}
return;
}
/**
* O(lg n)
* @param rootIndex
*/
private void maxHeapify(int rootIndex)
{
int leftNode;
int rightNode;
int largerValuesIndex = rootIndex;
try
{
leftNode = rootIndex*2 + 1;
rightNode = leftNode + 1;
if(indexExistsInArray(leftNode))
{
if(this.A.get(rootIndex) < this.A.get(leftNode))
{
largerValuesIndex = leftNode;
}
}
if(indexExistsInArray(rightNode))
{
if(this.A.get(rightNode) > this.A.get(largerValuesIndex))
{
largerValuesIndex = rightNode;
}
}
if(largerValuesIndex != rootIndex)
{
exchange(rootIndex, largerValuesIndex);
maxHeapify(largerValuesIndex);
}
}
catch(Exception e)
{
return;
}
return;
}
public static void main(String[] args)
{
//ArrayList<Integer> arr = {1, 2, 3, 4, 5, 6, 7};
// ===== CONTROL ===== //
int amountOfIterations = 1000000;
int maxArrayLength = 10000;
// ================= //
long sumOfTime = 0;
for(int i = 0; i < amountOfIterations; i++)
{
Random rg = new Random();
int r = rg.nextInt(maxArrayLength);
int[] a = randomArrayGenerator(r + 1); // add 1 so it's never 0
List<Integer> arr = new ArrayList<Integer>();
for (int index = 0; index < a.length; index++)
{
arr.add(a[index]);
}
// Construct
HeapsortExample hs = new HeapsortExample(arr);
long startTime = System.nanoTime();
ArrayList<Integer> result = hs.heapSort();
long endTime = System.nanoTime();
for(int k : result)
{
//System.out.print(k + " ");
}
sumOfTime += endTime - startTime;
// System.out.print("\n" + "It took " + (endTime - startTime) + " nanoseconds");
// System.out.print("\n");
}
long averageTime = sumOfTime/amountOfIterations;
System.out.println("AVERAGE TIME: " + averageTime);
}
// ============================ UTILITIES ======================== //
private void exchange(int e1, int e2)
{
int valueOfE1 = this.A.get(e1);
this.A.set(e1, this.A.get(e2));
this.A.set(e2, valueOfE1);
}
private boolean indexExistsInArray(int index)
{
return index >= 0 && index < this.A.size();
}
private void popOffHeapAppendToSortedArray(int val)
{
this.sortedArray.add(val);
this.A.remove(this.A.size() - 1);
}
public static int[] randomArrayGenerator(int length)
{
int[] a = new int[length];
for (int i = 1; i <= length; i++)
{
a[i-1] = i;
}
Random rg = new Random();
int tmp;
for (int i = length-1; i > 0; i--)
{
int r = rg.nextInt(i+1);
tmp = a[r];
a[r] = a[i];
a[i] = tmp;
}
return a;
}
}