implementing quick sort
This commit is contained in:
@@ -4,11 +4,31 @@ public class QuickSorter extends Sorter{
|
||||
|
||||
public QuickSorter(String filename){
|
||||
super("quick", filename);
|
||||
System.out.println("This is stub code, to be removed");
|
||||
}
|
||||
|
||||
void sort(){
|
||||
quickSort(0,numbers.length-1);
|
||||
}
|
||||
|
||||
private void quickSort(int lowIndex, int highIndex){
|
||||
if(lowIndex<highIndex){
|
||||
int pivot = partition(lowIndex, highIndex);
|
||||
quickSort(lowIndex, pivot-1);
|
||||
quickSort(pivot+1, highIndex);
|
||||
}
|
||||
}
|
||||
|
||||
private int partition(int lowIndex, int highIndex){
|
||||
int pivot = numbers[highIndex];
|
||||
int i = lowIndex-1;
|
||||
for(int j=lowIndex;j<highIndex;j++){
|
||||
if(numbers[j]<pivot){
|
||||
i++;
|
||||
swap(i,j);
|
||||
}
|
||||
}
|
||||
swap(i+1, highIndex);
|
||||
return i+1;
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user