-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQuickSort.java
More file actions
54 lines (44 loc) · 857 Bytes
/
QuickSort.java
File metadata and controls
54 lines (44 loc) · 857 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
50
51
52
53
54
public class QuickSort {
public static int partition(int[] a,int lo,int hi)
{
int i = lo;
int j = hi+1;
while(true)
{
while(a[++i]<a[lo])
{
if(i>=hi) break;
}
while(a[--j]>a[lo])
{
if(j<=lo) break;
}
if (i>=j) {
break;
}
swap(a,i,j);
}
swap(a,lo,j);
return j;
}
public static void sort(int[] a,int lo,int hi) {
if(lo>=hi) return;
int p = partition(a, lo, hi);
sort(a, lo, p-1);
sort(a, p+1, hi);
}
public static void swap(int[] arr,int i,int j)
{
int temp = arr[i];
arr[i]=arr[j];
arr[j]=temp;
}
public static void main(String[] args) {
// TODO Auto-generated method stub
int[] a ={5,8,4,6,12,80,9,9};
sort(a, 0, a.length-1);
for (int i = 0; i < a.length; i++) {
System.out.println(a[i]);
}
}
}