Skip to content

Commit b8e0e82

Browse files
authored
Added HeapSort
1 parent 73bb72b commit b8e0e82

1 file changed

Lines changed: 187 additions & 0 deletions

File tree

HeapSort.java

Lines changed: 187 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,187 @@
1+
import java.util.Scanner;
2+
3+
/**
4+
* Heap Sort Algorithm.
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; // calculate index of left children
72+
int rightIndex = rootIndex * 2 + 2;
73+
boolean hasLeftChild = leftIndex <= lastChild;
74+
boolean hasRightChild = rightIndex <= lastChild;
75+
int root = this.heap[rootIndex];
76+
if (hasRightChild) {
77+
int left = this.heap[leftIndex]; // get left child
78+
int right = this.heap[rightIndex]; // get right child
79+
if (left < right && left < root) {
80+
this.swap(leftIndex, rootIndex); //swap left with root
81+
this.heapSubtree(leftIndex, lastChild);
82+
} else if (right < root) {
83+
this.swap(rightIndex, rootIndex); //swap right with root
84+
this.heapSubtree(rightIndex, lastChild);
85+
}
86+
} else if (hasLeftChild) { // if no right, but has left
87+
int left = this.heap[leftIndex];
88+
if (left < root) {
89+
this.swap(leftIndex, rootIndex); //swap left and root
90+
this.heapSubtree(leftIndex, lastChild);
91+
}
92+
}
93+
}
94+
95+
/**
96+
* Makes heap with {@code root} as root.
97+
*
98+
* @param root
99+
* index of root of heap
100+
*/
101+
private void makeMinHeap(int root) {
102+
int leftIndex = root * 2 + 1; // calculate index of left child
103+
int rightIndex = root * 2 + 2; // calculate index of left child
104+
boolean hasLeftChild = leftIndex < this.heap.length;
105+
boolean hasRightChild = rightIndex < this.heap.length;
106+
if (hasRightChild) { //if has left and right
107+
this.makeMinHeap(leftIndex);
108+
this.makeMinHeap(rightIndex);
109+
this.heapSubtree(root, this.heap.length - 1);
110+
} else if (hasLeftChild) {
111+
this.heapSubtree(root, this.heap.length - 1);
112+
}
113+
}
114+
115+
/**
116+
* Gets the root of this.heap.
117+
*
118+
* @return root of this.heap
119+
*/
120+
private int getRoot() {
121+
this.swap(0, this.size - 1);
122+
this.size--;
123+
this.heapSubtree(0, this.size - 1);
124+
return this.heap[this.size]; // return old root
125+
}
126+
127+
/**
128+
* Sorts this.heap with heap sort; displays ordered elements to console.
129+
*
130+
* @return {@code sorted} array of sorted elements
131+
*/
132+
public final int[] sort() {
133+
this.makeMinHeap(0); // make min heap using index 0 as root.
134+
int[] sorted = new int[this.size];
135+
int index = 0;
136+
while (this.size > 0) {
137+
int min = this.getRoot();
138+
sorted[index] = min;
139+
index++;
140+
}
141+
return sorted;
142+
}
143+
144+
/**
145+
* Gets input to sort.
146+
*
147+
* @return unsorted array of integers to sort
148+
*/
149+
public static int[] getInput() {
150+
final int numElements = 6;
151+
int[] unsorted = new int[numElements];
152+
Scanner input = new Scanner(System.in);
153+
System.out.println("Enter any 6 Numbers for Unsorted Array : ");
154+
for (int i = 0; i < numElements; i++) {
155+
unsorted[i] = input.nextInt();
156+
}
157+
input.close();
158+
return unsorted;
159+
}
160+
161+
/**
162+
* Prints elements in heap.
163+
*
164+
* @param heap
165+
* array representing heap
166+
*/
167+
public static void printData(int[] heap) {
168+
System.out.println("Sorted Elements:");
169+
for (int i = 0; i < heap.length; i++) {
170+
System.out.print(" " + heap[i] + " ");
171+
}
172+
}
173+
174+
/**
175+
* Main method.
176+
*
177+
* @param args
178+
* the command line arguments
179+
*/
180+
public static void main(String[] args) {
181+
int[] heap = getInput();
182+
HeapSort data = new HeapSort(heap);
183+
int[] sorted = data.sort();
184+
printData(sorted);
185+
}
186+
187+
}

0 commit comments

Comments
 (0)