Skip to content

Commit 789d9a1

Browse files
committed
Added READMEs
1 parent 9a1f80b commit 789d9a1

File tree

9 files changed

+19
-2
lines changed

9 files changed

+19
-2
lines changed

assets/ParanoidQuickSort.jpeg

50.5 KB
Loading

assets/QuickSortFirstPivot.png

42.2 KB
Loading

assets/QuickSortRandomPivot.jpeg

61.5 KB
Loading

assets/ThreeWayPartitioning.jpeg

65.5 KB
Loading

src/algorithms/sorting/quickSort/lomuto/QuickSort.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ private static int partition(int[] arr, int start, int end) {
8989
int pIdx = random(start, end);
9090
int pivot = arr[pIdx];
9191

92-
swap(arr, start, pIdx);
92+
swap(arr, start, pIdx); // swap the pivot to the start of the array
9393

9494
int less = start + 1;
9595

@@ -100,7 +100,7 @@ private static int partition(int[] arr, int start, int end) {
100100
}
101101
}
102102

103-
swap(arr, less - 1, start);
103+
swap(arr, less - 1, start); // swap the pivot to its correct position
104104

105105
return less - 1;
106106
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
This is how QuickSort works if we always pick the first element as the pivot.
2+
3+
![QuickSort with first element as pivot](../../../../../assets/QuickSortFirstPivot.png)
4+
5+
Image Source: https://www.geeksforgeeks.org/implement-quicksort-with-first-element-as-pivot/
6+
7+
If we use randomised pivot selection, the idea is very similar to the above implementation. All we
8+
need to do is to swap the random pivot to the first element in the array, then partition as per usual,
9+
then swap the pivot back to its correct position. Below is an illustration:
10+
11+
![Lomuto's QuickSort with random pivot](../../../../../assets/QuickSortRandomPivot.jpeg)
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
![ParanoidQuickSort](../../../../../assets/ParanoidQuickSort.jpeg)

src/algorithms/sorting/quickSort/threeWayPartitioning/QuickSort.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@
1111
* elements equal to the pivot, and elements greater than the pivot. By doing so, we can avoid unnecessary comparisons
1212
* and swaps with duplicate elements, making the sorting process more efficient.
1313
*
14+
* Implementation Invariant:
15+
* The pivot and any element numerically equal to the pivot will be in the correct positions in the array. Elements
16+
* to their left are < them and elements to their right are > than them.
17+
*
1418
* Complexity Analysis:
1519
* Time:
1620
* - Worst case (poor choice of pivot): O(n^2)
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
![ThreeWayPartitioning](../../../../../assets/ThreeWayPartitioning.jpeg)

0 commit comments

Comments
 (0)