-
Notifications
You must be signed in to change notification settings - Fork 0
/
QuickSort.java
51 lines (43 loc) · 1.32 KB
/
QuickSort.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
public class QuickSort{
void sort(String arr[], int low, int high){
if (low < high) {
int pi = partition(arr, low, high);
sort(arr, low, pi - 1);
sort(arr, pi + 1, high);
}
}
int partition(String arr[], int low, int high){
String pivot = arr[low];
int i = high + 1;
for (int j = high; j > low; j--) {
if (arr[j].compareToIgnoreCase(pivot) >= 0){
i--;
swap(arr, i, j);
}
}
swap(arr, i - 1, low);
return i - 1;
}
void swap(String arr[], int i, int j){
String temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
static void printArray(String arr[]){
for (String str : arr)
System.out.print(str + " ");
System.out.println();
}
public static void main(String args[]){
String arr[] = {"John", "Alice", "Bob", "David", "Eva"};
int n = arr.length;
System.out.println("Before sorting:");
for(int i=0;i<n;i++)
System.out.print(arr[i]+" ");
System.out.println();
QuickSort ob = new QuickSort();
ob.sort(arr, 0, n - 1);
System.out.println("Sorted array:");
printArray(arr);
}
}