-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathQuickSort.java
101 lines (89 loc) · 3.2 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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
package sorting;
import java.util.Arrays;
public class QuickSort {
/**
* Time Complexity: O(n log n) - Avg case
* Time Complexity: O(n^2) - Worst case
* Space Complexity: O(n)
* @param input
*/
public static void sortWithNaive(int[] input){
quickSortWithNaivePartition(input,0,input.length-1);
}
/**
* Time Complexity: O(n log n) - Avg case
* Time Complexity: O(n^2) - Worst case
* Space Complexity: O(log n) on average due to recursive function calls
* @param input
*/
public static void sortWithLomuto(int[] input){
quickSortWithLomutoPartition(input,0,input.length-1);
}
private static void quickSortWithNaivePartition(int[] input, int left,int right){
if(left < right){
int p = naivePartition(input, left, right);
quickSortWithNaivePartition(input,left,p-1);
quickSortWithNaivePartition(input,p+1,right);
}
}
private static void quickSortWithLomutoPartition(int[] input, int left,int right){
if(left < right){
int p = lomutoPartition(input, left, right);
quickSortWithLomutoPartition(input,left,p-1);
quickSortWithLomutoPartition(input,p+1,right);
}
}
private static int lomutoPartition(int[] input, int left, int right){
int pivot = input[right];
int i = left; // place for swapping
for (int j = left; j < right; j++) {
if (input[j] <= pivot) {
swap(i, j, input);
i++;
}
}
swap(i, right, input);
return i;
}
private static int naivePartition(int[] input, int left, int right){
int pivot = right;
int[] temp = new int[right-left+1];
int index = 0;
//Store all the elements less than pivot in temp arr
for(int i=left;i<left+temp.length;i++){
if(input[i] < input[right]){
temp[index++] = input[i];
}
}
pivot = index;
//Store the pivot element itself
temp[index++] = input[right];
//Store all the elements greater than pivot in temp arr
for(int i=left;i<left+temp.length;i++){
if(input[i] > input[right]){
temp[index++] = input[i];
}
}
//Update the elements in input with temp arr
index = 0;
for(int i=left;i<left+temp.length;i++){
input[i] = temp[index++];
}
return pivot; // return pivot
}
private static void swap(int i, int j, int[] input){
int temp = input[j];
input[j] = input[i];
input[i] = temp;
}
public static void main(String[] args){
int[] input = new int[]{3,6,7,5,1,2,9,4,8};
System.out.println("Before Sorting using Naive: " + Arrays.toString(input));
QuickSort.sortWithNaive(input);
System.out.println("After Sorting using Naive: " + Arrays.toString(input));
input = new int[]{3,6,7,5,1,2,9,4,8};
System.out.println("Before Sorting using Lomuto: " + Arrays.toString(input));
QuickSort.sortWithLomuto(input);
System.out.println("After Sorting using Lomuto: " + Arrays.toString(input));
}
}