38 lines
955 B
Java
38 lines
955 B
Java
package Sorting;
|
|
|
|
public class WordQuickSorter extends Sorter{
|
|
|
|
public WordQuickSorter(RandomWordFileReader reader) {
|
|
super("quick sort", reader);
|
|
}
|
|
|
|
public WordQuickSorter(String type, RandomWordFileReader reader) {
|
|
super(type, reader);
|
|
}
|
|
|
|
void sort() {
|
|
quickSort(0, words.length-1);
|
|
}
|
|
|
|
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 i = lowIndex -1;
|
|
String pivot = words[highIndex];
|
|
for(int j = lowIndex; j < highIndex; j++) {
|
|
if(compare(words[j], pivot) < 0) {
|
|
i++;
|
|
swap(words, i, j);
|
|
}
|
|
}
|
|
swap(words, i+1, highIndex);
|
|
return i+1;
|
|
}
|
|
|
|
} |