Uh oh!
There was an error while loading. Please reload this page.
forked from TheAlgorithms/Java
- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHeapSort.java
More file actions
Latest commit
185 lines (172 loc) · 5.06 KB
/
Copy pathHeapSort.java
File metadata and controls
185 lines (172 loc) · 5.06 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
importjava.util.Scanner;
/**
* Heap Sort Algorithm. Implements MinHeap
*
*/
publicclassHeapSort {
/**
* array to store heap.
*/
privateint[] heap;
/**
* size of heap.
*/
privateintsize;
/**
* Constructor.
*
* @param heap
* array of unordered integers
*/
publicHeapSort(int[] heap) {
this.setHeap(heap);
this.setSize(heap.length);
}
/**
* Sets this.size with {@code length).
*
* @param length
* integer length of heap
*/
privatevoidsetSize(intlength) {
this.size = length;
}
/**
* Sets Heap with {@code heap}.
*
* @param heap
* array of unordered elements
*/
privatevoidsetHeap(int[] heap) {
this.heap = heap;
}
/**
* Swaps index of {@code first} with {@code second}.
*
* @param first
* index to swap {@code second} with
* @param second
* index to swap {@code first} with
*/
privatevoidswap(intfirst, intsecond) {
inttemp = this.heap[first];
this.heap[first] = this.heap[second];
this.heap[second] = temp;
}
/**
* Heapifies subtree from {@code top} as root to {@code last} as last child.
*
* @param rootIndex
* index of root
* @param lastChild
* index of last child
*/
privatevoidheapSubtree(introotIndex, intlastChild) {
intleftIndex = rootIndex * 2 + 1;
intrightIndex = rootIndex * 2 + 2;
introot = this.heap[rootIndex];
if (rightIndex <= lastChild) { // if has right and left children
intleft = this.heap[leftIndex];
intright = this.heap[rightIndex];
if (left < right && left < root) {
this.swap(leftIndex, rootIndex);
this.heapSubtree(leftIndex, lastChild);
} elseif (right < root) {
this.swap(rightIndex, rootIndex);
this.heapSubtree(rightIndex, lastChild);
}
} elseif (leftIndex <= lastChild) { // if no right child, but has left child
intleft = this.heap[leftIndex];
if (left < root) {
this.swap(leftIndex, rootIndex);
this.heapSubtree(leftIndex, lastChild);
}
}
}
/**
* Makes heap with {@code root} as root.
*
* @param root
* index of root of heap
*/
privatevoidmakeMinHeap(introot) {
intleftIndex = root * 2 + 1;
intrightIndex = root * 2 + 2;
booleanhasLeftChild = leftIndex < this.heap.length;
booleanhasRightChild = rightIndex < this.heap.length;
if (hasRightChild) { //if has left and right
this.makeMinHeap(leftIndex);
this.makeMinHeap(rightIndex);
this.heapSubtree(root, this.heap.length - 1);
} elseif (hasLeftChild) {
this.heapSubtree(root, this.heap.length - 1);
}
}
/**
* Gets the root of this.heap.
*
* @return root of this.heap
*/
privateintgetRoot() {
this.swap(0, this.size - 1);
this.size--;
this.heapSubtree(0, this.size - 1);
returnthis.heap[this.size]; // return old root
}
/**
* Sorts this.heap with heap sort; displays ordered elements to console.
*
* @return {@code sorted} array of sorted elements
*/
publicfinalint[] sort() {
this.makeMinHeap(0); // make min heap using index 0 as root.
int[] sorted = newint[this.size];
intindex = 0;
while (this.size > 0) {
intmin = this.getRoot();
sorted[index] = min;
index++;
}
returnsorted;
}
/**
* Gets input to sort.
*
* @return unsorted array of integers to sort
*/
publicstaticint[] getInput() {
finalintnumElements = 6;
int[] unsorted = newint[numElements];
Scannerinput = newScanner(System.in);
System.out.println("Enter any 6 Numbers for Unsorted Array : ");
for (inti = 0; i < numElements; i++) {
unsorted[i] = input.nextInt();
}
input.close();
returnunsorted;
}
/**
* Prints elements in heap.
*
* @param heap
* array representing heap
*/
publicstaticvoidprintData(int[] heap) {
System.out.println("Sorted Elements:");
for (inti = 0; i < heap.length; i++) {
System.out.print(" " + heap[i] + " ");
}
}
/**
* Main method.
*
* @param args
* the command line arguments
*/
publicstaticvoidmain(String[] args) {
int[] heap = getInput();
HeapSortdata = newHeapSort(heap);
int[] sorted = data.sort();
printData(sorted);
}
}