Class to implment three way partition quick sort

This commit is contained in:
2020-06-05 19:01:27 -05:00
parent 52095c928d
commit ad3bc3a431

View File

@@ -0,0 +1,39 @@
package Sorting;
/**
* an implementation of quick sort with three-way partitioning
*/
public class QuickSorterThreeWay extends QuickSorter{
public QuickSorterThreeWay(String filename){
super("3-way partitioned quick", filename);
}
//edit this to use three-way partition
@Override
private void quickSort(int lowIndex, int highIndex){
if(compare(lowIndex,highIndex)<0){
int pivot = partition(lowIndex, highIndex);
quickSort(lowIndex, pivot-1);
quickSort(pivot+1, highIndex);
}
}
//edit this to use three way partition
@Override
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;
}
}