-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsortingtimes.cpp
More file actions
76 lines (64 loc) · 1.55 KB
/
sortingtimes.cpp
File metadata and controls
76 lines (64 loc) · 1.55 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
#include <iostream>
#include <time.h>
#include <stdlib.h>
using namespace std;
void populate_array(int *arr, int num_elements) {
for (int i=0; i < num_elements; i++) {
*(arr + i) = rand();
}
}
void print_array(int *arr, int num_elements) {
for (int i=0; i < num_elements; i++) {
cout << *(arr + i);
if (i < num_elements - 1) {
cout << ", ";
}
}
cout << endl;
}
void bubble_sort(int *arr, int num_elements) {
for (int i=0; i < num_elements; i++) {
for (int j=i+1; j < num_elements; j++) {
if (*(arr+i) > *(arr+j)) {
int temp = *(arr+i);
*(arr+i) = *(arr+j);
*(arr+j) = temp;
}
}
}
}
void insertion_sort(int *arr, int num_elements) {
for (int i=1; i < num_elements; i++) {
int val = *(arr+i);
int j;
for (j=i; j > 1; j++) { // descending
if (*(arr + j-1) > val) {
*(arr + j) = *(arr + j - 1);
}
else {
break;
}
}
*(arr + j) = val;
}
}
int main() {
int num;
cout << "How many integers would you like to sort? ";
cin >> num;
srand(time(NULL));
int *num_array = new int[num];
populate_array(num_array, num);
//print_array(num_array, num);
int before = time(NULL);
bubble_sort(num_array, num);
int after = time(NULL);
cout << "Bubble Sort run time: " << (after - before) << " seconds" << endl;
//print_array(num_array, num);
insertion_sort(num_array, num);
//print_array(num_array, num);
int after1 = time(NULL);
cout << "Insertion Sort run time: " << (after1 - after) << " seconds" << endl;
delete [] num_array; // deallocate num_array
return 1;
}