-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy path_17.QuickSort.java
More file actions
53 lines (51 loc) · 1.29 KB
/
_17.QuickSort.java
File metadata and controls
53 lines (51 loc) · 1.29 KB
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
import java.util.*;
public class quicksort
{
public static void quicksort(String A[],int p,int r)
{
if(p<r)
{
int q = partition(A,p,r);
quicksort(A,p,q-1);
quicksort(A,q+1,r);
}
}
public static int partition(String A[],int p,int r)
{
String x = A[r];
int i = p-1;
for(int j=p;j<r;j++)
{
if(A[j].compareTo(x) <= 0)
{
i = i+1;
String temp = A[i];
A[i] = A[j];
A[j] = temp;
}
}
String temp = A[i+1];
A[i+1] = A[r];
A[r] = temp;
return i+1;
}
public static void main(String args[])
{
Scanner scan = new Scanner(System.in);
System.out.print("Enter the number of words : ");
int n = scan.nextInt();
scan.nextLine();
String A[] = new String[n];
System.out.println("Enter the names : ");
for(int i=0;i<n;i++)
{
A[i] = scan.nextLine();
}
quicksort(A,0,n-1);
System.out.println("\n__After Quick Sort__\n");
for(int i=0;i<n;i++)
{
System.out.println(A[i]);
}
}
}