-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQuickSort.java
More file actions
52 lines (46 loc) · 735 Bytes
/
Copy pathQuickSort.java
File metadata and controls
52 lines (46 loc) · 735 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
public class QuickSort {
private int[] input;
public QuickSort(int[] input) {
this.input = input;
}
int partition (int p,int q)
{
int x=input[p]; //pivot
int i=p;
for(int j=i+1;j<=q;j++)
{
if (input[j]<x)
{
i++;
swap(i,j);
}
}
swap(p,i);
return i;
}
void swap(int i,int j)
{
int temp=input[j];
input[j]=input[i];
input[i]=temp;
}
public void sort(int i,int j)
{
if (i<j) //remember this
{
int r=partition(i,j);
sort(i,r-1);
sort(r+1,j);
}
//System.out.println(i);
}
public static void main(String[] args)
{
int[] a={4,1,3,2,7,0};
QuickSort qs=new QuickSort(a);
//System.out.println(qs.input);
qs.sort(0, a.length-1);
for (int i=0;i<a.length;i++)
System.out.println(qs.input[i]);
}
}