Merge remote-tracking branch 'upstream/practice' into kruti-working-branch

This commit is contained in:
Kruti Shah 2025-02-19 23:30:11 +05:30
commit e0c20341ad
8 changed files with 161 additions and 1 deletions

3
.gitignore vendored
View File

@ -1,6 +1,7 @@
# Ignore Gradle project-specific cache directory
.gradle
# Igonre the .idea directory
.idea
# Ignore Gradle build output directory
build
bin

View File

@ -16,6 +16,7 @@ dependencies {
implementation project(':module5')
implementation project(':module6')
implementation project(':module7')
implementation project(':module8')
implementation project(':common')
testImplementation project(':common')
}

View File

@ -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
View 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()
}

View File

@ -0,0 +1,35 @@
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
* * 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 extends Comparable<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;
}
}

View File

@ -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) {
}
}

View File

@ -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() {
}
}

View File

@ -17,4 +17,5 @@ include 'module5'
include 'module6'
include 'common'
include 'module7'
include 'module8'