-
Notifications
You must be signed in to change notification settings - Fork 0
/
QuickSort.java
36 lines (34 loc) · 975 Bytes
/
QuickSort.java
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
import java.util.*;
public class QuickSort {
public static void main(String[] args) {
int [] arr = {9,8,7,6,5,4,3,2,1,0};
sort(arr, 0, arr.length-1);
System.out.println(Arrays.toString(arr));
Arrays.sort(arr);
}
static void sort(int[] arr,int left,int right ){
if (left >= right)
return;
int s = left;
int e = right;
int m = e - (e - s)/2;
int pivot = arr[m];
while (s <= e){
while (arr[s] < pivot){
s++;
}
while (arr[e] > pivot){
e--;
}
if(s<=e){
int temp = arr[s];
arr[s] = arr[e];
arr[e] = temp;
s++;
e--;
}
sort(arr, left, e);
sort(arr, s, left);
}
}
}