-
Notifications
You must be signed in to change notification settings - Fork 2
/
lab3.cpp
236 lines (197 loc) · 5.57 KB
/
lab3.cpp
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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
/* Dong Lin
Lab 3 - 1 PM
*/
#include <iostream>
#include <ctime>
#include <limits>
#include <algorithm>
using namespace std;
const int SIZE = 150000;
void selectionSort(int arr[], int size);
void mergeSort(int *arr, int left, int right);
void merge(int *arr, int l, int m, int r);
int main()
{
int randomArr1[SIZE], randomArr2[SIZE], msArr[SIZE];
clock_t t;
srand(time(NULL));
for(int i = 0; i < SIZE; i++)
{
randomArr1[i] = rand() % INT_MAX;
randomArr2[i] = rand() % INT_MAX;
}
/*
cout << "Randomized array 1:" << endl;
for (int i = 0; i < SIZE; i++ )
cout << randomArr1[i] << ' ';
cout << endl;
*/
// selection sort randomized array
cout << ">> Selection Sort <<: " << endl;
t = clock();
selectionSort(randomArr1, SIZE);
t = clock() - t;
/*
cout << "Sorted randomized array: " << endl;
for (int i = 0; i < SIZE; i++ )
cout << randomArr1[i] << ' ';
cout << endl;
*/
cout << "Selection Sort of " << SIZE << " randomized int takes: "
<< (double)t / CLOCKS_PER_SEC << " seconds" << endl;
// selection sort sorted array
t = clock();
selectionSort(randomArr1, SIZE);
t = clock() - t;
cout << "Selection Sort of " << SIZE << " sorted int takes: "
<< (double)t / CLOCKS_PER_SEC << " seconds" << endl;
// selection sort half sorted array
for (int i = SIZE / 2 + 1, j = SIZE - 1 ; i < j; i++, j--)
{
int tmp = randomArr1[i];
randomArr1[i] = randomArr1[j];
randomArr1[j] = tmp;
}
t = clock();
selectionSort(randomArr1, SIZE);
t = clock() - t;
cout << "Selection Sort of " << SIZE << " half sorted int takes: "
<< (double)t / CLOCKS_PER_SEC << " seconds" << endl;
// selection sort reversed array
int size = sizeof(randomArr1) / sizeof(randomArr1[0]);
reverse(randomArr1, randomArr1 + size);
t = clock();
selectionSort(randomArr1, SIZE);
t = clock() - t;
cout << "Selection Sort of " << SIZE << " reversed int takes: "
<< (double)t / CLOCKS_PER_SEC << " seconds" << endl;
/*
for (int i = 0; i < SIZE; i++ )
cout << randomArr1[i] << ' ';
cout << endl;
*/
/*
cout << "Randomized array 2:" << endl;
for (int i = 0; i < SIZE; i++ )
cout << randomArr2[i] << ' ';
cout << endl;
*/
cout << ">> Merge Sort <<: " << endl;
// merge sort randomized array
t = clock();
mergeSort(randomArr2, 0, SIZE - 1);
t = clock() - t;
/*
cout << "Sorted randomized array: " << endl;
for (int i = 0; i < SIZE; i++ )
cout << randomArr2[i] << ' ';
cout << endl;
*/
cout << "Merge Sort of " << SIZE << " randomized int takes: "
<< (double)t / CLOCKS_PER_SEC << " seconds" << endl;
// merge sort sorted array
t = clock();
mergeSort(randomArr2, 0, SIZE - 1);
t = clock() - t;
cout << "Merge Sort of " << SIZE << " sorted int takes: "
<< (double)t / CLOCKS_PER_SEC << " seconds" << endl;
// merge sort half sorted array
for (int i = SIZE / 2 + 1, j = SIZE - 1 ; i < j; i++, j--)
{
int tmp = randomArr2[i];
randomArr2[i] = randomArr2[j];
randomArr2[j] = tmp;
}
t = clock();
mergeSort(randomArr2, 0, SIZE -1);
t = clock() - t;
cout << "Merge Sort of " << SIZE << " half sorted int takes: "
<< (double)t / CLOCKS_PER_SEC << " seconds" << endl;
// sort reversed array
size = sizeof(randomArr2) / sizeof(randomArr2[0]);
reverse(randomArr2, randomArr2 + size);
t = clock();
mergeSort(randomArr2, 0, SIZE - 1);
t = clock() - t;
cout << "Merge Sort of " << SIZE << " reversed int takes: "
<< (double)t / CLOCKS_PER_SEC << " seconds" << endl;
/*
for (int i = 0; i < SIZE; i++ )
cout << randomArr2[i] << ' ';
cout << endl;
*/
return 0;
}
void selectionSort(int arr[], int size)
{
int i, j, MIN, index, temp;
for (i = 0; i < size - 1; i++)
{
MIN = arr[i];
index = i;
for (j = i + 1; j < size; j++)
{
if (MIN > arr[j])
{
MIN = arr[j];
index = j;
}
}
temp = arr[i];
arr[i] = arr[index];
arr[index] = temp;
}
}
void mergeSort(int arr[], int left, int right)
{
int middle;
if (left < right)
{
middle = (left + right) / 2;
mergeSort(arr, left, middle);
mergeSort(arr, middle+1,right);
merge(arr, left, middle, right);
}
}
void merge(int original[], int left, int middle, int right)
{
int i,j,k;
int arr1 = middle - left + 1;
int arr2 = right - middle;
int *fPtr = new int[arr1];
int *sPtr = new int[arr2];
for (i = 0; i < arr1; i++)
fPtr[i] = original[left+i];
for (j = 0; j < arr2; j++)
sPtr[j] = original[middle + 1 + j];
i = j = 0;
k = left;
while (i < arr1 && j < arr2)
{
if (fPtr[i] <= sPtr[j])
{
original[k] = fPtr[i];
i++;
}
else
{
original[k] = sPtr[j];
j++;
}
k++;
}
while (i < arr1)
{
original[k] = fPtr[i];
k++;
i++;
}
while (j < arr2)
{
original[k] = sPtr[j];
k++;
j++;
}
delete [] fPtr;
delete [] sPtr;
}