-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsortingalgotest.cpp
More file actions
96 lines (84 loc) · 1.83 KB
/
sortingalgotest.cpp
File metadata and controls
96 lines (84 loc) · 1.83 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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#include <iostream>
#include <random>
#include <chrono>
#include <algorithm>
using namespace std;
void selectionSort(int *const a, int size);
void bubbleSort(int *const a, int size);
void insertionSort(int *const a, int size);
void printArray(const int *const a, int size);
void newArray(int *const a, int size);
int main() {
int array[50];
newArray(array, 50);
selectionSort(array, 50);
newArray(array, 50);
bubbleSort(array, 50);
newArray(array, 50);
insertionSort(array, 50);
}
void newArray(int *const a, int size) {
static unsigned seed = chrono::system_clock::now().time_since_epoch().count();
static std::default_random_engine generator(seed);
static std::uniform_int_distribution<int> distribution(1,1000);
cout << "creating random array\n";
for (int i = 0; i < size; i++){
a[i] = distribution(generator);
}
printArray(a,size);
}
void selectionSort(int *const a, int size) {
cout << "seleciton sort\n";
int i, i2, v;
for ( i = 1; i < size; ++i){
v = a[i];
i2 = i-1;
while (i2 >= 0 && a[i2] > v) {
a[i2 + 1] = a[i2];
i2 -= 1;
}
a[i2+1] = v;
}
printArray(a,size);
}
void bubbleSort(int *const a, int size) {
cout << "bubble sort\n";
int i, j, temp;
for(i = size; i > 1; --i) {
for( j = 1; j < i; ++j) {
if (a[j - 1] > a[j]){
temp = a[j];
a[j] = a[j-1];
a[j-1] = temp;
}
}
}
printArray(a,size);
}
void insertionSort(int *const a, int size) {
cout << "insertion sort\n";
int i,j,tempindex(0),temp;
for (i = 0; i < size; i++) {
for (j = i; j < size; j++) {
if( a[tempindex] > a[j] ) {
tempindex = j;
}
}
temp = a[i];
a[i] = a[tempindex];
a[tempindex] = temp;
}
printArray(a,size);
}
void printArray(const int *const a, int size) {
int i(0), c(0);
while (i < size) {
if (c > 10) {
cout << endl;
c = 0;
}
c++;
cout << a[i++] << '\t';
}
cout << endl;
}