-
Notifications
You must be signed in to change notification settings - Fork 4
/
QuickSortNames.java
51 lines (47 loc) · 1.18 KB
/
QuickSortNames.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
import java.util.Scanner;
class QuickSortNames {
int partition(String str[], int low, int high) {
String pivot = str[high];
int i = (low - 1);
for (int j = low; j < high; j++) {
if (str[j].compareTo(pivot) <= 0) {
i++;
String temp = str[i];
str[i] = str[j];
str[j] = temp;
}
}
String temp = str[i + 1];
str[i + 1] = str[high];
str[high] = temp;
return i + 1;
}
void sort(String str[], int low, int high) {
if (low < high) {
int pi = partition(str, low, high);
sort(str, low, pi - 1);
sort(str, pi + 1, high);
}
}
static void printArray(String str[], int n) {
for (int i = 0; i < n; ++i)
System.out.print(str[i] + " ");
System.out.println();
}
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
String str[] = new String[30];
int n, i;
System.out.println("Enter the number of names: ");
n = in.nextInt();
n = n + 1;
System.out.println("Enter the names: ");
for (i = 0; i < n; i++) {
str[i] = in.nextLine();
}
QuickSortNames ob = new QuickSortNames();
ob.sort(str, 0, n - 1);
System.out.println("Sorted array");
printArray(str, n);
}
}