Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 4 additions & 17 deletions src/main/java/algorithms/QuickSort.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,31 +14,22 @@ public QuickSort(ArrayList<Integer> array) {

@Override
public void sort() {
int SLEEP_DURATION = 30;

int SLEEP_DURATION = 25;
Set<Integer> sortedIndices = new HashSet<>();
quickSort(0, array.size() - 1, sortedIndices, SLEEP_DURATION);
Utils.displayVerticalArray(array, -1, -1, sortedIndices);
}

void quickSort(int start, int end, Set<Integer> 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<Integer> 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++) {
Expand All @@ -49,14 +40,10 @@ int partition(int start, int end, Set<Integer> 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;
}

Expand Down