practice #18
							
								
								
									
										1
									
								
								.idea/gradle.xml
									
									
									
										generated
									
									
									
								
							
							
						
						
									
										1
									
								
								.idea/gradle.xml
									
									
									
										generated
									
									
									
								
							| @ -13,6 +13,7 @@ | ||||
|             <option value="$PROJECT_DIR$/module4" /> | ||||
|             <option value="$PROJECT_DIR$/module5" /> | ||||
|             <option value="$PROJECT_DIR$/module6" /> | ||||
|             <option value="$PROJECT_DIR$/module7" /> | ||||
|           </set> | ||||
|         </option> | ||||
|       </GradleProjectSettings> | ||||
|  | ||||
| @ -15,6 +15,7 @@ dependencies { | ||||
|     implementation project(':module4') | ||||
|     implementation project(':module5') | ||||
|     implementation project(':module6') | ||||
|     implementation project(':module7') | ||||
|     implementation 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,54 @@ | ||||
| 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); | ||||
| 
 | ||||
|     } | ||||
| } | ||||
| @ -0,0 +1,56 @@ | ||||
| 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,4 +16,5 @@ include 'clients' | ||||
| include 'module5' | ||||
| include 'module6' | ||||
| include 'common' | ||||
| include 'module7' | ||||
| 
 | ||||
|  | ||||
		Loading…
	
		Reference in New Issue
	
	Block a user