-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathQuickSort.java
64 lines (50 loc) · 2.39 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
52
53
54
55
56
57
58
59
60
61
62
63
64
package phonebook;
import java.util.ArrayList;
/*
* Sort data using Quick Sort.
* Quick Sort ->
* The quicksort technique is done by separating the list into two parts.
* Initially, a pivot element is chosen by partitioning algorithm.
* The left part of the pivot holds the smaller values than the pivot, and right part holds the larger value.
* After partitioning, each separate lists are partitioned using the same procedure.
*/
public class QuickSort {
public static ArrayList<String> quickSortNameData = new ArrayList<>(Main.nameData);
public static ArrayList<Integer> quickSortNumberData = new ArrayList<>(Main.numberData);
public static void sort() {
quickSort(quickSortNameData, quickSortNumberData, 0, quickSortNameData.size() - 1);
}
private static void quickSort(ArrayList<String> quickSortNameData, ArrayList<Integer> quickSortNumberData, int start, int end) {
if (start < end) {
int partitionIndex = partition(quickSortNameData, quickSortNumberData, start, end);
quickSort(quickSortNameData, quickSortNumberData, start, partitionIndex - 1);
quickSort(quickSortNameData, quickSortNumberData, partitionIndex + 1, end);
}
}
private static int partition(ArrayList<String> quickSortNameData, ArrayList<Integer> quickSortNumberData, int start, int end) {
String pivot = quickSortNameData.get(end);
int i = start - 1;
for (int j = start; j < end; j++) {
if (quickSortNameData.get(j).compareTo(pivot) < 0) {
i++;
// Swap name.
String name = quickSortNameData.get(i);
quickSortNameData.set(i, quickSortNameData.get(j));
quickSortNameData.set(j, name);
// Swap number
int number = quickSortNumberData.get(i);
quickSortNumberData.set(i, quickSortNumberData.get(j));
quickSortNumberData.set(j, number);
}
}
// Swap name.
String name = quickSortNameData.get(i + 1);
quickSortNameData.set(i + 1, quickSortNameData.get(end));
quickSortNameData.set(end, name);
// Swap number
int number = quickSortNumberData.get(i + 1);
quickSortNumberData.set(i + 1, quickSortNumberData.get(end));
quickSortNumberData.set(end, number);
return i+1;
}
}