-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQuickSort.java
More file actions
68 lines (52 loc) · 1.86 KB
/
QuickSort.java
File metadata and controls
68 lines (52 loc) · 1.86 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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
/**
* Created by Phil on 8/15/2015.
*/
public class QuickSort {
public QuickSort() {}
public int[] sort(int[] collection) {
System.out.println("Pre-sorted: ");
for(int i = 0; i < collection.length; i++) {
System.out.print(collection[i] + " ");
}
System.out.println();
quickSort(collection, 0, collection.length - 1);
System.out.println("Post-sorted: ");
for(int i = 0; i < collection.length; i++) {
System.out.print(collection[i] + " ");
}
return collection;
}
private void quickSort(int[] collection, int leftIndex, int rightIndex) {
if(leftIndex >= rightIndex) return;
int pivotIndex = pivot(collection, leftIndex, rightIndex);
quickSort(collection, leftIndex, pivotIndex-1);
quickSort(collection, pivotIndex+1, rightIndex);
}
private int pivot(int[] collection, int leftIndex, int rightIndex) {
int i = leftIndex+1;
int j = rightIndex;
int midIndex = leftIndex + (rightIndex - leftIndex)/2;
int pivotValue = collection[midIndex];
swap(collection, midIndex, leftIndex);
while(i < j) {
while(collection[i] < pivotValue && i <= rightIndex)
i++;
while(collection[j] > pivotValue && j >= leftIndex)
j--;
if(i < j)
swap(collection, i, j);
}
swap(collection, leftIndex, j);
return j;
}
private void swap(int[] collection, int indexA, int indexB) {
int swapSpace = collection[indexA];
collection[indexA] = collection[indexB];
collection[indexB] = swapSpace;
}
public static void main(String[] args) {
QuickSort qs = new QuickSort();
int[] unsorted = {6,8,2,3,7,1,9,10,43,2,5,25,43,21,75};
qs.sort(unsorted);
}
}