Class to implment three way partition quick sort
This commit is contained in:
39
Sorting/QuickSorterThreeWay.java
Normal file
39
Sorting/QuickSorterThreeWay.java
Normal 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;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user