forked from Hithomelabs/Princeton1
Compare commits
No commits in common. "0e4275c1a8264a0acee2849727bb767df98d5f51" and "c710f4de7a9263023631e65d43e3e93ba240f43f" have entirely different histories.
0e4275c1a8
...
c710f4de7a
1
.idea/gradle.xml
generated
1
.idea/gradle.xml
generated
@ -13,7 +13,6 @@
|
|||||||
<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,7 +15,6 @@ 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')
|
||||||
}
|
}
|
||||||
|
@ -1,22 +0,0 @@
|
|||||||
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]);
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,21 +0,0 @@
|
|||||||
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()
|
|
||||||
}
|
|
@ -1,54 +0,0 @@
|
|||||||
package com.hithomelabs.princeton1.module7;
|
|
||||||
|
|
||||||
import com.hithomelabs.princeton1.module5.AbstractCustomSorts;
|
|
||||||
|
|
||||||
|
|
||||||
public class Quick<E> extends AbstractCustomSorts<E> {
|
|
||||||
@Override
|
|
||||||
public void sort(E[] arr) {
|
|
||||||
int N = arr.length;
|
|
||||||
quickSort(arr, 0, N - 1);
|
|
||||||
}
|
|
||||||
|
|
||||||
public void altSort(E[] arr) {
|
|
||||||
int N = arr.length;
|
|
||||||
altQuickSort(arr, 0, N-1);
|
|
||||||
}
|
|
||||||
|
|
||||||
private void altQuickSort(E[] arr, int lo, int hi) {
|
|
||||||
if (lo >= hi) return;
|
|
||||||
int i = lo + 1;
|
|
||||||
int j = i;
|
|
||||||
while(j <= hi){
|
|
||||||
if(less((Comparable<E>) arr[j], arr[lo])){
|
|
||||||
exch(arr, i, j);
|
|
||||||
i++;
|
|
||||||
}
|
|
||||||
j++;
|
|
||||||
}
|
|
||||||
exch(arr, i-1, lo);
|
|
||||||
altQuickSort(arr, lo, i-2);
|
|
||||||
altQuickSort(arr, i, hi);
|
|
||||||
}
|
|
||||||
|
|
||||||
private void quickSort(E[] arr, int lo, int hi) {
|
|
||||||
|
|
||||||
if (lo >= hi) return;
|
|
||||||
int i = lo;
|
|
||||||
int j = hi+1;
|
|
||||||
while(true){
|
|
||||||
while(less((Comparable<E>) arr[++i], arr[lo])){
|
|
||||||
if(i == hi) break;
|
|
||||||
}
|
|
||||||
while(!less((Comparable<E>) arr[--j], arr[lo])){
|
|
||||||
if (j == lo ) break;
|
|
||||||
}
|
|
||||||
if(j<=i) break;
|
|
||||||
exch(arr, i , j);
|
|
||||||
}
|
|
||||||
exch(arr, j, lo);
|
|
||||||
quickSort(arr, lo, j-1);
|
|
||||||
quickSort(arr, j+1, hi);
|
|
||||||
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,56 +0,0 @@
|
|||||||
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);
|
|
||||||
}
|
|
||||||
|
|
||||||
@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,5 +16,4 @@ include 'clients'
|
|||||||
include 'module5'
|
include 'module5'
|
||||||
include 'module6'
|
include 'module6'
|
||||||
include 'common'
|
include 'common'
|
||||||
include 'module7'
|
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user