diff --git a/Sorting/WordQuickSorter.java b/Sorting/WordQuickSorter.java new file mode 100644 index 0000000..92770f3 --- /dev/null +++ b/Sorting/WordQuickSorter.java @@ -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; + } + +} \ No newline at end of file