Skip to content

Commit

Permalink
Missing file.
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewcmyers committed Nov 1, 2024
1 parent f05ee17 commit 36611e7
Showing 1 changed file with 18 additions and 0 deletions.
18 changes: 18 additions & 0 deletions lectures/sorting/partition.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/** Partition array {@code a} into {@code a[l..k)} and {@code a[k..r)},
* where {@code l < k < r}, and all elements in {@code a[l..k)} are
* less than or equal to all elements in {@code a[k..r)}.
* Requires: {@code 0≤l}, {@code r≤a.length}, and {@code r-l≥2}.
*/
int partition(int[] a, int l, int r) {
swap(a, l, l + random.nextInt(r-l));
int p = a[l];
int i = l, j = r - 1;
while (a[j] > p) j--;
while (i < j) {
swap(a, i, j); // swap a[i] and a[j]
do i++; while (a[i] < p);
do j--; while (a[j] > p);
}
return j+1;
}

0 comments on commit 36611e7

Please sign in to comment.