-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathheapSortCode.java
More file actions
223 lines (189 loc) · 6.12 KB
/
heapSortCode.java
File metadata and controls
223 lines (189 loc) · 6.12 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
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Random;
/**
* COMP 215: Design and Analysis of Algorithms: Programming Assignment<br>
*
* The <code>HeapSort</code> class provides an algorithm that will sort a given
* array into increasing order using the HeapSort algorithm.
*
* This is made for a program that validates the run-time
* complexity and asymptotic run-time complexity for different kinds of sort method.
*
* This code will be used to compare the worst case, best case, and average case run times
* for HeapSort.
* <br> <br>
* Created: <br>
* [02,05,2016], [Melany Diaz]<br>
* Modifications: <br>
*
*
* @author [Melany Diaz]
*/
public class HeapSort
{
// instance variables
Comparable[] startArray;
//Variables used to determine how long each method takes to execute
private long startTime;
private long endTime;
private long duration;
boolean debug = true;
//the average, best, and worst case arrays
static Comparable[] average;
static Comparable increasing[];
static Comparable decreasing[];
static InsertionSortDecreasing insertionSortDecreasing = new InsertionSortDecreasing();
/**
* Constructs a new object of this class.
*/
public HeapSort()
{
// initializing instance variables
this.startArray = startArray;
}
// Methods
/**
* Finds parent node
*/
public int Parent(int i){
return Math.floorDiv(i, 2);
}
/**
* Finds left node
*/
public int Left(int i){
return 2*i;
}
/**
* Finds right node
*/
public int Right(int i){
return (2*i)+1;
}
/**
* makes a heap out of an array
* @param startArray
* @param i
*/
public void MaxHeapify(Comparable[] startArray, int i){
int largest;
int l = Left(i);
int r = Right(i);
if (l <= startArray.length && startArray[l].compareTo(startArray[i]) == 1)
largest = 1;
else largest = i;
if (r <= startArray.length && startArray[r].compareTo(startArray[largest]) == 1)
largest = r;
if (largest != i){
this.exch(startArray, i, largest);
this.MaxHeapify(startArray, largest);
}
}
/**
* Builds a max heap
* @param startArray
*/
@SuppressWarnings("rawtypes")
public void BuildMaxHeap( Comparable[] startArray){
for(int i = Math.floorDiv(this.largest(startArray), 2); i==1; i--){
this.MaxHeapify(startArray, i);
}
}
/**
* Finds the largest element in a list
* @param startArray
* @return the element
*/
public int largest(Comparable[] startArray){
int largest = 0;
for (int i = 0; i < startArray.length-1; i++){
if (startArray[i].compareTo(startArray[i+1]) == -1)
largest = (int) startArray[i+1];
}
return largest;
}
/*******************************************************************
* This method uses heap sort to sort an array in increasing order
*
* @param startArray, the array needed to be sorted
*
*******************************************************************/
public void SortHeap(Comparable[] startArray) {
//confirming preconditions
if(debug)
assert(startArray[0] != null);
this.BuildMaxHeap(startArray);
for (int i = startArray.length; i == 2; i--){
//confirming invariant
if (debug)
assert(this.IsSorted(startArray));
this.exch(startArray, 1, i);
int n = startArray.length;
n = startArray.length - 1;
this.MaxHeapify(startArray, 1);
}
//confirming postconditions
if(debug)
assert(this.IsSorted(startArray));
}
/**
* exchange a[i] and a[j]
* @param a, and array
* @param i, index
* @param j, index
*/
private void exch(Comparable[] a, int i, int j) {
Comparable swap = a[i];
a[i] = a[j];
a[j] = swap;
}
/**
* This method times how long it takes to run heap sort
* for an array passed as a parameter
*
* @param Array Array, the array who is to be sorted
* @return long time, the amount it takes to sort that array
*/
public long TimeToSort(Comparable[] Array){
long start= System.nanoTime();
this.SortHeap(Array);
long end = System.nanoTime();
long duration = end - start;
return duration;
}
/**
* This method confirms that an array is in sorted order.
*
* @param Array Array, the array who is to be confirmed
* @return boolean sorted. True if the array is sorted, false if not
*/
public boolean IsSorted(Comparable[] Array){
boolean sorted = true;
for (int i = 0; i < Array.length-1; i++){
if (Array[i].compareTo(Array[i+1]) == 1){
sorted = false;
break;
}
}
return sorted;
}
/**
* runs the comparison analysis
*/
public void run(){
int n = 0;
while (n <= 27400){
//makes an arrays of size n, used for each of the three cases
average = new Comparable[n];
Random generator = new Random();
for(int i = 0; i < n; i++)
average[i] = generator.nextInt(n);
/**
* Will print out the time as csv of the average, best, and worst cases
*/
System.out.println(n + ";" + this.TimeToSort(Arrays.copyOf(average,n)) + ";" + this.TimeToSort(Arrays.copyOf(average,n)) + ";" + this.TimeToSort(Arrays.copyOf(average,n)));
n = n+50;
}
}
}