forked from Hithomelabs/Princeton1
Mod 7 quicksort
This commit is contained in:
parent
67a7b0e096
commit
96ce281b20
@ -2,10 +2,57 @@ package com.hithomelabs.princeton1.module7;
|
||||
|
||||
import com.hithomelabs.princeton1.module5.AbstractCustomSorts;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
import java.util.Random;
|
||||
|
||||
|
||||
public class Quick<E> extends AbstractCustomSorts<E> {
|
||||
@Override
|
||||
public void sort(E[] arr) {
|
||||
// Collections.shuffle(Arrays.asList(arr));
|
||||
sort(arr, 0, arr.length-1);
|
||||
}
|
||||
|
||||
public void sort(E[] arr, int lo, int hi){
|
||||
if(lo>=hi) return;
|
||||
int pivot = partition(arr, lo, hi);
|
||||
sort(arr, lo, pivot-1);
|
||||
sort(arr, pivot+1,hi);
|
||||
}
|
||||
|
||||
private int partition(E[] arr, int lo, int hi){
|
||||
int pivot=lo, i=lo, j=hi+1;
|
||||
|
||||
while(i<j){
|
||||
while(less((Comparable<E>) arr[++i], arr[pivot]))
|
||||
if(i == hi) break;
|
||||
|
||||
while(less((Comparable<E>) arr[pivot], arr[--j]))
|
||||
if(j == lo) break;
|
||||
|
||||
if(i>=j) break;
|
||||
exch(arr,i, j);
|
||||
}
|
||||
exch(arr,pivot,j);
|
||||
return j;
|
||||
}
|
||||
|
||||
// public static void main(String[] args){
|
||||
// Integer[] arrTest = new Integer[6];
|
||||
//
|
||||
// for(int i=0;i<6;i++){
|
||||
// arrTest[i] = new Random().nextInt(100);
|
||||
// System.out.println(arrTest[i]);
|
||||
// }
|
||||
//
|
||||
// System.out.println();
|
||||
//
|
||||
// Quick<Integer> sortAlgo = new Quick<>();
|
||||
//
|
||||
// sortAlgo.sort(arrTest);
|
||||
//
|
||||
// for(Integer i: arrTest)
|
||||
// System.out.println(i);
|
||||
// }
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user