initial commit
This commit is contained in:
34
Sorting/WordQuickSorter.java
Normal file
34
Sorting/WordQuickSorter.java
Normal 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;
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user