From b8e0e82c124f388f2540c54436abcd40ce1b9abd Mon Sep 17 00:00:00 2001 From: Shyam Thiagarajan Date: Sat, 8 Oct 2016 13:41:35 -0400 Subject: [PATCH 1/4] Added HeapSort --- HeapSort.java | 187 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 187 insertions(+) create mode 100644 HeapSort.java diff --git a/HeapSort.java b/HeapSort.java new file mode 100644 index 00000000..5da774e7 --- /dev/null +++ b/HeapSort.java @@ -0,0 +1,187 @@ +import java.util.Scanner; + +/** + * Heap Sort Algorithm. + * + */ +public class HeapSort { + /** + * array to store heap. + */ + private int[] heap; + /** + * size of heap. + */ + private int size; + + /** + * Constructor. + * + * @param heap + * array of unordered integers + */ + public HeapSort(int[] heap) { + this.setHeap(heap); + this.setSize(heap.length); + } + + /** + * Sets this.size with {@code length). + * + * @param length + * integer length of heap + */ + private void setSize(int length) { + this.size = length; + } + + /** + * Sets Heap with {@code heap}. + * + * @param heap + * array of unordered elements + */ + private void setHeap(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 + */ + private void swap(int first, int second) { + int temp = 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 + */ + private void heapSubtree(int rootIndex, int lastChild) { + int leftIndex = rootIndex * 2 + 1; // calculate index of left children + int rightIndex = rootIndex * 2 + 2; + boolean hasLeftChild = leftIndex <= lastChild; + boolean hasRightChild = rightIndex <= lastChild; + int root = this.heap[rootIndex]; + if (hasRightChild) { + int left = this.heap[leftIndex]; // get left child + int right = this.heap[rightIndex]; // get right child + if (left < right && left < root) { + this.swap(leftIndex, rootIndex); //swap left with root + this.heapSubtree(leftIndex, lastChild); + } else if (right < root) { + this.swap(rightIndex, rootIndex); //swap right with root + this.heapSubtree(rightIndex, lastChild); + } + } else if (hasLeftChild) { // if no right, but has left + int left = this.heap[leftIndex]; + if (left < root) { + this.swap(leftIndex, rootIndex); //swap left and root + this.heapSubtree(leftIndex, lastChild); + } + } + } + + /** + * Makes heap with {@code root} as root. + * + * @param root + * index of root of heap + */ + private void makeMinHeap(int root) { + int leftIndex = root * 2 + 1; // calculate index of left child + int rightIndex = root * 2 + 2; // calculate index of left child + boolean hasLeftChild = leftIndex < this.heap.length; + boolean hasRightChild = rightIndex < this.heap.length; + if (hasRightChild) { //if has left and right + this.makeMinHeap(leftIndex); + this.makeMinHeap(rightIndex); + this.heapSubtree(root, this.heap.length - 1); + } else if (hasLeftChild) { + this.heapSubtree(root, this.heap.length - 1); + } + } + + /** + * Gets the root of this.heap. + * + * @return root of this.heap + */ + private int getRoot() { + this.swap(0, this.size - 1); + this.size--; + this.heapSubtree(0, this.size - 1); + return this.heap[this.size]; // return old root + } + + /** + * Sorts this.heap with heap sort; displays ordered elements to console. + * + * @return {@code sorted} array of sorted elements + */ + public final int[] sort() { + this.makeMinHeap(0); // make min heap using index 0 as root. + int[] sorted = new int[this.size]; + int index = 0; + while (this.size > 0) { + int min = this.getRoot(); + sorted[index] = min; + index++; + } + return sorted; + } + + /** + * Gets input to sort. + * + * @return unsorted array of integers to sort + */ + public static int[] getInput() { + final int numElements = 6; + int[] unsorted = new int[numElements]; + Scanner input = new Scanner(System.in); + System.out.println("Enter any 6 Numbers for Unsorted Array : "); + for (int i = 0; i < numElements; i++) { + unsorted[i] = input.nextInt(); + } + input.close(); + return unsorted; + } + + /** + * Prints elements in heap. + * + * @param heap + * array representing heap + */ + public static void printData(int[] heap) { + System.out.println("Sorted Elements:"); + for (int i = 0; i < heap.length; i++) { + System.out.print(" " + heap[i] + " "); + } + } + + /** + * Main method. + * + * @param args + * the command line arguments + */ + public static void main(String[] args) { + int[] heap = getInput(); + HeapSort data = new HeapSort(heap); + int[] sorted = data.sort(); + printData(sorted); + } + +} From 6f517552561d0f4197b98d8eac24066252b4e46b Mon Sep 17 00:00:00 2001 From: Shyam Thiagarajan Date: Sat, 8 Oct 2016 13:42:37 -0400 Subject: [PATCH 2/4] Update HeapSort.java --- HeapSort.java | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/HeapSort.java b/HeapSort.java index 5da774e7..030c84b7 100644 --- a/HeapSort.java +++ b/HeapSort.java @@ -68,25 +68,25 @@ public class HeapSort { * index of last child */ private void heapSubtree(int rootIndex, int lastChild) { - int leftIndex = rootIndex * 2 + 1; // calculate index of left children + int leftIndex = rootIndex * 2 + 1; int rightIndex = rootIndex * 2 + 2; boolean hasLeftChild = leftIndex <= lastChild; boolean hasRightChild = rightIndex <= lastChild; int root = this.heap[rootIndex]; if (hasRightChild) { - int left = this.heap[leftIndex]; // get left child - int right = this.heap[rightIndex]; // get right child + int left = this.heap[leftIndex]; + int right = this.heap[rightIndex]; if (left < right && left < root) { - this.swap(leftIndex, rootIndex); //swap left with root + this.swap(leftIndex, rootIndex); this.heapSubtree(leftIndex, lastChild); } else if (right < root) { - this.swap(rightIndex, rootIndex); //swap right with root + this.swap(rightIndex, rootIndex); this.heapSubtree(rightIndex, lastChild); } } else if (hasLeftChild) { // if no right, but has left int left = this.heap[leftIndex]; if (left < root) { - this.swap(leftIndex, rootIndex); //swap left and root + this.swap(leftIndex, rootIndex); this.heapSubtree(leftIndex, lastChild); } } @@ -99,8 +99,8 @@ public class HeapSort { * index of root of heap */ private void makeMinHeap(int root) { - int leftIndex = root * 2 + 1; // calculate index of left child - int rightIndex = root * 2 + 2; // calculate index of left child + int leftIndex = root * 2 + 1; + int rightIndex = root * 2 + 2; boolean hasLeftChild = leftIndex < this.heap.length; boolean hasRightChild = rightIndex < this.heap.length; if (hasRightChild) { //if has left and right From a400f600ad8e8146717683427a2517b0fc819eb2 Mon Sep 17 00:00:00 2001 From: Shyam Thiagarajan Date: Sat, 8 Oct 2016 13:43:51 -0400 Subject: [PATCH 3/4] Update HeapSort.java --- HeapSort.java | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/HeapSort.java b/HeapSort.java index 030c84b7..ed3a07d4 100644 --- a/HeapSort.java +++ b/HeapSort.java @@ -70,10 +70,8 @@ public class HeapSort { private void heapSubtree(int rootIndex, int lastChild) { int leftIndex = rootIndex * 2 + 1; int rightIndex = rootIndex * 2 + 2; - boolean hasLeftChild = leftIndex <= lastChild; - boolean hasRightChild = rightIndex <= lastChild; int root = this.heap[rootIndex]; - if (hasRightChild) { + if (rightIndex <= lastChild) { // if has right and left children int left = this.heap[leftIndex]; int right = this.heap[rightIndex]; if (left < right && left < root) { @@ -83,7 +81,7 @@ public class HeapSort { this.swap(rightIndex, rootIndex); this.heapSubtree(rightIndex, lastChild); } - } else if (hasLeftChild) { // if no right, but has left + } else if (leftIndex <= lastChild) { // if no right child, but has left child int left = this.heap[leftIndex]; if (left < root) { this.swap(leftIndex, rootIndex); From 31c1c94dc35066c6327535b984b55961701dc396 Mon Sep 17 00:00:00 2001 From: Shyam Thiagarajan Date: Sat, 8 Oct 2016 13:44:26 -0400 Subject: [PATCH 4/4] Update HeapSort.java --- HeapSort.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/HeapSort.java b/HeapSort.java index ed3a07d4..7dd420a1 100644 --- a/HeapSort.java +++ b/HeapSort.java @@ -1,7 +1,7 @@ import java.util.Scanner; /** - * Heap Sort Algorithm. + * Heap Sort Algorithm. Implements MinHeap * */ public class HeapSort {