forked from Hithomelabs/Princeton1
Adding heap sort to practice
This commit is contained in:
parent
2679715bb2
commit
0f662bf89c
3
.gitignore
vendored
3
.gitignore
vendored
@ -1,6 +1,7 @@
|
|||||||
# Ignore Gradle project-specific cache directory
|
# Ignore Gradle project-specific cache directory
|
||||||
.gradle
|
.gradle
|
||||||
|
# Igonre the .idea directory
|
||||||
|
.idea
|
||||||
# Ignore Gradle build output directory
|
# Ignore Gradle build output directory
|
||||||
build
|
build
|
||||||
bin
|
bin
|
@ -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
|
* * 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 <T> void sink(T[] arr, int root, int N){
|
public static <T> 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
|
// * * Swim if element is not root, and parent is lesser than node
|
||||||
public static <T extends Comparable<T>> void swim(T[] arr, int node, int N){
|
public static <T extends Comparable<T>> 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 <T> void exch(T[] arr, int i, int j){
|
private static <T> void exch(T[] arr, int i, int j){
|
||||||
@ -43,7 +27,6 @@ public class Heap{
|
|||||||
}
|
}
|
||||||
|
|
||||||
private static <T> boolean less(T v, T w){
|
private static <T> boolean less(T v, T w){
|
||||||
|
|
||||||
if(((Comparable<T>)v).compareTo(w) < 1 ) return true;
|
if(((Comparable<T>)v).compareTo(w) < 1 ) return true;
|
||||||
else return false;
|
else return false;
|
||||||
}
|
}
|
||||||
|
@ -10,22 +10,18 @@ public class HeapSort<E> extends AbstractCustomSorts<E> {
|
|||||||
int N = arr.length;
|
int N = arr.length;
|
||||||
|
|
||||||
E[] heapArr = (E[]) new Object[N+1];
|
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);
|
System.arraycopy(arr, 0, heapArr, 1, N);
|
||||||
|
|
||||||
// * * An array of size N holds a heap of size N-1
|
// * * An array of size N holds a heap of size N-1
|
||||||
coreSortingLogic(heapArr, N);
|
coreSortingLogic(heapArr, N);
|
||||||
|
// * * Re-copying the sorted array to the original
|
||||||
System.arraycopy(heapArr, 1, arr, 0, N);
|
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) {
|
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);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user