-
Notifications
You must be signed in to change notification settings - Fork 0
/
quicksort.java
56 lines (54 loc) · 991 Bytes
/
quicksort.java
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
class sort
{
void qsort(String a[],int first, int last)
{
int i,j;
String pivot,temp;
if(first<last)
{
pivot=a[first];
i=first;
j=last;
while(i<j)
{
while((a[i].compareTo(pivot)<=0)&&i<last)
i++;
while((a[j].compareTo(pivot)>=0)&&j>first)
j--;
if(i<j)
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
temp=a[j];
a[j]=a[first];
a[first]=temp;
this.qsort(a,first,j-1);
this.qsort(a,j+1,last);
}
}
}
class quicksort
{
public static void main(String args[])
{
int n,i;
String s[]=new String[20];
Scanner sc=new Scanner(System.in);
System.out.println("Enter the limit");
n=sc.nextInt();
System.out.println("Enter the elements");
for(i=0;i<=n;i++)
s[i]=sc.nextLine();
System.out.println("BEFORE SORTING");
for(i=1;i<=n;i++)
System.out.println(s[i]);
sort q=new sort();
q.qsort(s,1,n);
System.out.println("\nAFTER SORTING");
for(i=1;i<=n;i++)
System.out.println(s[i]);
}
}