-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathquickSortTest.java
More file actions
152 lines (115 loc) · 2.92 KB
/
quickSortTest.java
File metadata and controls
152 lines (115 loc) · 2.92 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
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
import java.util.Arrays;
public class quickSortTest {
public static void main(String[] args) {
// TODO Auto-generated method stub
int[] array = new int[11];
array[0] = 10;
array[1] = 34;
array[2] = 0;
array[3] = 21;
array[4] = 14;
array[5] = 37;
array[6] = 66;
array[7] = 71;
array[8] = 42;
array[9] = 9;
array[10] = 7;
int[]smallarray = new int[3];
smallarray[0] = 10;
smallarray[1] = 34;
smallarray[2] = 0;
//smallarray[3] = 21;
//smallarray[4] = 14;
quickSort(array, 0, 10);
//System.out.println(Arrays.toString(insertionSort(smallarray, 0, 2)));
System.out.println(Arrays.toString(array));
}
//quick sort method
public static void quickSort(int[] array, int first, int last) {
if (last - first < 3) { //length is less than 3
insertionSort(array, first, last);
}
else {
int middle = first + (last-first)/2;
int pivot = mot(array[first], array[middle], array[last]);
int split_point = partition(array, first, last, pivot);
quickSort(array, first, split_point-1); //first half of the array
quickSort(array, split_point+1, last);
}
}
//insert sort method
public static int[] insertionSort(int[] array, int first, int last) {
for (int i= first+1; i<(last-first+1); i++) {
int key = array[i];
int j = i-1; //pointer
while (j >= 0 && array[j] > key) {
array[j+1] = array[j];
j = j-1;
}
array[j + 1] = key;
}
return array;
}
//median of 3
public static int mot(int first, int middle, int last) {
int median = 0;
if (middle > first && middle < last) {
median = middle;
}
else if (middle > last && middle < first) {
median = middle;
}
else if (first > middle && first < last) {
median = first;
}
else if (first > last && first < middle) {
median = first;
}
else if (last > first && last < middle) {
median = last;
}
else if (last > middle && last < first) {
median = last;
}
return median;
}
//partition method
public static int partition(int[] array, int first, int last, int pivot) {
int firstValue = array[first];
int lastValue = array[last];
int pivotIndex = 0;
//swapping pivot and last
//for loop to put the last value at where the pivot is located
for (int i=0; i<array.length; i++) {
if (array[i] == pivot)
pivotIndex = i;
}
array[pivotIndex] = lastValue;
array[last] = pivot;
pivotIndex = last;
int i = first;
int j = last - 1;
boolean loop = true;
//move i right and j left until A[i] > pivot and A[j] < pivot
while (loop == true) {
while(array[i] <= pivot) {
i++;
}
while(array[j] >= pivot && j!=0) {
j--;
}
if(i < j) { //i and j have not yet crossed
int temp = array[j];
array[j] = array[i];
array[i] = temp;
}
else {
loop = false;
}
}
//swapping pivot and i
array[pivotIndex] = array[i];
array[i] = pivot;
return i;
}
}