-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQuickSort12.java
More file actions
49 lines (41 loc) · 904 Bytes
/
QuickSort12.java
File metadata and controls
49 lines (41 loc) · 904 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
40
41
42
43
44
45
46
47
48
49
package Arrays;
import java.util.Arrays;
public class QuickSort12 {
public static void main(String[] args) {
int []ar= {30,40,0,5,6,90,1,15,7};
quickSort(ar,0,ar.length-1);
System.out.println(Arrays.toString(ar));
}
public static void quickSort(int []ar,int start,int end) {
if(start<end) {
int index=partition(ar,start,end);
quickSort(ar,start,index-1);
quickSort(ar,index+1,end);
}
}
public static int partition(int []ar,int start,int end)
{
int ref=ar[start];
int i=start,j=end;
while(i<j) {
while(i<=end && ar[i]<=ref) {
i++;
}
while(j>start && ar[i]>=ref) {
j--;
if(i<j) {
int temp=ar[i];
ar[i]=ar[j];
ar[j]=temp;
}
else {
break;
}
int temp=ar[start];
ar[start]=ar[j];
ar[j]=temp;
}
}
return j;
}
}