diff --git a/src/main/java/algorithms/QuickSort.java b/src/main/java/algorithms/QuickSort.java index 3626524..89727e1 100644 --- a/src/main/java/algorithms/QuickSort.java +++ b/src/main/java/algorithms/QuickSort.java @@ -14,8 +14,7 @@ public QuickSort(ArrayList array) { @Override public void sort() { - int SLEEP_DURATION = 30; - + int SLEEP_DURATION = 25; Set sortedIndices = new HashSet<>(); quickSort(0, array.size() - 1, sortedIndices, SLEEP_DURATION); Utils.displayVerticalArray(array, -1, -1, sortedIndices); @@ -23,22 +22,14 @@ public void sort() { void quickSort(int start, int end, Set sortedIndices, int sleepDuration) { if (start < end) { - // pi is the partition return index of pivot int pi = partition(start, end, sortedIndices, sleepDuration); - - // Recursion calls for smaller elements - // and greater or equals elements quickSort(start, pi - 1, sortedIndices, sleepDuration); quickSort(pi + 1, end, sortedIndices, sleepDuration); } } - // Partition function int partition(int start, int end, Set sortedIndices, int sleepDuration) { int pivot = array.get(end); - - // Index of smaller element and indicates - // the right position of pivot found so far int pIndex = start; for (int i = start; i <= end - 1; i++) { @@ -49,14 +40,10 @@ int partition(int start, int end, Set sortedIndices, int sleepDuration) } } - // Traverse arr[start..end] and move all smaller - // elements to the left side. Elements from start to - // i are smaller after every iteration - - // Move pivot after smaller elements and - // return its position - Utils.swapHighlighted(array, pIndex, end, sortedIndices, sleepDuration); // Move pivot to correct position + Utils.swapHighlighted(array, pIndex, end, sortedIndices, sleepDuration); sortedIndices.add(pIndex); + sortedIndices.add(start); + sortedIndices.add(end); return pIndex; }