-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQuickSort.java
More file actions
65 lines (51 loc) · 1.64 KB
/
QuickSort.java
File metadata and controls
65 lines (51 loc) · 1.64 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
/**
Author: Rajin Santos Gajadhar
Student ID: 239479650
Final Exam: Question 8
Any and all work in this file is my own.
*/
public class QuickSort {
public static void quickSort(int[] arr, int begin, int end) {
if (begin < end) {
int partitionIndex = partition(arr, begin, end);
// Recursively sort elements
quickSort(arr, begin, partitionIndex-1);
quickSort(arr, partitionIndex+1, end);
}
}
private static int partition(int[] arr, int begin, int end) {
int pivot = arr[end];
int i = (begin-1);
for (int j = begin; j < end; j++) {
if (arr[j] <= pivot) {
i++;
int swapTemp = arr[i];
arr[i] = arr[j];
arr[j] = swapTemp;
}
}
int swapTemp = arr[i+1];
arr[i+1] = arr[end];
arr[end] = swapTemp;
return i+1;
}
// Main
public static void main(String[] args) {
int[] testArray1 = {34, 7, 23, 32, 5, 62};
int[] testArray2 = {88, 55, 42, 90, 21};
quickSort(testArray1, 0, testArray1.length - 1);
quickSort(testArray2, 0, testArray2.length - 1);
System.out.println(arrayToString(testArray1));
System.out.println(arrayToString(testArray2));
}
private static String arrayToString(int[] array) {
StringBuilder sb = new StringBuilder();
sb.append("[");
for (int i = 0; i < array.length; i++) {
sb.append(array[i]);
if (i < array.length - 1) sb.append(", ");
}
sb.append("]");
return sb.toString();
}
}