Files
Tutoring-APCompSci/Sorting/QuickSorterThreeWay.java
2020-06-08 12:35:06 -05:00

44 lines
1.1 KiB
Java

package Sorting;
/**
* an implementation of quick sort with three-way partitioning
*/
public class QuickSorterThreeWay extends Sorter{
public QuickSorterThreeWay(String filename){
super("quick sort (3-way partition)", filename);
}
void sort(){
quickSort(0,numbers.length-1);
}
void quickSort(int lowIndex, int highIndex){
if(compare(lowIndex,highIndex)<0){
int pivot=numbers[highIndex];
int lessThan = lowIndex;
int greaterThan = highIndex;
int i = greaterThan-1;
while(i>=lessThan){
int comp = compare(numbers[i], pivot);
if(comp>0){
swap(i, greaterThan);
i--;
greaterThan--;
}
else if(comp<0){
swap(i, lessThan);
lessThan++;
}
else{
i--;
}
}
quickSort(lowIndex, lessThan-1);
quickSort(greaterThan+1, highIndex);
}
}
}