-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
199 lines (175 loc) · 4.57 KB
/
main.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
#include <iostream>
#include <ctime>
#include <iomanip>
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
using namespace std;
//Prototypes
void mergeSort( int data [], int lenD );
void merge( int merged [], int lenD, int L [], int lenL, int R [], int lenR );
void selectionSort( int data [], int lenD );
void insertionSort( int data [], int lenD );
void bubbleSort( int data [], int lenD );
void mergeSort( int data [], int lenD ) {
if (lenD > 1) {
int middle = lenD / 2;
int rem = lenD - middle;
int* L = new int [ middle ];
int* R = new int [ rem ];
for ( int i = 0; i < lenD; i++) {
if (i < middle) {
L [ i ] = data [ i ];
} else {
R [ i - middle ] = data [ i ];
}
}
mergeSort ( L, middle );
mergeSort ( R, rem );
merge ( data, lenD, L, middle, R, rem);
}
}
void merge ( int merged [], int lenD, int L [], int lenL, int R [], int lenR) {
int i = 0;
int j = 0;
while ( i < lenL || j < lenR ) {
if ( i < lenL && j < lenR ) {
if( L [ i ] <= R [ j ]) {
merged [ i + j ] = L [ i ];
i++;
} else {
merged [ i + j ] = R [ j ];
j++;
}
} else if ( i < lenL ) {
merged [ i + j ] = L [ i ];
i++;
} else if ( j < lenR ) {
merged [ i+j ] = R [ j ];
j++;
}
}
}
void selectionSort ( int data [], int lenD ) {
int j = 0;
int tmp = 0;
for ( int i = 0; i < lenD; i++ ) {
j = i;
for ( int k = i; k < lenD; k++ ) {
if ( data [ j ] > data [ k ] ) {
j = k;
}
}
tmp = data [ i ];
data [ i ] = data [ j ];
data [ j ] = tmp;
}
}
void insertionSort ( int data [], int lenD ) {
int key = 0;
int i = 0;
for ( int j = 1; j < lenD; j++ ) {
key = data [ j ];
i = j - 1;
while ( i >= 0 && data [ i ] > key ) {
data [ i + 1 ] = data [ i ];
i = i - 1;
data [ i + 1 ] = key;
}
}
}
void bubbleSort ( int data [], int lenD ) {
int tmp = 0;
for ( int i = 0; i < lenD; i++ ) {
for ( int j = ( lenD - 1 ); j >= ( i + 1 ); j-- ) {
if ( data [ j ] < data [ j - 1 ] ) {
tmp = data [ j ];
data [ j ] = data [ j - 1 ];
data [ j - 1 ] = tmp;
}
}
}
}
int main(int argc, char** argv) {
int i = 0;
int n;
n = 1000;
clock_t start;
cout << "Insertion Sort" << endl; //Start Insertion Sort
cout << "Number of Values : " << " | " << "Execution Time : " << endl;
cout << "___________________________________________________" << endl;
while ( i < 4 ) {
int w = 1000;
int a [ n ];
for ( int q = 0; q < n; q++ ) { // Worst case is reserve case
a [ q ] = w;
w--;
}
start = clock();
insertionSort ( a, n );
start = clock() - start;
cout << n << setw(35) << (double)start / (double)CLOCKS_PER_SEC * 1000.0 << " ms " << endl;
n = n * 5;
i++;
}
i = 0;
n = 1000;
cout << "___________________________________________________" << endl;
cout << "Selection Sort" << endl; //Start Selection Sort
cout << "Number of Values : " << " | " << "Execution Time : " << endl;
cout << "___________________________________________________" << endl;
while ( i < 4 ) {
int w = 1000;
int a [ n ];
for ( int q = 0; q < n; q++ ) { // Worst case is sorted
a [ q ] = w;
w++;
}
start = clock();
selectionSort ( a, n );
start = clock() - start;
cout << n << setw(35) << (double)start / (double)CLOCKS_PER_SEC * 1000.0 << " ms " << endl;
n = n * 5;
i++;
}
i = 0;
n = 1000;
cout << "___________________________________________________" << endl;
cout << "Bubble Sort" << endl; //Start Bubble Sort
cout << "Number of Values : " << " | " << "Execution Time : " << endl;
cout << "___________________________________________________" << endl;
while ( i < 4 ) {
int w = 1000;
int a [ n ];
for ( int q = 0; q < n; q++ ) { // Worst case is random but i used reserve sorted
a [ q ] = w;
w--;
}
start = clock();
bubbleSort ( a, n );
start = clock() - start;
cout << n << setw(35) << (double)start / (double)CLOCKS_PER_SEC * 1000.0 << " ms " << endl;
n = n * 5;
i++;
}
i = 0;
n = 1000;
cout << "___________________________________________________" << endl;
cout << "Merge Sort" << endl; //Start Merge Sort
cout << "Number of Values : " << " | " << "Execution Time : " << endl;
cout << "___________________________________________________" << endl;
while ( i < 4 ) {
int w = 1000;
int a [ n ];
for ( int q = 0; q < n; q++ ) { // Worst case is reserve sorted
a [ q ] = w;
w++;
}
start = clock();
mergeSort ( a, n );
start = clock() - start;
cout << n << setw(35) << (double)start / (double)CLOCKS_PER_SEC * 1000.0 << " ms " << endl;
n = n * 5;
i++;
}
system ("pause");
return 0;
}