-
Notifications
You must be signed in to change notification settings - Fork 0
/
homework1sorting driver_AOA.java
201 lines (156 loc) · 4.4 KB
/
homework1sorting driver_AOA.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
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
import java.util.Random;
public class SortingDriver {
// Do not change the main method
public static void main(String[] args) {
final long SEED = 1378947745233l;
Random generator = new Random(SEED);
System.out.println("size\tselect\tinsert\tmerge\tquick");
int start = 100, end = 100000;
int size = start;
while (size < end) {
int randomArray[] = new int[size];
for (int i = 0; i < randomArray.length; ++i)
randomArray[i] = generator.nextInt();
System.out.print(size + "\t");
int a1[] = randomArray.clone(), a2[] = randomArray.clone(), a3[] = randomArray
.clone(), a4[] = randomArray.clone();
long startTime = System.currentTimeMillis();
selectionSort(a1);
long endTime = System.currentTimeMillis();
long time = endTime - startTime;
System.out.print(time + "\t");
startTime = System.currentTimeMillis();
insertionSort(a2);
endTime = System.currentTimeMillis();
time = endTime - startTime;
System.out.print(time + "\t");
startTime = System.currentTimeMillis();
mergeSort(a3);
endTime = System.currentTimeMillis();
time = endTime - startTime;
System.out.print(time + "\t");
startTime = System.currentTimeMillis();
quickSort(a4);
endTime = System.currentTimeMillis();
time = endTime - startTime;
System.out.println(time);
size = size * 3 / 2;
}
}
// Implement your algorithms here
public static void selectionSort(int arr[]) {
int imin;
for (int i=0;(i<arr.length-1);i++){
imin=i;
for(int j=i+1 ;j<arr.length;j++){
if(arr[j]<arr[imin]){
imin=j;
}
}
if(imin!=i){
final int temp= arr[i];
arr[i]=arr[imin];
arr[imin]=temp;
}
}
}
public static void insertionSort(int arr[]) {
for(int i=1;i<arr.length;i++){
int temp=i;
int val=arr[i];
while((temp>0) && (arr[temp-1]>val)){
arr[temp]=arr[temp-1];
temp--;
}
arr[temp]=val;
}
}
public static void mergeSort(int arr[]) {
MergeSort.mergeSort(arr); // static function call mergesort class
}
public static void quickSort(int arr[]) {
QuickSort.quickSort(arr); // static function call quick sort
}
}
// class for merge sort
class MergeSort{
static void mergeSort(int arr[]){
if (arr.length > 1) {
int[] leftarr = leftHalf(arr);
int[] rightarr = rightHalf(arr);
mergeSort(leftarr);
mergeSort(rightarr);
merge(arr, leftarr, rightarr);
}
}
static int[] leftHalf(int[] arr){
int n = arr.length / 2;
int[] left = new int[n];
for (int i = 0; i < n; i++) {
left[i] = arr[i];
}
return left;
}
static int[] rightHalf(int[] arr){
int n1 = arr.length / 2;
int n = arr.length - n1;
int[] right = new int[n];
for (int i = 0; i < n; i++) {
right[i] = arr[i + n1];
}
return right;
}
static void merge(int[] output, int[] left, int[] right) {
int l = 0;
int m = 0;
for (int i = 0; i < output.length; i++) {
if (m >= right.length || (l < left.length && left[l] <= right[m])) {
output[i] = left[l];
l++;
}
else {
output[i] = right[m];
m++;
}
}
}
}
// class for quick sort
class QuickSort {
public static void quickSort(int[] array) {
quickSort(array, 0, array.length - 1);
}
private static void quickSort(int[] array, int imin, int imax) {
if (imax > imin) {
int ipivot = part(array, imin, imax);
quickSort(array, imin, ipivot - 1);
quickSort(array, ipivot + 1, imax);
}
}
private static int part(int[] array, int imin, int imax) {
int pivot = array[imin];
int ilow = imin + 1;
int ihigh = imax;
while (ihigh > ilow) {
while (ilow <= ihigh && array[ilow] <= pivot)
ilow++;
while (ilow <= ihigh && array[ihigh] > pivot)
ihigh--;
if (ihigh > ilow) {
int temp = array[ihigh];
array[ihigh] = array[ilow];
array[ilow] = temp;
}
}
while (ihigh > imin && array[ihigh] >= pivot)
ihigh--;
if (pivot > array[ihigh]) {
array[imin] = array[ihigh];
array[ihigh] = pivot;
return ihigh;
}
else {
return imin;
}
}
}