From 74555ecf20e15f68a98fd4f86fb3db6465b84eca Mon Sep 17 00:00:00 2001 From: bruch-alex Date: Wed, 16 Oct 2024 23:53:27 +0200 Subject: [PATCH 1/2] fix highlighting bug when using quickSort --- src/main/java/algorithms/QuickSort.java | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/src/main/java/algorithms/QuickSort.java b/src/main/java/algorithms/QuickSort.java index 3626524..5939858 100644 --- a/src/main/java/algorithms/QuickSort.java +++ b/src/main/java/algorithms/QuickSort.java @@ -49,14 +49,12 @@ 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 sortedIndices.add(pIndex); + sortedIndices.add(start); + sortedIndices.add(end); return pIndex; } From 5c38e5f772b9d18b0c302834d9fb4f84f1386658 Mon Sep 17 00:00:00 2001 From: bruch-alex Date: Thu, 17 Oct 2024 00:02:13 +0200 Subject: [PATCH 2/2] delete comments --- src/main/java/algorithms/QuickSort.java | 15 ++------------- 1 file changed, 2 insertions(+), 13 deletions(-) diff --git a/src/main/java/algorithms/QuickSort.java b/src/main/java/algorithms/QuickSort.java index 5939858..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,9 +40,7 @@ int partition(int start, int end, Set sortedIndices, int sleepDuration) } } - // 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);