forked from MichaelVandi/CodingInterview
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path08_11_2020_quick_sort.java
More file actions
47 lines (43 loc) · 1.56 KB
/
08_11_2020_quick_sort.java
File metadata and controls
47 lines (43 loc) · 1.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
/*
Time:
Best = O(n log(n)), Average = O(n log(n)), Worst = O(n^2)
Space: O(1)
*/
class Solution {
public int[] sortArray(int[] nums) {
applyQuickSort(nums, 0, nums.length - 1);
return nums;
}
public void applyQuickSort(int[] array, int start, int end) {
if (start >= end) return;
int pointer = start;
int left = start + 1;
int right = end;
while (left <= right) {
// Left and right pointers are not in place so we swap them
if (array[left] > array[pointer] && array[right] < array[pointer]) {
swap(array, left, right);
}
// Left and right pointers are both in place
if (array[left] <= array[pointer]) left++;
if (array[right] >= array[pointer]) right--;
}
swap(array, right, pointer);
// Determine which sub array is smaller and apply quick sort to the smaller one first
boolean leftIsSmaller = (right - 1) - start < end - (right + 1);
if (leftIsSmaller) {
// Apply quick sort on the left sub array first
applyQuickSort(array, start, right - 1);
applyQuickSort(array, right + 1, end);
} else {
// Apply quick sort on the right sub array first
applyQuickSort(array, right + 1, end);
applyQuickSort(array, start, right - 1);
}
}
public void swap(int[] array, int i, int j) {
int temp = array[i];
array[i] = array[j];
array[j] = temp;
}
}