rename to distinguish btw number and word sorters
This commit is contained in:
38
Sorting/NumberQuickSorter.java
Normal file
38
Sorting/NumberQuickSorter.java
Normal file
@@ -0,0 +1,38 @@
|
||||
package Sorting;
|
||||
|
||||
public class NumberQuickSorter extends NumberSorter{
|
||||
|
||||
public NumberQuickSorter(RandomNumberFileReader reader){
|
||||
super("quick sort", reader);
|
||||
}
|
||||
|
||||
public NumberQuickSorter(String qsortVariant, RandomNumberFileReader reader){
|
||||
super(qsortVariant, reader);
|
||||
}
|
||||
|
||||
void sort(){
|
||||
quickSort(0,numbers.length-1);
|
||||
}
|
||||
|
||||
void quickSort(int lowIndex, int highIndex){
|
||||
if(compare(lowIndex,highIndex)<0){
|
||||
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(compare(numbers[j],pivot)<0){
|
||||
i++;
|
||||
swap(i,j);
|
||||
}
|
||||
}
|
||||
swap(i+1, highIndex);
|
||||
return i+1;
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user