-
Notifications
You must be signed in to change notification settings - Fork 84
Expand file tree
/
Copy pathQuickSort.java
More file actions
39 lines (32 loc) · 879 Bytes
/
QuickSort.java
File metadata and controls
39 lines (32 loc) · 879 Bytes
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
import java.util.Arrays;
public class QuickSort {
public static void main(String[] args) {
int[] arr = {5,9,1,7,3,4,0,2};
quicksort(arr,0,arr.length-1);
System.out.println(Arrays.toString(arr));
}
static void quicksort(int[] arr,int lb,int ub){
if(lb>=ub) return;
int start = lb;
int end = ub;
int mid = lb + (ub-lb)/2;
int pivot = arr[mid];
while(start<=end){
while(arr[start]<pivot){
start++;
}
while(arr[end]>pivot){
end--;
}
if(start<=end){
int temp = arr[start];
arr[start]=arr[end];
arr[end]=temp;
start++;
end--;
}
}
quicksort(arr,lb,end);
quicksort(arr,start,ub);
}
}