-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathqsort.java
More file actions
51 lines (43 loc) · 1008 Bytes
/
qsort.java
File metadata and controls
51 lines (43 loc) · 1008 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
import java.util.*;
import java.io.*;
public class qsort {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc=new Scanner(System.in);
System.out.println("Enter the number");
int l=sc.nextInt();
sc.nextLine();
String s[]=new String[l];
System.out.println("Enter the strings");
for(int i=0;i<l;i++)
s[i]=sc.nextLine();
quicksort(s,0,l-1);
for(int i=0;i<l;i++)
System.out.println(s[i]);
}
static void swap(String s[],int i,int j)
{
String temp=s[i];
s[i]=s[j];
s[j]=temp;
}
static void quicksort(String[] s, int left, int right) {
// TODO Auto-generated method stub
int i,j;String pivot;
if(left<right)
{ do {
i=left;
j=right+1;
pivot=s[left];
do {i++;if(i>right)break;}while(s[i].compareTo(pivot)<0);
do {j--;if(j<=left)break;}while(s[j].compareTo(pivot)>0);
if(i<j)
swap(s,i,j);
}
while(i<j);
swap(s,left,j);
quicksort(s,left,j-1);
quicksort(s,j+1,right);
}
}
}