Skip to content

Commit 53ae179

Browse files
committed
Create main.cpp
1 parent ef98256 commit 53ae179

File tree

1 file changed

+199
-0
lines changed

1 file changed

+199
-0
lines changed

main.cpp

+199
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,199 @@
1+
#include <iostream>
2+
#include <ctime>
3+
#include <iomanip>
4+
5+
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
6+
7+
using namespace std;
8+
9+
//Prototypes
10+
11+
void mergeSort( int data [], int lenD );
12+
void merge( int merged [], int lenD, int L [], int lenL, int R [], int lenR );
13+
void selectionSort( int data [], int lenD );
14+
void insertionSort( int data [], int lenD );
15+
void bubbleSort( int data [], int lenD );
16+
void mergeSort( int data [], int lenD ) {
17+
if (lenD > 1) {
18+
int middle = lenD / 2;
19+
int rem = lenD - middle;
20+
int* L = new int [ middle ];
21+
int* R = new int [ rem ];
22+
23+
for ( int i = 0; i < lenD; i++) {
24+
if (i < middle) {
25+
L [ i ] = data [ i ];
26+
} else {
27+
R [ i - middle ] = data [ i ];
28+
}
29+
}
30+
31+
mergeSort ( L, middle );
32+
mergeSort ( R, rem );
33+
merge ( data, lenD, L, middle, R, rem);
34+
}
35+
}
36+
37+
void merge ( int merged [], int lenD, int L [], int lenL, int R [], int lenR) {
38+
int i = 0;
39+
int j = 0;
40+
while ( i < lenL || j < lenR ) {
41+
if ( i < lenL && j < lenR ) {
42+
if( L [ i ] <= R [ j ]) {
43+
merged [ i + j ] = L [ i ];
44+
i++;
45+
} else {
46+
merged [ i + j ] = R [ j ];
47+
j++;
48+
}
49+
} else if ( i < lenL ) {
50+
merged [ i + j ] = L [ i ];
51+
i++;
52+
} else if ( j < lenR ) {
53+
merged [ i+j ] = R [ j ];
54+
j++;
55+
}
56+
}
57+
}
58+
59+
void selectionSort ( int data [], int lenD ) {
60+
int j = 0;
61+
int tmp = 0;
62+
for ( int i = 0; i < lenD; i++ ) {
63+
j = i;
64+
for ( int k = i; k < lenD; k++ ) {
65+
if ( data [ j ] > data [ k ] ) {
66+
j = k;
67+
}
68+
}
69+
tmp = data [ i ];
70+
data [ i ] = data [ j ];
71+
data [ j ] = tmp;
72+
}
73+
}
74+
75+
void insertionSort ( int data [], int lenD ) {
76+
int key = 0;
77+
int i = 0;
78+
for ( int j = 1; j < lenD; j++ ) {
79+
key = data [ j ];
80+
i = j - 1;
81+
while ( i >= 0 && data [ i ] > key ) {
82+
data [ i + 1 ] = data [ i ];
83+
i = i - 1;
84+
data [ i + 1 ] = key;
85+
}
86+
}
87+
}
88+
89+
void bubbleSort ( int data [], int lenD ) {
90+
int tmp = 0;
91+
for ( int i = 0; i < lenD; i++ ) {
92+
for ( int j = ( lenD - 1 ); j >= ( i + 1 ); j-- ) {
93+
if ( data [ j ] < data [ j - 1 ] ) {
94+
tmp = data [ j ];
95+
data [ j ] = data [ j - 1 ];
96+
data [ j - 1 ] = tmp;
97+
}
98+
}
99+
}
100+
}
101+
102+
int main(int argc, char** argv) {
103+
104+
int i = 0;
105+
int n;
106+
n = 1000;
107+
108+
clock_t start;
109+
cout << "Insertion Sort" << endl; //Start Insertion Sort
110+
cout << "Number of Values : " << " | " << "Execution Time : " << endl;
111+
cout << "___________________________________________________" << endl;
112+
while ( i < 4 ) {
113+
int w = 1000;
114+
int a [ n ];
115+
for ( int q = 0; q < n; q++ ) { // Worst case is reserve case
116+
a [ q ] = w;
117+
w--;
118+
}
119+
start = clock();
120+
insertionSort ( a, n );
121+
start = clock() - start;
122+
123+
cout << n << setw(35) << (double)start / (double)CLOCKS_PER_SEC * 1000.0 << " ms " << endl;
124+
n = n * 5;
125+
i++;
126+
}
127+
128+
i = 0;
129+
n = 1000;
130+
131+
cout << "___________________________________________________" << endl;
132+
cout << "Selection Sort" << endl; //Start Selection Sort
133+
cout << "Number of Values : " << " | " << "Execution Time : " << endl;
134+
cout << "___________________________________________________" << endl;
135+
while ( i < 4 ) {
136+
int w = 1000;
137+
int a [ n ];
138+
for ( int q = 0; q < n; q++ ) { // Worst case is sorted
139+
a [ q ] = w;
140+
w++;
141+
}
142+
start = clock();
143+
selectionSort ( a, n );
144+
start = clock() - start;
145+
146+
cout << n << setw(35) << (double)start / (double)CLOCKS_PER_SEC * 1000.0 << " ms " << endl;
147+
n = n * 5;
148+
i++;
149+
}
150+
151+
i = 0;
152+
n = 1000;
153+
154+
cout << "___________________________________________________" << endl;
155+
cout << "Bubble Sort" << endl; //Start Bubble Sort
156+
cout << "Number of Values : " << " | " << "Execution Time : " << endl;
157+
cout << "___________________________________________________" << endl;
158+
while ( i < 4 ) {
159+
int w = 1000;
160+
int a [ n ];
161+
for ( int q = 0; q < n; q++ ) { // Worst case is random but i used reserve sorted
162+
a [ q ] = w;
163+
w--;
164+
}
165+
start = clock();
166+
bubbleSort ( a, n );
167+
start = clock() - start;
168+
169+
cout << n << setw(35) << (double)start / (double)CLOCKS_PER_SEC * 1000.0 << " ms " << endl;
170+
n = n * 5;
171+
i++;
172+
}
173+
174+
i = 0;
175+
n = 1000;
176+
177+
cout << "___________________________________________________" << endl;
178+
cout << "Merge Sort" << endl; //Start Merge Sort
179+
cout << "Number of Values : " << " | " << "Execution Time : " << endl;
180+
cout << "___________________________________________________" << endl;
181+
while ( i < 4 ) {
182+
int w = 1000;
183+
int a [ n ];
184+
for ( int q = 0; q < n; q++ ) { // Worst case is reserve sorted
185+
a [ q ] = w;
186+
w++;
187+
}
188+
start = clock();
189+
mergeSort ( a, n );
190+
start = clock() - start;
191+
192+
cout << n << setw(35) << (double)start / (double)CLOCKS_PER_SEC * 1000.0 << " ms " << endl;
193+
n = n * 5;
194+
i++;
195+
}
196+
197+
system ("pause");
198+
return 0;
199+
}

0 commit comments

Comments
 (0)