forked from Hithomelabs/Princeton1
Compare commits
4 Commits
2902cf5c61
...
9fba3bd4fe
Author | SHA1 | Date | |
---|---|---|---|
9fba3bd4fe | |||
0ca9295c03 | |||
3d72371e14 | |||
bbc040dcd4 |
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
|
@ -16,6 +16,7 @@ dependencies {
|
|||||||
implementation project(':module5')
|
implementation project(':module5')
|
||||||
implementation project(':module6')
|
implementation project(':module6')
|
||||||
implementation project(':module7')
|
implementation project(':module7')
|
||||||
|
implementation project(':module8')
|
||||||
implementation project(':common')
|
implementation project(':common')
|
||||||
testImplementation 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,33 @@
|
|||||||
|
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
|
||||||
|
*/
|
||||||
|
|
||||||
|
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 <T> void sink(T[] arr, int root, int N){
|
||||||
|
}
|
||||||
|
|
||||||
|
// * * Swim if element is not root, and parent is lesser than node
|
||||||
|
public static <T> void swim(T[] arr, int node, int N){
|
||||||
|
}
|
||||||
|
|
||||||
|
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,27 @@
|
|||||||
|
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 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) {
|
||||||
|
}
|
||||||
|
}
|
@ -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 'module6'
|
||||||
include 'common'
|
include 'common'
|
||||||
include 'module7'
|
include 'module7'
|
||||||
|
include 'module8'
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user