-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQuick_Sort.java
More file actions
46 lines (44 loc) · 1.13 KB
/
Copy pathQuick_Sort.java
File metadata and controls
46 lines (44 loc) · 1.13 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
//Java program for Quick Sort
import java.util.Scanner;
class Quick_Sort{
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 - 1; j++){
if (A[j].compareToIgnoreCase(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 sc = new Scanner (System.in);
System.out.print("Enter the limit : ");
int n = sc.nextInt();
sc.nextLine();
String A[] = new String[n];
System.out.print("Enter the values : \n");
for (int i = 0; i < n; i++){
A[i] = sc.nextLine ();
}
quickSort (A, 0, n - 1);
System.out.println("\nAfter quick sort");
for (int i = 0; i < n; i++){
System.out.print(A[i]+" ");
}
sc.close();
}
}