forked from Hithomelabs/Princeton1
Heapsort Implementation
This commit is contained in:
parent
b8f6081173
commit
2679715bb2
@ -16,6 +16,7 @@ dependencies {
|
||||
implementation project(':module5')
|
||||
implementation project(':module6')
|
||||
implementation project(':module7')
|
||||
implementation project(':module8')
|
||||
implementation project(':common')
|
||||
testImplementation project(':common')
|
||||
}
|
||||
|
@ -0,0 +1,23 @@
|
||||
package com.hithomelabs.clients.module8;
|
||||
|
||||
import com.hithomelabs.princeton1.common.Apple;
|
||||
import com.hithomelabs.princeton1.module8.HeapSort;
|
||||
|
||||
import java.util.Random;
|
||||
|
||||
public class HeapSortClient {
|
||||
|
||||
public static void main(String[] args) {
|
||||
int size = 10;
|
||||
Apple[] apples = new Apple[size];
|
||||
HeapSort<Apple> heap = new HeapSort<Apple>();
|
||||
|
||||
for (int i = 0; i < apples.length; i++) {
|
||||
apples[i] = new Apple(new Random().nextInt(10));
|
||||
}
|
||||
heap.sort(apples);
|
||||
for (int i = 0; i < apples.length; i++)
|
||||
System.out.println(apples[i]);
|
||||
}
|
||||
|
||||
}
|
21
module8/build.gradle
Normal file
21
module8/build.gradle
Normal file
@ -0,0 +1,21 @@
|
||||
plugins {
|
||||
id 'java'
|
||||
}
|
||||
|
||||
group = 'com.hithomelabs.princeton1.module8'
|
||||
version = 'unspecified'
|
||||
|
||||
repositories {
|
||||
mavenCentral()
|
||||
}
|
||||
|
||||
dependencies {
|
||||
testImplementation platform('org.junit:junit-bom:5.10.0')
|
||||
testImplementation 'org.junit.jupiter:junit-jupiter'
|
||||
implementation project(':module5')
|
||||
testImplementation project(':common')
|
||||
}
|
||||
|
||||
test {
|
||||
useJUnitPlatform()
|
||||
}
|
@ -0,0 +1,52 @@
|
||||
package com.hithomelabs.princeton1.module8;
|
||||
|
||||
/*
|
||||
* * Designing Heap as a wrapper class with static methods that perform sink and swim operations on underlying comparable[] array
|
||||
* * Heap does not have any instance variables, instance variables shall be housed by, the implementation using heaps, like priority queues and heap sort
|
||||
*/
|
||||
|
||||
import com.hithomelabs.princeton1.module5.AbstractCustomSorts;
|
||||
|
||||
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
|
||||
*/
|
||||
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
|
||||
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){
|
||||
T temp = arr[i];
|
||||
arr[i] = arr[j];
|
||||
arr[j] = temp;
|
||||
}
|
||||
|
||||
private static <T> boolean less(T v, T w){
|
||||
|
||||
if(((Comparable<T>)v).compareTo(w) < 1 ) return true;
|
||||
else return false;
|
||||
}
|
||||
|
||||
|
||||
}
|
@ -0,0 +1,31 @@
|
||||
package com.hithomelabs.princeton1.module8;
|
||||
|
||||
import com.hithomelabs.princeton1.module5.AbstractCustomSorts;
|
||||
|
||||
|
||||
public class HeapSort<E> extends AbstractCustomSorts<E> {
|
||||
|
||||
@Override
|
||||
public void sort(E[] arr) {
|
||||
int N = arr.length;
|
||||
|
||||
E[] heapArr = (E[]) new Object[N+1];
|
||||
// * * to simplify we copy original array from
|
||||
System.arraycopy(arr, 0, heapArr, 1, N);
|
||||
|
||||
// * * An array of size N holds a heap of size N-1
|
||||
coreSortingLogic(heapArr, N);
|
||||
System.arraycopy(heapArr, 1, arr, 0, 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);
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,51 @@
|
||||
package com.hithomelabs.princeton1.module8;
|
||||
|
||||
import com.hithomelabs.princeton1.common.Apple;
|
||||
import org.junit.jupiter.api.*;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Random;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
class HeapSortTest {
|
||||
|
||||
private HeapSort<Apple> heap;
|
||||
private ArrayList<Apple> apples;
|
||||
private Random random;
|
||||
|
||||
@BeforeEach
|
||||
void setUp() {
|
||||
heap = new HeapSort<Apple>();
|
||||
apples = new ArrayList<Apple>();
|
||||
random = new Random();
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Sort a heap of a 100 random sized apples")
|
||||
public void testSort(){
|
||||
for(int i = 0; i < 100; i++)
|
||||
apples.add(new Apple(random.nextInt(1000)));
|
||||
Apple[] arr = apples.toArray(new Apple[apples.size()]);
|
||||
heap.sort(arr);
|
||||
apples.sort(null);
|
||||
Apple[] sorted = apples.toArray(new Apple[apples.size()]);
|
||||
Assertions.assertArrayEquals(sorted, arr);
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Sort 100 apples in descending order")
|
||||
public void testSortBorder(){
|
||||
for(int i = 0; i < 100; i++)
|
||||
apples.add(new Apple(100 - i));
|
||||
Apple[] arr = apples.toArray(new Apple[apples.size()]);
|
||||
heap.sort(arr);
|
||||
apples.sort(null);
|
||||
Apple[] sorted = apples.toArray(new Apple[apples.size()]);
|
||||
Assertions.assertArrayEquals(sorted, arr);
|
||||
}
|
||||
|
||||
@AfterEach
|
||||
void tearDown() {
|
||||
}
|
||||
}
|
@ -17,4 +17,5 @@ include 'module5'
|
||||
include 'module6'
|
||||
include 'common'
|
||||
include 'module7'
|
||||
include 'module8'
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user