-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmyQuickSort.java
More file actions
57 lines (51 loc) · 1.51 KB
/
Copy pathmyQuickSort.java
File metadata and controls
57 lines (51 loc) · 1.51 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
48
49
50
51
52
53
54
55
56
57
public class myQuickSort{
public static void main(String[] args){
int[] test = {2,8,4,32,5,1,58,9,3,11,6,7,21,13};
for(int i: test){
System.out.print(i+" ");
}
System.out.println("");
myQuickSort(test);
for(int i: test){
System.out.print(i+" ");
}
System.out.println("");
}
// inline quick sort
// S: O(1)
// T: O(nlogn) , worst O(n^2), so we should pick the pivot randomly.
public static void myQuickSort(int[] arr){
myQuickSort(arr, 0, arr.length-1);
}
public static void myQuickSort(int[] arr, int start, int end){
if(start > end)
return;
int index = sort(arr, start,end);
if(start < index-1)// this is the most tricky part.
myQuickSort(arr, start, index-1);
if(index < end)
myQuickSort(arr, index, end);
}
public static int sort(int[] arr, int start, int end){
int pivot = arr[(start+end)/2];
while(start <= end){
while(arr[start] < pivot)
start++;
while(arr[end] > pivot)
end--;
if(start <= end){
swap(arr, start, end);
start++;
end--;
}
}
return start;
}
public static void swap(int[] arr, int a , int b){
if(a != b){
arr[a] = arr[a] + arr[b];
arr[b] = arr[a] - arr[b];
arr[a] = arr[a] - arr[b];
}
}
}