Skip to content

Commit a111922

Browse files
Merge pull request #1 from ShyamW/master
Implemented HeapSort
2 parents 73bb72b + 31c1c94 commit a111922

File tree

1 file changed

+185
-0
lines changed

1 file changed

+185
-0
lines changed

HeapSort.java

Lines changed: 185 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,185 @@
1+
import java.util.Scanner;
2+
3+
/**
4+
* Heap Sort Algorithm. Implements MinHeap
5+
*
6+
*/
7+
public class HeapSort {
8+
/**
9+
* array to store heap.
10+
*/
11+
private int[] heap;
12+
/**
13+
* size of heap.
14+
*/
15+
private int size;
16+
17+
/**
18+
* Constructor.
19+
*
20+
* @param heap
21+
* array of unordered integers
22+
*/
23+
public HeapSort(int[] heap) {
24+
this.setHeap(heap);
25+
this.setSize(heap.length);
26+
}
27+
28+
/**
29+
* Sets this.size with {@code length).
30+
*
31+
* @param length
32+
* integer length of heap
33+
*/
34+
private void setSize(int length) {
35+
this.size = length;
36+
}
37+
38+
/**
39+
* Sets Heap with {@code heap}.
40+
*
41+
* @param heap
42+
* array of unordered elements
43+
*/
44+
private void setHeap(int[] heap) {
45+
this.heap = heap;
46+
}
47+
48+
/**
49+
* Swaps index of {@code first} with {@code second}.
50+
*
51+
* @param first
52+
* index to swap {@code second} with
53+
* @param second
54+
* index to swap {@code first} with
55+
*/
56+
private void swap(int first, int second) {
57+
int temp = this.heap[first];
58+
this.heap[first] = this.heap[second];
59+
this.heap[second] = temp;
60+
}
61+
62+
/**
63+
* Heapifies subtree from {@code top} as root to {@code last} as last child.
64+
*
65+
* @param rootIndex
66+
* index of root
67+
* @param lastChild
68+
* index of last child
69+
*/
70+
private void heapSubtree(int rootIndex, int lastChild) {
71+
int leftIndex = rootIndex * 2 + 1;
72+
int rightIndex = rootIndex * 2 + 2;
73+
int root = this.heap[rootIndex];
74+
if (rightIndex <= lastChild) { // if has right and left children
75+
int left = this.heap[leftIndex];
76+
int right = this.heap[rightIndex];
77+
if (left < right && left < root) {
78+
this.swap(leftIndex, rootIndex);
79+
this.heapSubtree(leftIndex, lastChild);
80+
} else if (right < root) {
81+
this.swap(rightIndex, rootIndex);
82+
this.heapSubtree(rightIndex, lastChild);
83+
}
84+
} else if (leftIndex <= lastChild) { // if no right child, but has left child
85+
int left = this.heap[leftIndex];
86+
if (left < root) {
87+
this.swap(leftIndex, rootIndex);
88+
this.heapSubtree(leftIndex, lastChild);
89+
}
90+
}
91+
}
92+
93+
/**
94+
* Makes heap with {@code root} as root.
95+
*
96+
* @param root
97+
* index of root of heap
98+
*/
99+
private void makeMinHeap(int root) {
100+
int leftIndex = root * 2 + 1;
101+
int rightIndex = root * 2 + 2;
102+
boolean hasLeftChild = leftIndex < this.heap.length;
103+
boolean hasRightChild = rightIndex < this.heap.length;
104+
if (hasRightChild) { //if has left and right
105+
this.makeMinHeap(leftIndex);
106+
this.makeMinHeap(rightIndex);
107+
this.heapSubtree(root, this.heap.length - 1);
108+
} else if (hasLeftChild) {
109+
this.heapSubtree(root, this.heap.length - 1);
110+
}
111+
}
112+
113+
/**
114+
* Gets the root of this.heap.
115+
*
116+
* @return root of this.heap
117+
*/
118+
private int getRoot() {
119+
this.swap(0, this.size - 1);
120+
this.size--;
121+
this.heapSubtree(0, this.size - 1);
122+
return this.heap[this.size]; // return old root
123+
}
124+
125+
/**
126+
* Sorts this.heap with heap sort; displays ordered elements to console.
127+
*
128+
* @return {@code sorted} array of sorted elements
129+
*/
130+
public final int[] sort() {
131+
this.makeMinHeap(0); // make min heap using index 0 as root.
132+
int[] sorted = new int[this.size];
133+
int index = 0;
134+
while (this.size > 0) {
135+
int min = this.getRoot();
136+
sorted[index] = min;
137+
index++;
138+
}
139+
return sorted;
140+
}
141+
142+
/**
143+
* Gets input to sort.
144+
*
145+
* @return unsorted array of integers to sort
146+
*/
147+
public static int[] getInput() {
148+
final int numElements = 6;
149+
int[] unsorted = new int[numElements];
150+
Scanner input = new Scanner(System.in);
151+
System.out.println("Enter any 6 Numbers for Unsorted Array : ");
152+
for (int i = 0; i < numElements; i++) {
153+
unsorted[i] = input.nextInt();
154+
}
155+
input.close();
156+
return unsorted;
157+
}
158+
159+
/**
160+
* Prints elements in heap.
161+
*
162+
* @param heap
163+
* array representing heap
164+
*/
165+
public static void printData(int[] heap) {
166+
System.out.println("Sorted Elements:");
167+
for (int i = 0; i < heap.length; i++) {
168+
System.out.print(" " + heap[i] + " ");
169+
}
170+
}
171+
172+
/**
173+
* Main method.
174+
*
175+
* @param args
176+
* the command line arguments
177+
*/
178+
public static void main(String[] args) {
179+
int[] heap = getInput();
180+
HeapSort data = new HeapSort(heap);
181+
int[] sorted = data.sort();
182+
printData(sorted);
183+
}
184+
185+
}

0 commit comments

Comments
 (0)