forked from Hithomelabs/Princeton1
Compare commits
5 Commits
7499161725
...
a4ccbee99c
Author | SHA1 | Date | |
---|---|---|---|
a4ccbee99c | |||
3c3eee2e4d | |||
f0b577b5f6 | |||
7ba2d22e43 | |||
fdca30f50a |
@ -16,7 +16,7 @@ jobs:
|
|||||||
uses: actions/setup-java@v4
|
uses: actions/setup-java@v4
|
||||||
with:
|
with:
|
||||||
distribution: 'zulu'
|
distribution: 'zulu'
|
||||||
java-version: '21'
|
java-version: '11'
|
||||||
- name: Validate Gradle Wrapper
|
- name: Validate Gradle Wrapper
|
||||||
uses: gradle/actions/wrapper-validation@v3
|
uses: gradle/actions/wrapper-validation@v3
|
||||||
- name: Gradle build
|
- name: Gradle build
|
||||||
|
1
.idea/gradle.xml
generated
1
.idea/gradle.xml
generated
@ -10,6 +10,7 @@
|
|||||||
<option value="$PROJECT_DIR$" />
|
<option value="$PROJECT_DIR$" />
|
||||||
<option value="$PROJECT_DIR$/clients" />
|
<option value="$PROJECT_DIR$/clients" />
|
||||||
<option value="$PROJECT_DIR$/module4" />
|
<option value="$PROJECT_DIR$/module4" />
|
||||||
|
<option value="$PROJECT_DIR$/module5" />
|
||||||
</set>
|
</set>
|
||||||
</option>
|
</option>
|
||||||
</GradleProjectSettings>
|
</GradleProjectSettings>
|
||||||
|
8
.idea/modules.xml
generated
Normal file
8
.idea/modules.xml
generated
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project version="4">
|
||||||
|
<component name="ProjectModuleManager">
|
||||||
|
<modules>
|
||||||
|
<module fileurl="file://$PROJECT_DIR$/.idea/modules/module4/Pricenton1.module4.test.iml" filepath="$PROJECT_DIR$/.idea/modules/module4/Pricenton1.module4.test.iml" />
|
||||||
|
</modules>
|
||||||
|
</component>
|
||||||
|
</project>
|
8
.idea/modules/module4/Pricenton1.module4.test.iml
generated
Normal file
8
.idea/modules/module4/Pricenton1.module4.test.iml
generated
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<module version="4">
|
||||||
|
<component name="AdditionalModuleElements">
|
||||||
|
<content url="file://$MODULE_DIR$/../../../module4/src/test" dumb="true">
|
||||||
|
<sourceFolder url="file://$MODULE_DIR$/../../../module4/src/test/java" isTestSource="true" />
|
||||||
|
</content>
|
||||||
|
</component>
|
||||||
|
</module>
|
6
.vscode/settings.json
vendored
6
.vscode/settings.json
vendored
@ -1,4 +1,8 @@
|
|||||||
{
|
{
|
||||||
"java.compile.nullAnalysis.mode": "automatic",
|
"java.compile.nullAnalysis.mode": "automatic",
|
||||||
"jdk.java.onSave.organizeImports": true
|
"jdk.java.onSave.organizeImports": true,
|
||||||
|
"java.configuration.updateBuildConfiguration": "interactive",
|
||||||
|
"cSpell.words": [
|
||||||
|
"hithomelabs"
|
||||||
|
]
|
||||||
}
|
}
|
@ -1,10 +1,18 @@
|
|||||||
#Data Structures
|
# Data Structures
|
||||||
|
|
||||||
1. Stacks:
|
1. Stacks:
|
||||||
- Linear data structure
|
- [API](../module4/src/main/java/com/hithomelabs/princeton1/module4/Stack.java)
|
||||||
- Can have various implementations
|
- [Sample client](../clients/src/main/java/com/hithomelabs/clients/ArrayStackClient.java)
|
||||||
- Should support (LIFO)
|
- Linear data structure
|
||||||
- Primitive implementation (Using arrays of primitive data types)
|
- Can have various implementations
|
||||||
- Other implementations (Using linked lists)
|
- Should support (LIFO)
|
||||||
2. Queues:
|
- [Primitive implementation](../module4/src/main/java/com/hithomelabs/princeton1/module4/ArrayStack.java) (Using arrays of primitive data types)
|
||||||
|
- [Other implementations](../module4/src/main/java/com/hithomelabs/princeton1/module4/LinkedStack.java) (Using linked lists)
|
||||||
|
2. Queues:
|
||||||
|
- [API](../module4/src/main/java/com/hithomelabs/princeton1/module4/Queue.java)
|
||||||
|
- [sample Client](../clients/src/main/java/com/hithomelabs/clients/LinkedQueueClient.java)
|
||||||
|
- Linear data structure.
|
||||||
|
- Can have various implementations.
|
||||||
|
- Should support FIFO.
|
||||||
|
- [Primitive implementation](../module4/src/main/java/com/hithomelabs/princeton1/module4/ArrayQueue.java) (Using array of primitive data types)
|
||||||
|
- Other implementation ([using linked lists](../module4/src/main/java/com/hithomelabs/princeton1/module4/LinkedQueue.java))
|
@ -13,6 +13,12 @@ dependencies {
|
|||||||
testImplementation platform('org.junit:junit-bom:5.10.0')
|
testImplementation platform('org.junit:junit-bom:5.10.0')
|
||||||
testImplementation 'org.junit.jupiter:junit-jupiter'
|
testImplementation 'org.junit.jupiter:junit-jupiter'
|
||||||
implementation project(':module4')
|
implementation project(':module4')
|
||||||
|
implementation project(':module5')
|
||||||
|
}
|
||||||
|
java {
|
||||||
|
toolchain {
|
||||||
|
languageVersion = JavaLanguageVersion.of(11)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
test {
|
test {
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
package com.hithomelabs.clients;
|
package com.hithomelabs.clients.module4;
|
||||||
|
|
||||||
import com.hithomelabs.princeton1.module4.ArrayQueue;
|
import com.hithomelabs.princeton1.module4.ArrayQueue;
|
||||||
import com.hithomelabs.princeton1.module4.Queue;
|
import com.hithomelabs.princeton1.module4.Queue;
|
@ -1,4 +1,4 @@
|
|||||||
package com.hithomelabs.clients;
|
package com.hithomelabs.clients.module4;
|
||||||
|
|
||||||
import com.hithomelabs.princeton1.module4.ArrayStack;
|
import com.hithomelabs.princeton1.module4.ArrayStack;
|
||||||
import com.hithomelabs.princeton1.module4.Stack;
|
import com.hithomelabs.princeton1.module4.Stack;
|
@ -1,4 +1,4 @@
|
|||||||
package com.hithomelabs.clients;
|
package com.hithomelabs.clients.module4;
|
||||||
|
|
||||||
import com.hithomelabs.princeton1.module4.LinkedQueue;
|
import com.hithomelabs.princeton1.module4.LinkedQueue;
|
||||||
import com.hithomelabs.princeton1.module4.Queue;
|
import com.hithomelabs.princeton1.module4.Queue;
|
@ -1,4 +1,4 @@
|
|||||||
package com.hithomelabs.clients;
|
package com.hithomelabs.clients.module4;
|
||||||
|
|
||||||
|
|
||||||
import com.hithomelabs.princeton1.module4.LinkedStack;
|
import com.hithomelabs.princeton1.module4.LinkedStack;
|
@ -0,0 +1,28 @@
|
|||||||
|
package com.hithomelabs.clients.module5;
|
||||||
|
|
||||||
|
import com.hithomelabs.princeton1.module5.Insertion;
|
||||||
|
import com.hithomelabs.princeton1.module5.Apple;
|
||||||
|
import com.hithomelabs.princeton1.module5.Orange;
|
||||||
|
|
||||||
|
public class InsertionClient {
|
||||||
|
|
||||||
|
public static void main(String[] args){
|
||||||
|
|
||||||
|
Apple[] apples = new Apple[3];
|
||||||
|
Orange[] oranges = new Orange[3];
|
||||||
|
Insertion<Apple> insertion = new Insertion<>();
|
||||||
|
|
||||||
|
apples[0] = new Apple(3);
|
||||||
|
apples[1] = new Apple(5);
|
||||||
|
apples[2] = new Apple(4);
|
||||||
|
insertion.sort(apples);
|
||||||
|
|
||||||
|
//* * Sample output
|
||||||
|
for (int i = 0; i < apples.length; i++)
|
||||||
|
System.out.println(apples[i]);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,66 @@
|
|||||||
|
package com.hithomelabs.clients.module5;
|
||||||
|
|
||||||
|
import com.hithomelabs.princeton1.module5.Selection;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
|
||||||
|
public class SelectionClient {
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
public static void main(String[] args){
|
||||||
|
|
||||||
|
Apple[] apples = new Apple[3];
|
||||||
|
Orange[] oranges = new Orange[3];
|
||||||
|
Selection<Apple> selection = new Selection<Apple>();
|
||||||
|
|
||||||
|
apples[0] = new Apple(3);
|
||||||
|
apples[1] = new Apple(5);
|
||||||
|
apples[2] = new Apple(4);
|
||||||
|
selection.sort(apples);
|
||||||
|
|
||||||
|
//* * Sample output
|
||||||
|
for (int i = 0; i < apples.length; i++)
|
||||||
|
System.out.println(apples[i]);
|
||||||
|
|
||||||
|
oranges[0] = new Orange(4);
|
||||||
|
oranges[1] = new Orange(1);
|
||||||
|
// * Should give runtime exception as ClassCastException is a runtime exception
|
||||||
|
//selection.sort(oranges);
|
||||||
|
Selection<Orange> selection2 = new Selection<Orange>();
|
||||||
|
// * Should result in a compile time exception, as casting to Orange will fail
|
||||||
|
//selection2.sort(apples);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static class Orange{
|
||||||
|
private int size;
|
||||||
|
Orange(int size){
|
||||||
|
this.size = size;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return "An orange of size "+size;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static class Apple implements Comparable<Apple>{
|
||||||
|
private int size;
|
||||||
|
|
||||||
|
Apple(int size){
|
||||||
|
this.size = size;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return "An apple of size "+size;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int compareTo(Apple that) {
|
||||||
|
if (this.size < that.size) return -1;
|
||||||
|
else if (this.size == that.size)return 0;
|
||||||
|
else return 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,29 @@
|
|||||||
|
package com.hithomelabs.clients.module5;
|
||||||
|
|
||||||
|
import com.hithomelabs.princeton1.module5.Apple;
|
||||||
|
import com.hithomelabs.princeton1.module5.Shell;
|
||||||
|
|
||||||
|
import java.util.Arrays;
|
||||||
|
import java.util.Random;
|
||||||
|
|
||||||
|
public class ShellClient {
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
|
||||||
|
int size = 100;
|
||||||
|
Apple[] apples = new Apple[size];
|
||||||
|
Shell<Apple> shell = new Shell<Apple>();
|
||||||
|
|
||||||
|
for (int i = 0; i < apples.length; i++) {
|
||||||
|
apples[i] = new Apple(new Random().nextInt(1000));
|
||||||
|
}
|
||||||
|
Apple[] applesCopy = Arrays.copyOf(apples, size);
|
||||||
|
shell.sort(apples);
|
||||||
|
shell.insertionSort(applesCopy);
|
||||||
|
|
||||||
|
//* * Sample output
|
||||||
|
for (int i = 0; i < apples.length; i++)
|
||||||
|
System.out.println(apples[i]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -1,8 +1,9 @@
|
|||||||
package com.hithomelabs.princeton1.module4;
|
package com.hithomelabs.princeton1.module4;
|
||||||
|
|
||||||
import javax.annotation.Nonnull;
|
|
||||||
import java.util.Iterator;
|
import java.util.Iterator;
|
||||||
|
|
||||||
|
import javax.annotation.Nonnull;
|
||||||
|
|
||||||
public class ArrayQueue<E> extends Queue<E>{
|
public class ArrayQueue<E> extends Queue<E>{
|
||||||
|
|
||||||
// insertion from tail, removal from head
|
// insertion from tail, removal from head
|
||||||
|
@ -1,13 +1,13 @@
|
|||||||
package com.hithomelabs.princeton1.module4;
|
package com.hithomelabs.princeton1.module4;
|
||||||
import javax.annotation.Nonnull;
|
|
||||||
import java.util.Iterator;
|
import java.util.Iterator;
|
||||||
|
import javax.annotation.Nonnull;
|
||||||
|
|
||||||
// Concrete implementation of stack using arrays
|
// Concrete implementation of stack using arrays
|
||||||
// Creating a generic stack of type E
|
// Creating a generic stack of type E
|
||||||
public class ArrayStack<E> extends Stack<E> {
|
public class ArrayStack<E> extends Stack<E> {
|
||||||
|
|
||||||
// Capacity and size are two variables, capacity determines total capacity of array, capacity defaults at 10
|
// Capacity and size are two variables, capacity determines total capacity of array, capacity defaults at 10
|
||||||
// Everytime size == capacity, capacity = 2 * capacity
|
// every time size == capacity, capacity = 2 * capacity
|
||||||
private static final int DEFAULT_CAPACITY = 10;
|
private static final int DEFAULT_CAPACITY = 10;
|
||||||
private int capacity;
|
private int capacity;
|
||||||
private int size;
|
private int size;
|
||||||
@ -18,7 +18,7 @@ public class ArrayStack<E> extends Stack<E> {
|
|||||||
arr = (E[]) new Object[this.capacity];
|
arr = (E[]) new Object[this.capacity];
|
||||||
}
|
}
|
||||||
|
|
||||||
// Constructor chaining, default constructor will call parametrised constructor with default initial capacity 10
|
// Constructor chaining, default constructor will call parametrized constructor with default initial capacity 10
|
||||||
public ArrayStack(){
|
public ArrayStack(){
|
||||||
this(DEFAULT_CAPACITY);
|
this(DEFAULT_CAPACITY);
|
||||||
}
|
}
|
||||||
@ -64,7 +64,7 @@ public class ArrayStack<E> extends Stack<E> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private void checkResize() {
|
private void checkResize() {
|
||||||
if (size < capacity / 4 && capacity >= 40){
|
if (size < capacity / 4 && capacity >= 20){
|
||||||
capacity = capacity / 2;
|
capacity = capacity / 2;
|
||||||
changeCapacity(capacity);
|
changeCapacity(capacity);
|
||||||
}
|
}
|
||||||
|
@ -60,7 +60,7 @@ public class LinkedQueue<E> extends Queue<E>{
|
|||||||
Node current = head;
|
Node current = head;
|
||||||
@Override
|
@Override
|
||||||
public boolean hasNext() {
|
public boolean hasNext() {
|
||||||
return current != tail;
|
return current != null;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -0,0 +1,76 @@
|
|||||||
|
package com.hithomelabs.princeton1.module4;
|
||||||
|
|
||||||
|
import java.util.Iterator;
|
||||||
|
import org.junit.jupiter.api.AfterEach;
|
||||||
|
import org.junit.jupiter.api.BeforeEach;
|
||||||
|
import org.junit.jupiter.api.DisplayName;
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
|
|
||||||
|
import static org.junit.jupiter.api.Assertions.*;
|
||||||
|
|
||||||
|
class ArrayQueueTest {
|
||||||
|
|
||||||
|
private Queue<Integer> queue;
|
||||||
|
|
||||||
|
@BeforeEach
|
||||||
|
void setUp() {
|
||||||
|
queue = new ArrayQueue<Integer>();
|
||||||
|
}
|
||||||
|
|
||||||
|
@AfterEach
|
||||||
|
void tearDown() {
|
||||||
|
queue = null;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("Empty queue must return size zero")
|
||||||
|
public void testEmptyQueueSize(){
|
||||||
|
assertEquals(0, queue.size());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("Enqueuing must work as expected")
|
||||||
|
public void testEnqueue(){
|
||||||
|
queue.enqueue(1);
|
||||||
|
queue.enqueue(2);
|
||||||
|
assertEquals(2,queue.size());
|
||||||
|
Iterator<Integer> iterator = queue.iterator();
|
||||||
|
assertEquals(1, iterator.next());
|
||||||
|
assertEquals(2, iterator.next());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("Dequeueing must work as expected")
|
||||||
|
public void testDequeue(){
|
||||||
|
queue.enqueue(1);
|
||||||
|
queue.enqueue(2);
|
||||||
|
assertEquals(1, queue.dequeue());
|
||||||
|
queue.enqueue(3);
|
||||||
|
assertEquals(2, queue.size());
|
||||||
|
assertEquals(2, queue.dequeue());
|
||||||
|
assertEquals(3, queue.dequeue());
|
||||||
|
assertEquals(0, queue.size());
|
||||||
|
assertNull(queue.dequeue());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("Testing resizable array and iterator")
|
||||||
|
public void testResize(){
|
||||||
|
for(int i = 0; i < 21; i++)
|
||||||
|
queue.enqueue(i);
|
||||||
|
for(int i = 0; i < 13; i++){
|
||||||
|
queue.dequeue();
|
||||||
|
}
|
||||||
|
assertEquals(13, queue.dequeue());
|
||||||
|
assertEquals(7,queue.size());
|
||||||
|
Integer[] arr = new Integer[queue.size()];
|
||||||
|
Iterator<Integer> iterator = queue.iterator();
|
||||||
|
int count = 0;
|
||||||
|
while(iterator.hasNext()){
|
||||||
|
arr[count++] = iterator.next();
|
||||||
|
}
|
||||||
|
assertArrayEquals(new Integer[]{14, 15, 16, 17, 18, 19, 20}, arr);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,85 @@
|
|||||||
|
package com.hithomelabs.princeton1.module4;
|
||||||
|
|
||||||
|
import org.junit.jupiter.api.AfterEach;
|
||||||
|
import org.junit.jupiter.api.BeforeEach;
|
||||||
|
import org.junit.jupiter.api.DisplayName;
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
|
import java.util.Iterator;
|
||||||
|
|
||||||
|
import static org.junit.jupiter.api.Assertions.*;
|
||||||
|
|
||||||
|
class ArrayStackTest {
|
||||||
|
|
||||||
|
private Stack<String> stack;
|
||||||
|
|
||||||
|
@BeforeEach
|
||||||
|
void setUp() {
|
||||||
|
stack = new ArrayStack<String>();
|
||||||
|
}
|
||||||
|
|
||||||
|
@AfterEach
|
||||||
|
void tearDown() {
|
||||||
|
stack = null;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("Empty stack must return size zero")
|
||||||
|
public void testEmptyStackSize(){
|
||||||
|
assertEquals(0, stack.size());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("Push must work as expected")
|
||||||
|
public void testEnqueue(){
|
||||||
|
stack.push("Hello");
|
||||||
|
stack.push("World");
|
||||||
|
assertEquals(2,stack.size());
|
||||||
|
Iterator<String> iterator = stack.iterator();
|
||||||
|
assertEquals("Hello", iterator.next());
|
||||||
|
assertEquals("World", iterator.next());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("Pop must work as expected")
|
||||||
|
public void testDequeue(){
|
||||||
|
stack.push("hello");
|
||||||
|
stack.push("world");
|
||||||
|
assertEquals("world", stack.pop());
|
||||||
|
stack.push("I");
|
||||||
|
assertEquals(2, stack.size());
|
||||||
|
assertEquals("I", stack.pop());
|
||||||
|
assertEquals("hello", stack.pop());
|
||||||
|
assertEquals(0, stack.size());
|
||||||
|
assertNull(stack.pop());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("Testing resizable array and iterator")
|
||||||
|
public void testResize(){
|
||||||
|
stack.push("Hello");
|
||||||
|
stack.push("world");
|
||||||
|
stack.push("I");
|
||||||
|
stack.push("am");
|
||||||
|
stack.push("testing");
|
||||||
|
stack.push("my");
|
||||||
|
stack.push("implementation");
|
||||||
|
stack.push("of");
|
||||||
|
stack.push("Stacks");
|
||||||
|
stack.push("Data");
|
||||||
|
stack.push("Structure");
|
||||||
|
assertEquals("Structure", stack.pop());
|
||||||
|
int counter = 6;
|
||||||
|
while(counter-- > 0)
|
||||||
|
stack.pop();
|
||||||
|
assertEquals(4,stack.size());
|
||||||
|
String[] arr = new String[stack.size()];
|
||||||
|
Iterator<String> iterator = stack.iterator();
|
||||||
|
int count = 0;
|
||||||
|
while(iterator.hasNext()){
|
||||||
|
arr[count++] = iterator.next();
|
||||||
|
}
|
||||||
|
assertArrayEquals(new String[]{"Hello", "world", "I", "am"}, arr);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,76 @@
|
|||||||
|
package com.hithomelabs.princeton1.module4;
|
||||||
|
|
||||||
|
import org.junit.jupiter.api.AfterEach;
|
||||||
|
import org.junit.jupiter.api.BeforeEach;
|
||||||
|
import org.junit.jupiter.api.DisplayName;
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
|
import java.util.Iterator;
|
||||||
|
|
||||||
|
import static org.junit.jupiter.api.Assertions.*;
|
||||||
|
|
||||||
|
class LinkedQueueTest {
|
||||||
|
|
||||||
|
private Queue<Integer> queue;
|
||||||
|
|
||||||
|
@BeforeEach
|
||||||
|
void setUp() {
|
||||||
|
queue = new LinkedQueue<>();
|
||||||
|
}
|
||||||
|
|
||||||
|
@AfterEach
|
||||||
|
void tearDown() {
|
||||||
|
queue = null;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("Empty queue must return size zero")
|
||||||
|
public void testEmptyQueueSize(){
|
||||||
|
assertEquals(0, queue.size());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("Enqueuing must work as expected")
|
||||||
|
public void testEnqueue(){
|
||||||
|
queue.enqueue(1);
|
||||||
|
queue.enqueue(2);
|
||||||
|
assertEquals(2,queue.size());
|
||||||
|
Iterator<Integer> iterator = queue.iterator();
|
||||||
|
assertEquals(1, iterator.next());
|
||||||
|
assertEquals(2, iterator.next());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("Dequeueing must work as expected")
|
||||||
|
public void testDequeue(){
|
||||||
|
queue.enqueue(1);
|
||||||
|
queue.enqueue(2);
|
||||||
|
assertEquals(1, queue.dequeue());
|
||||||
|
queue.enqueue(3);
|
||||||
|
assertEquals(2, queue.size());
|
||||||
|
assertEquals(2, queue.dequeue());
|
||||||
|
assertEquals(3, queue.dequeue());
|
||||||
|
assertEquals(0, queue.size());
|
||||||
|
assertNull(queue.dequeue());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("Testing resizable array and iterator")
|
||||||
|
public void testResize(){
|
||||||
|
for(int i = 0; i < 21; i++)
|
||||||
|
queue.enqueue(i);
|
||||||
|
for(int i = 0; i < 13; i++){
|
||||||
|
queue.dequeue();
|
||||||
|
}
|
||||||
|
assertEquals(13, queue.dequeue());
|
||||||
|
assertEquals(7,queue.size());
|
||||||
|
Integer[] arr = new Integer[queue.size()];
|
||||||
|
Iterator<Integer> iterator = queue.iterator();
|
||||||
|
int count = 0;
|
||||||
|
while(iterator.hasNext()){
|
||||||
|
arr[count++] = iterator.next();
|
||||||
|
}
|
||||||
|
assertArrayEquals(new Integer[]{14, 15, 16, 17, 18, 19, 20}, arr);
|
||||||
|
}
|
||||||
|
}
|
23
module5/build.gradle
Normal file
23
module5/build.gradle
Normal file
@ -0,0 +1,23 @@
|
|||||||
|
plugins {
|
||||||
|
id 'java'
|
||||||
|
}
|
||||||
|
|
||||||
|
group = 'com.hithomelabs.princeton1.module5'
|
||||||
|
version = 'unspecified'
|
||||||
|
|
||||||
|
repositories {
|
||||||
|
mavenCentral()
|
||||||
|
}
|
||||||
|
|
||||||
|
dependencies {
|
||||||
|
testImplementation platform('org.junit:junit-bom:5.10.0')
|
||||||
|
testImplementation 'org.junit.jupiter:junit-jupiter'
|
||||||
|
}
|
||||||
|
java {
|
||||||
|
toolchain {
|
||||||
|
languageVersion = JavaLanguageVersion.of(11)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
test {
|
||||||
|
useJUnitPlatform()
|
||||||
|
}
|
@ -0,0 +1,17 @@
|
|||||||
|
package com.hithomelabs.princeton1.module5;
|
||||||
|
|
||||||
|
public abstract class AbstractCustomSorts<E> {
|
||||||
|
|
||||||
|
public abstract void sort(E[] arr);
|
||||||
|
|
||||||
|
public void exch(E[] arr, int j, int i) {
|
||||||
|
E temp = arr[i];
|
||||||
|
arr[i] = arr[j];
|
||||||
|
arr[j] = temp;
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean less(Comparable<E> e1, E e2) {
|
||||||
|
return e1.compareTo(e2) < 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,37 @@
|
|||||||
|
package com.hithomelabs.princeton1.module5;
|
||||||
|
|
||||||
|
import java.util.Objects;
|
||||||
|
|
||||||
|
public class Apple implements Comparable<Apple> {
|
||||||
|
private int size;
|
||||||
|
|
||||||
|
public Apple(int size) {
|
||||||
|
this.size = size;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean equals(Object obj) {
|
||||||
|
if(this == obj) return true;
|
||||||
|
if (obj == null || this.getClass() != obj.getClass())
|
||||||
|
return false;
|
||||||
|
Apple that = (Apple) obj;
|
||||||
|
return Objects.equals(this.size, that.size);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int hashCode() {
|
||||||
|
return Objects.hash(size);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return "An apple of size " + size;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int compareTo(Apple that) {
|
||||||
|
if (this.size < that.size) return -1;
|
||||||
|
else if (this.size == that.size) return 0;
|
||||||
|
else return 1;
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,23 @@
|
|||||||
|
package com.hithomelabs.princeton1.module5;
|
||||||
|
|
||||||
|
public class Insertion<E> extends AbstractCustomSorts<E> {
|
||||||
|
|
||||||
|
public void sort(E[] arr){
|
||||||
|
if (arr == null) return;
|
||||||
|
else{
|
||||||
|
int N = arr.length;
|
||||||
|
// * * swap arr[i] with each element greater to it's left
|
||||||
|
for (int i = 1; i < N; i++){
|
||||||
|
int j = i;
|
||||||
|
while(j >= 1 && less((Comparable<E>)arr[j], arr[j-1])){
|
||||||
|
exch(arr, j, j-1);
|
||||||
|
j = j-1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,14 @@
|
|||||||
|
package com.hithomelabs.princeton1.module5;
|
||||||
|
|
||||||
|
public class Orange {
|
||||||
|
private int size;
|
||||||
|
|
||||||
|
public Orange(int size) {
|
||||||
|
this.size = size;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return "An orange of size " + size;
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,23 @@
|
|||||||
|
package com.hithomelabs.princeton1.module5;
|
||||||
|
|
||||||
|
|
||||||
|
public class Selection<E> extends AbstractCustomSorts<E>{
|
||||||
|
|
||||||
|
/*
|
||||||
|
* * Selection sort "selects" the smallest element and swaps it with arr[0] of the array
|
||||||
|
* * Then proceeds to do the same swapping arr[i] with arr[i:arr.length-1]
|
||||||
|
*/
|
||||||
|
public void sort(E[] arr){
|
||||||
|
if (arr == null) return;
|
||||||
|
Comparable<E>[] arr1 = (Comparable<E>[]) arr;
|
||||||
|
for(int i = 0; i < arr1.length - 1; i++){
|
||||||
|
int minIndex = i;
|
||||||
|
for(int j = i+1; j < arr.length; j ++){
|
||||||
|
if (less((Comparable<E>) arr[j], arr[minIndex])) minIndex = j;
|
||||||
|
}
|
||||||
|
exch(arr, i, minIndex);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,66 @@
|
|||||||
|
package com.hithomelabs.princeton1.module5;
|
||||||
|
|
||||||
|
public class Shell<E> extends AbstractCustomSorts<E> {
|
||||||
|
|
||||||
|
// * * sample metadata class to compare no. of sorts and compares, shell sort vs insertion sort
|
||||||
|
private class MetaData{
|
||||||
|
|
||||||
|
int compares;
|
||||||
|
int swaps;
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* * We will be performing h sort
|
||||||
|
* * Suppose or function to determine subsequent values of h is:
|
||||||
|
* * h(i+1) = 3*h(i) + 1; h = 1, 4, 13, 40, 121, ....
|
||||||
|
* * h will never be perfectly divisible by 3; further dividing h[i]/3 will give h[i-1]
|
||||||
|
* * We want to h-sort using larges value of h smaller than N, followed by smaller values of h
|
||||||
|
* * If N = 100, we h-sort by 40, meaning every 40th element will be in order
|
||||||
|
* * Then we h-sort by 13, meaning every 40th and every 13th element will be in order and every 40th element will be in order
|
||||||
|
* * Finally, when h will come out as 1, it will be same as insertion sort, however by that time the array will almost be sorted
|
||||||
|
* * Insertion sort for an almost sorted array is linear time.
|
||||||
|
* * As a case study we implement both insertion sort and selection sort for an array of 1000 nos.
|
||||||
|
* * And check number of compares and no. of exchanges in each case
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public void sort(E[] arr) {
|
||||||
|
MetaData metaData = new MetaData();
|
||||||
|
int N = arr.length;
|
||||||
|
int h = 1;
|
||||||
|
// * * Calculates the largest value of h greater than n
|
||||||
|
while (3 * h + 1 < N) {
|
||||||
|
h = 3 * h + 1;
|
||||||
|
}
|
||||||
|
while (h >= 1) {
|
||||||
|
h = hsort(arr, h, metaData);
|
||||||
|
h = h / 3;
|
||||||
|
}
|
||||||
|
System.out.println("Array sorted (shell sort) with " + metaData.compares + " compares and " + metaData.swaps + " swaps");
|
||||||
|
}
|
||||||
|
|
||||||
|
private int hsort(E[] arr, int h, MetaData metadata) {
|
||||||
|
int N = arr.length;
|
||||||
|
for(int i = h; i < N; i++){
|
||||||
|
int j = i;
|
||||||
|
++metadata.compares;
|
||||||
|
while(j >= h && less((Comparable<E>) arr[j], arr[j-h])){
|
||||||
|
++metadata.swaps;
|
||||||
|
exch(arr, j, j-h);
|
||||||
|
j = j - h;
|
||||||
|
++metadata.compares;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return h;
|
||||||
|
}
|
||||||
|
/*
|
||||||
|
! sample implementation of insertion sort as h-sort of h = 1
|
||||||
|
* Will just be comparing the number of saps taken across both implementations
|
||||||
|
*/
|
||||||
|
public void insertionSort(E[] arr){
|
||||||
|
MetaData metaData = new MetaData();
|
||||||
|
int h = 1;
|
||||||
|
h = hsort(arr, h, metaData);
|
||||||
|
System.out.println("Array sorted (insertion sort) with " + metaData.compares + " compares and " + metaData.swaps + " swaps");
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,63 @@
|
|||||||
|
package com.hithomelabs.princeton1.module5;
|
||||||
|
|
||||||
|
import org.junit.jupiter.api.AfterEach;
|
||||||
|
import org.junit.jupiter.api.BeforeEach;
|
||||||
|
import org.junit.jupiter.api.DisplayName;
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.Random;
|
||||||
|
|
||||||
|
import static org.junit.jupiter.api.Assertions.*;
|
||||||
|
|
||||||
|
class SortTest {
|
||||||
|
|
||||||
|
private ArrayList<Apple> apples;
|
||||||
|
private AbstractCustomSorts<Apple> sortingAlgorithm;
|
||||||
|
private Random random;
|
||||||
|
|
||||||
|
@BeforeEach
|
||||||
|
void setUp() {
|
||||||
|
apples = new ArrayList<Apple>();
|
||||||
|
//sortingAlgorithm = new Selection<Apple>();
|
||||||
|
random = new Random();
|
||||||
|
}
|
||||||
|
|
||||||
|
private void testSort(AbstractCustomSorts<Apple> sortingAlgorithm) {
|
||||||
|
for (int i = 0; i < 100; i++)
|
||||||
|
apples.add(new Apple(random.nextInt(100)));
|
||||||
|
Apple[] arr = apples.toArray(new Apple[apples.size()]);
|
||||||
|
apples.sort(null);
|
||||||
|
Apple[] sorted = apples.toArray(new Apple[apples.size()]);
|
||||||
|
sortingAlgorithm.sort(arr);
|
||||||
|
assertArrayEquals(sorted, arr);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("Testing Insertion sort functionality")
|
||||||
|
public void testInsertionSort() {
|
||||||
|
sortingAlgorithm = new Insertion<Apple>();
|
||||||
|
testSort(sortingAlgorithm);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("Testing Selection sort functionality")
|
||||||
|
public void testSelectionSort() {
|
||||||
|
sortingAlgorithm = new Selection<Apple>();
|
||||||
|
testSort(sortingAlgorithm);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("Testing Shell sort functionality")
|
||||||
|
public void testShellSort() {
|
||||||
|
sortingAlgorithm = new Shell<>();
|
||||||
|
testSort(sortingAlgorithm);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@AfterEach
|
||||||
|
void tearDown() {
|
||||||
|
sortingAlgorithm = null;
|
||||||
|
apples = null;
|
||||||
|
}
|
||||||
|
}
|
@ -13,4 +13,5 @@ plugins {
|
|||||||
rootProject.name = 'Pricenton1'
|
rootProject.name = 'Pricenton1'
|
||||||
include('module4')
|
include('module4')
|
||||||
include 'clients'
|
include 'clients'
|
||||||
|
include 'module5'
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user