forked from Hithomelabs/Princeton1
Compare commits
4 Commits
b8da0cb3b1
...
cc3fc5df3d
Author | SHA1 | Date | |
---|---|---|---|
cc3fc5df3d | |||
a409bce387 | |||
b8f6081173 | |||
6a752c9dc2 |
1
.idea/gradle.xml
generated
1
.idea/gradle.xml
generated
@ -13,6 +13,7 @@
|
|||||||
<option value="$PROJECT_DIR$/module4" />
|
<option value="$PROJECT_DIR$/module4" />
|
||||||
<option value="$PROJECT_DIR$/module5" />
|
<option value="$PROJECT_DIR$/module5" />
|
||||||
<option value="$PROJECT_DIR$/module6" />
|
<option value="$PROJECT_DIR$/module6" />
|
||||||
|
<option value="$PROJECT_DIR$/module7" />
|
||||||
</set>
|
</set>
|
||||||
</option>
|
</option>
|
||||||
</GradleProjectSettings>
|
</GradleProjectSettings>
|
||||||
|
@ -15,6 +15,7 @@ dependencies {
|
|||||||
implementation project(':module4')
|
implementation project(':module4')
|
||||||
implementation project(':module5')
|
implementation project(':module5')
|
||||||
implementation project(':module6')
|
implementation project(':module6')
|
||||||
|
implementation project(':module7')
|
||||||
implementation project(':common')
|
implementation project(':common')
|
||||||
testImplementation project(':common')
|
testImplementation project(':common')
|
||||||
}
|
}
|
||||||
|
@ -0,0 +1,22 @@
|
|||||||
|
package com.hithomelabs.clients.module7;
|
||||||
|
|
||||||
|
import com.hithomelabs.princeton1.common.Apple;
|
||||||
|
import com.hithomelabs.princeton1.module7.Quick;
|
||||||
|
|
||||||
|
import java.util.Random;
|
||||||
|
|
||||||
|
public class QuickSortClient {
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
int size = 100;
|
||||||
|
Apple[] apples = new Apple[size];
|
||||||
|
Quick<Apple> quick = new Quick<Apple>();
|
||||||
|
|
||||||
|
for (int i = 0; i < apples.length; i++) {
|
||||||
|
apples[i] = new Apple(new Random().nextInt(1000));
|
||||||
|
}
|
||||||
|
quick.sort(apples);
|
||||||
|
for (int i = 0; i < apples.length; i++)
|
||||||
|
System.out.println(apples[i]);
|
||||||
|
}
|
||||||
|
}
|
21
module7/build.gradle
Normal file
21
module7/build.gradle
Normal file
@ -0,0 +1,21 @@
|
|||||||
|
plugins {
|
||||||
|
id 'java'
|
||||||
|
}
|
||||||
|
|
||||||
|
group = 'com.hithomelabs.princeton1.module7'
|
||||||
|
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,56 @@
|
|||||||
|
package com.hithomelabs.princeton1.module7;
|
||||||
|
|
||||||
|
import com.hithomelabs.princeton1.module5.AbstractCustomSorts;
|
||||||
|
|
||||||
|
|
||||||
|
public class Quick<E> extends AbstractCustomSorts<E> {
|
||||||
|
@Override
|
||||||
|
public void sort(E[] arr) {
|
||||||
|
randomShuffle(arr);
|
||||||
|
partition(arr, 0, arr.length-1);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void randomShuffle(E[] arr){
|
||||||
|
int array_len = arr.length;
|
||||||
|
for(int i=0; i< array_len; i++){
|
||||||
|
int random_index = (int)(Math.random()*array_len);
|
||||||
|
exch(arr, i, random_index);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void partition(E[] arr, int low, int high){
|
||||||
|
if(low >= high)
|
||||||
|
return;
|
||||||
|
int mid = sort(arr, low, high);
|
||||||
|
partition(arr, low, mid-1);
|
||||||
|
partition(arr, mid + 1, high);
|
||||||
|
}
|
||||||
|
|
||||||
|
private int sort(E[] arr, int low, int high){
|
||||||
|
int i = low+1;
|
||||||
|
int j = high;
|
||||||
|
while (true){
|
||||||
|
// Find the i index greater than 1st element
|
||||||
|
while (i<=high && !less((Comparable<E>) arr[low], arr[i]))
|
||||||
|
i++;
|
||||||
|
// Find the j index less than 1st element
|
||||||
|
while (!less((Comparable<E>) arr[j], arr[low]) && j > low)
|
||||||
|
j--;
|
||||||
|
// Break if indexes are crossed
|
||||||
|
if(j <= i)
|
||||||
|
break;
|
||||||
|
// Swap index values of i & j
|
||||||
|
if(less((Comparable<E>) arr[j], arr[i]))
|
||||||
|
exch(arr, i++, j--);
|
||||||
|
}
|
||||||
|
// Swap 1st element to it's correct position
|
||||||
|
int k_index = low+1;
|
||||||
|
while (k_index <= high){
|
||||||
|
if(less((Comparable<E>) arr[low], arr[k_index]))
|
||||||
|
break;
|
||||||
|
k_index++;
|
||||||
|
}
|
||||||
|
exch(arr, low, k_index-1);
|
||||||
|
return k_index - 1;
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,60 @@
|
|||||||
|
package com.hithomelabs.princeton1.module7;
|
||||||
|
|
||||||
|
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 QuickTest {
|
||||||
|
|
||||||
|
private Quick<Apple> quick;
|
||||||
|
private ArrayList<Apple> apples;
|
||||||
|
private Random random;
|
||||||
|
|
||||||
|
@BeforeEach
|
||||||
|
void setUp() {
|
||||||
|
quick = new Quick<Apple>();
|
||||||
|
apples = new ArrayList<Apple>();
|
||||||
|
random = new Random();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("testing Quick sort default implementation")
|
||||||
|
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()]);
|
||||||
|
quick.sort(arr);
|
||||||
|
apples.sort(null);
|
||||||
|
Apple[] sorted = apples.toArray(new Apple[apples.size()]);
|
||||||
|
Assertions.assertArrayEquals(sorted, arr);
|
||||||
|
}
|
||||||
|
|
||||||
|
// * * Optional test for alternate sort implmentation
|
||||||
|
/*
|
||||||
|
@Test
|
||||||
|
@DisplayName("testing Quick sort default implementation")
|
||||||
|
public void testAltSort(){
|
||||||
|
|
||||||
|
for(int i = 0; i < 100; i++)
|
||||||
|
apples.add(new Apple(random.nextInt(1000)));
|
||||||
|
Apple[] arr = apples.toArray(new Apple[apples.size()]);
|
||||||
|
quick.altSort(arr);
|
||||||
|
apples.sort(null);
|
||||||
|
Apple[] sorted = apples.toArray(new Apple[apples.size()]);
|
||||||
|
Assertions.assertArrayEquals(sorted, arr);
|
||||||
|
}
|
||||||
|
*/
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
@AfterEach
|
||||||
|
void tearDown() {
|
||||||
|
quick = null;
|
||||||
|
apples = null;
|
||||||
|
random = null;
|
||||||
|
}
|
||||||
|
}
|
@ -16,4 +16,5 @@ include 'clients'
|
|||||||
include 'module5'
|
include 'module5'
|
||||||
include 'module6'
|
include 'module6'
|
||||||
include 'common'
|
include 'common'
|
||||||
|
include 'module7'
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user