From 0f662bf89c5e106f915781fba33fa5535b6d3d20 Mon Sep 17 00:00:00 2001 From: hitanshu310 Date: Sun, 16 Feb 2025 17:05:10 +0530 Subject: [PATCH] Adding heap sort to practice --- .gitignore | 3 ++- .../hithomelabs/princeton1/module8/Heap.java | 19 +------------------ .../princeton1/module8/HeapSort.java | 16 ++++++---------- 3 files changed, 9 insertions(+), 29 deletions(-) diff --git a/.gitignore b/.gitignore index 4a305d7..cf74795 100644 --- a/.gitignore +++ b/.gitignore @@ -1,6 +1,7 @@ # Ignore Gradle project-specific cache directory .gradle - +# Igonre the .idea directory +.idea # Ignore Gradle build output directory build bin \ No newline at end of file diff --git a/module8/src/main/java/com/hithomelabs/princeton1/module8/Heap.java b/module8/src/main/java/com/hithomelabs/princeton1/module8/Heap.java index f5e5a65..b728767 100644 --- a/module8/src/main/java/com/hithomelabs/princeton1/module8/Heap.java +++ b/module8/src/main/java/com/hithomelabs/princeton1/module8/Heap.java @@ -11,29 +11,13 @@ public class Heap{ /* * * Sink Node T with to it's appropriate place in a Heap of size N, if it's children exist and are greater + * * Implement sink API to sink a node if it's sub-heap is not heap-order */ public static void sink(T[] arr, int root, int N){ - // * Check if at least one child exists - while(2 * root <= N){ - int j = 2 * root; - // * Check if the right child exists and is larger than the left child, if yes swap right and left child - if(j+1 <= N){ - if (less(arr[j], arr[j+1])) j++; - } - if (!less(arr[root], arr[j])) break; - exch(arr, root, j); - // * The root node has now sunken low, call sink recursively with new node to check if it sinks further - root = j; - } } // * * Swim if element is not root, and parent is lesser than node public static > void swim(T[] arr, int node, int N){ - while(node > 1){ - if(! less(arr[node/2],arr[node])) break; - exch(arr, node, node/2); - node = node/2; - } } private static void exch(T[] arr, int i, int j){ @@ -43,7 +27,6 @@ public class Heap{ } private static boolean less(T v, T w){ - if(((Comparable)v).compareTo(w) < 1 ) return true; else return false; } diff --git a/module8/src/main/java/com/hithomelabs/princeton1/module8/HeapSort.java b/module8/src/main/java/com/hithomelabs/princeton1/module8/HeapSort.java index bb71d81..55d7b91 100644 --- a/module8/src/main/java/com/hithomelabs/princeton1/module8/HeapSort.java +++ b/module8/src/main/java/com/hithomelabs/princeton1/module8/HeapSort.java @@ -10,22 +10,18 @@ public class HeapSort extends AbstractCustomSorts { int N = arr.length; E[] heapArr = (E[]) new Object[N+1]; - // * * to simplify we copy original array from + // * * to simplify we copy original array and write it to the new array starting index 1 System.arraycopy(arr, 0, heapArr, 1, N); - // * * An array of size N holds a heap of size N-1 coreSortingLogic(heapArr, N); + // * * Re-copying the sorted array to the original System.arraycopy(heapArr, 1, arr, 0, N); } + /* + * * Implement the core sorting logic + * * P.S the provision of making the index 0 null for ease of use has already been done above + */ private void coreSortingLogic(E[] arr, int N) { - // * * Converting array to max-heap an array in place - for (int i = N/2; i >= 1; i--) - Heap.sink(arr, i, N); - // * * After converting to max-heap, in every iteration remove the max element of the heap - while(N > 1){ - exch(arr, 1, N--); - Heap.sink(arr, 1, N); - } } }