initial commit

This commit is contained in:
2020-07-03 16:41:26 -05:00
parent 2513999d29
commit fc30b25022

View File

@@ -0,0 +1,34 @@
package Sorting;
public class WordQuickSorter extends Sorter{
public WordQuickSorter(RandomWordFileReader reader) {
super("quick sort", 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;
}
}