-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathquicksort.java
More file actions
48 lines (44 loc) · 821 Bytes
/
Copy pathquicksort.java
File metadata and controls
48 lines (44 loc) · 821 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
package datastructures;
public class quicksort {
public static void main(String[] args) {
// TODO Auto-generated method stub
int a[]= {40,60,80,1,2};
for(int i=0;i<5;i++)
System.out.print(a[i]+" ");
sort(a,0,4);
System.out.println();
for(int i=0;i<5;i++)
System.out.print(a[i]+" ");
}
static void sort(int a[],int l,int h)
{
if(l<h)
{
int p=partition(a,l,h);
sort(a,l,p-1);
sort(a,p+1,h);
}
}
static int partition(int a[],int l,int h)
{
int p=a[h];
int i=l-1;
for(int j=l;j<=h;j++)
{
if(a[j]<p)
{
i++;
swap(a,i,j);
}
}
swap(a,i+1,h);
return i+1;
}
static void swap(int a[],int i,int j)
{
int temp;
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}