forked from WWaldo/Sorting-Algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHeapSort.java
84 lines (78 loc) · 1.82 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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
import java.io.IOException;
import java.util.ArrayList;
public class HeapSort extends Sorts{
public ArrayList<Integer> heapSort(ArrayList<Integer> unsortedList)
{
int count = unsortedList.size();
heapify(unsortedList, count);
int end = count-1;
while(end > 0)
{
swap(unsortedList, end, 0);
end = end - 1;
siftDown(unsortedList, 0, end);
}
return unsortedList;
}
public void heapify(ArrayList<Integer> unsortedList, int count)
{
int start = count/2 - 1;
while(start >= 0)
{
siftDown(unsortedList, start, count - 1);
start -= 1;
}
}
public void siftDown(ArrayList<Integer> unsortedList, int start, int end)
{
int root = start;
while(root*2+1 <= end)
{
int child = root*2+1;
int swap = root;
if(unsortedList.get(swap) < unsortedList.get(child))
{
swap = child;
}
if(child+1 <= end && unsortedList.get(swap) < unsortedList.get(child+1))
{
swap = child+1;
}
if(swap != root)
{
swap(unsortedList, root, swap);
root = swap;
}
else
{
return;
}
}
}
public void swap(ArrayList<Integer> unsortedList, int swapOne, int swapTwo)
{
int holder = unsortedList.get(swapOne);
unsortedList.set(swapOne, unsortedList.get(swapTwo));
unsortedList.set(swapTwo, holder);
}
public void HeapTime(IOClass ioStream) throws IOException
{
ioStream.readFromFile();
ArrayList<Integer> sortedList = new ArrayList<Integer>();
long timeBefore = System.nanoTime();
sortedList = heapSort(ioStream.getInputArray());
long timeAfter = System.nanoTime();
double rawTime = timeAfter - timeBefore;
double timeInMilli = rawTime/1000000;
if(isSorted(sortedList))
{
ioStream.setInputArray(sortedList);
System.out.print("HeapSort time (in Milli): ");
System.out.println(timeInMilli);
}
else
{
System.out.println("Not sorted!");
}
}
}