-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQuickSort.java
More file actions
63 lines (53 loc) · 913 Bytes
/
QuickSort.java
File metadata and controls
63 lines (53 loc) · 913 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
55
56
57
58
59
60
61
62
63
// This program also follows Divide and Conquer strategy like MergeSort.java
class QuickSort
{
void partition(int l, int r, int a[])
{
int pivot = a[0];
int i=l;
int j=r;
while(i<j)
{
do
{i++;} while(pivot>=a[i])
do
{j--;} while(a[j]>pivot)
if(i<j)
{
int temp = a[i];
a[i]=a[j];
a[j]=temp;
}
}
int temp=a[l];
a[l]=a[j];
a[j]=temp;
return j;
}
}
void sort(int l, int r, int a[])
{
if(l<r)
{
int pi = partition(l,r,a);
sort(l,pi-1,a);
sort(pi+1,l,a);
}
}
static void printArray(int a[])
{
int n=a.length;
for(int i=0;i<n;i++)
System.out.print(a[i]+ " ");
System.out.println();
}
public static void main(String[] args)
{
int a[]={ 10,5,7,11,8,2};
int n = a.length;
QuickSort ob = new QuickSort();
sort(0,n-1,a);
System.out.println("Sorted Array:");
printArray(a);
}
}