Skip to content

Commit 301c3db

Browse files
authored
Merge pull request #122 from py3-coder/main
Create Quicksort.java
2 parents dcee508 + 7536de7 commit 301c3db

File tree

1 file changed

+62
-0
lines changed

1 file changed

+62
-0
lines changed

Quicksort.java

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
class QuickSort
2+
{
3+
public static void main(String[]args)
4+
{
5+
int a[]={10,75,-15,17,0,-2};
6+
QSort(a,0,a.length-1);
7+
System.out.println("Sorted Array : ");
8+
System.out.print("[");
9+
for(int i=0;i<a.length;i++)
10+
{
11+
if(i == a.length-1)
12+
System.out.print(a[i]);
13+
else
14+
System.out.print(a[i] + ", ");
15+
}
16+
System.out.println("]");
17+
}
18+
19+
public static void QSort(int a[],int l,int h)
20+
{
21+
if(l<h)
22+
{
23+
int loc=Partion(a,l,h);
24+
QSort(a,l,loc-1);
25+
QSort(a,loc+1,h);
26+
}
27+
}
28+
29+
public static int Partion(int a[],int l,int h)
30+
{
31+
int pivot = a[l];
32+
int start=l;
33+
int end=h;int tmp;
34+
35+
while(start < end)
36+
{
37+
while(start<=h && a[start]<=pivot)
38+
{
39+
start++;
40+
}
41+
while(a[end]>pivot)
42+
{
43+
end--;
44+
}
45+
if(start<end)
46+
{
47+
tmp=a[start];
48+
a[start]=a[end];
49+
a[end]=tmp;
50+
51+
52+
}
53+
54+
}
55+
56+
57+
tmp=a[l];
58+
a[l]=a[end];
59+
a[end]=tmp;
60+
return end;
61+
}
62+
}

0 commit comments

Comments
 (0)