-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathquick sort
More file actions
52 lines (44 loc) · 1.34 KB
/
Copy pathquick sort
File metadata and controls
52 lines (44 loc) · 1.34 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
import java.util.Scanner;
public class Quicksort{
public static void swap(String[] a, int i, int j) {
String temp = a[i];
a[i] = a[j];
a[j] = temp;
}
public static int partition(String[] a, int low, int high) {
String pivot = a[low];
int i = low;
for (int j = low + 1; j <= high; j++) {
if (a[j].compareTo(pivot) < 0) {
i++;
swap(a, i, j);
}
}
swap(a, low, i);
return i;
}
public static void quickSort(String[] a, int low, int high) {
if (low < high) {
int pi = partition(a, low, high);
quickSort(a, low, pi - 1);
quickSort(a, pi + 1, high);
}
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter the limit: ");
int limit = sc.nextInt();
sc.nextLine();
String[] strings = new String[limit];
System.out.println("Enter the list of names:");
for (int i = 0; i < limit; i++) {
strings[i] = sc.nextLine();
}
quickSort(strings, 0, limit - 1);
System.out.println("Sorted names:");
for (int i = 0; i < limit; i++) {
System.out.println(strings[i]);
}
sc.close();
}
}