MCPcopy Create free account
hub / github.com/careercup/ctci / partition

Method partition

java/Chapter 11/Introduction/Quicksort.java:12–34  ·  view source on GitHub ↗
(int arr[], int left, int right)

Source from the content-addressed store, hash-verified

10 }
11
12 public static int partition(int arr[], int left, int right) {
13 int pivot = arr[(left + right) / 2]; // Pick a pivot point. Can be an element.
14
15 while (left <= right) { // Until we've gone through the whole array
16 // Find element on left that should be on right
17 while (arr[left] < pivot) {
18 left++;
19 }
20
21 // Find element on right that should be on left
22 while (arr[right] > pivot) {
23 right--;
24 }
25
26 // Swap elements, and move left and right indices
27 if (left <= right) {
28 swap(arr, left, right);
29 left++;
30 right--;
31 }
32 }
33 return left;
34 }
35
36 public static void quickSort(int arr[], int left, int right) {
37 int index = partition(arr, left, right);

Callers 1

quickSortMethod · 0.95

Calls 1

swapMethod · 0.95

Tested by

no test coverage detected