- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSort algorithms.cpp
More file actions
Latest commit
194 lines (161 loc) · 10.7 KB
/
Copy pathSort algorithms.cpp
File metadata and controls
194 lines (161 loc) · 10.7 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
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
// Sort algorithms.cpp: определяет точку входа для консольного приложения.
//
#include"stdafx.h"
#include<iostream>
#include<stdlib.h>
#include<time.h>
usingnamespacestd;// because I am lazy
voidprintArray(int* myArray, constint arrayLength) {
cout << "Printing the array:" << endl;
for (int i = 0; i < arrayLength; ++i)
cout << myArray[i] << "";
cout << endl;
}
voidzeroArray(int* myArray, constint arrayLength) {
cout << "The array is going to be set to zero" << endl;
for (int i = 0; i < arrayLength; ++i)
myArray[i] = 0;
printArray(myArray, arrayLength);
}
voidinsertionSort(int* myArray, constint arrayLength) {
cout << "Insertion sort is starting" << endl;
int i;
for (int j = 1; j < arrayLength; ++j) {
int key = myArray[j];
for (i = j - 1; i > -1 && myArray[i] > key; --i)
myArray[i + 1] = myArray[i];
myArray[i + 1] = key;
}
}
voidbubbleSort(int* myArray, constint arrayLength) {
cout << "Bubble sort is starting" << endl;
for (int i = arrayLength - 1; i > 0; --i)
for (int j = 0; j < i; ++j)
if (myArray[j] > myArray[j + 1]) {
myArray[j] += myArray[j + 1];
myArray[j + 1] = myArray[j] - myArray[j + 1];
myArray[j] -= myArray[j + 1];
}
}
voidselectionSort(int* myArray, constint arrayLength) {
cout << "Selection sort is starting" << endl;
for (int i = 0; i < arrayLength - 1; ++i) {
int iSmall = i;
for (int j = i + 1; j < arrayLength; ++j)
if (myArray[iSmall] > myArray[j])
iSmall = j;
if (i != iSmall)
swap(myArray[iSmall], myArray[i]);
}
}
voidmergeSort(int* myArray, constint start, constint end) {
cout << "Merge sort is starting" << endl;
if (end - start < 2or myArray == nullptr) return;
if (end - start == 2) {
if (myArray[start] > myArray[end - 1])
swap(myArray[start], myArray[end - 1]);
}
mergeSort(myArray, start, start + (end - start) / 2);
mergeSort(myArray, start + (end - start) / 2, end);
int* newArray = newint[end - start];
int newLength = 0;
int s1 = start;
inte1 = start + (end - start) / 2;
int s2 = e1;
while (newLength < end - start) {
if (s1 >= e1 || s2 < end && myArray[s2] < myArray[s1])
newArray[newLength++] = myArray[s2++];
else
newArray[newLength++] = myArray[s1++];
}
for (int i = start; i < end; ++i)
myArray[i] = newArray[i - start];
}
voidquickSort(int* myArray, constint arrayLength) {
cout << "Quick sort is starting" << endl;
if (arrayLength < 2or myArray == nullptr) return;
if (arrayLength == 2) {
if (myArray[0] > myArray[1])
swap(myArray[0], myArray[1]);
}
int right = arrayLength - 1;
int left = 0;
int temp;
srand(time(NULL));
int point = myArray[rand() % arrayLength];
while (left <= right) {
while (myArray[left] < point) ++left;
while (myArray[right] > point) --right;
if (left < right) {
temp = myArray[left];
myArray[left++] = myArray[right];
myArray[right--] = temp;
}
}
if (right > 0) quickSort(myArray, right + 1);
if (arrayLength > left) quickSort(myArray + left, arrayLength - left);
}
voidcountingSort(int* myArray, constint len, constint min, constint max) {
cout << "Counting sort is starting" << endl;
if (min > max or myArray == nullptr) return;
int *cnt = newint[max - min + 1];
for (int i = min; i <= max; ++i)
cnt[i - min] = 0;
for (int i = 0; i < len; ++i)
++cnt[myArray[i] - min];
for (int i = min; i <= max; ++i)
for (int j = cnt[i - min]; j--;)
*myArray++ = i;
delete[] cnt;
}
intmain()
{
int key = 1;
clock_t timer;
while (key) {
int myArray[6] = { 5, 6, 2, 1, 9, 5 };
int arrayLength = 6;
printArray(myArray, arrayLength);
cout << "What insertion mode do you want to use? Choose between:" << endl;
cout << "insertion" << endl << "bubble" << endl << "selection" << endl
<< "quick" << endl << "counting" << endl << "merge" << endl;
char* insMode = newchar[10];
cin >> insMode;
if (!strcmp(insMode, "insertion") || !strcmp(insMode, "INSERTION") || !strcmp(insMode, "ins")) {
timer = clock();
insertionSort(myArray, arrayLength);
}
elseif (!strcmp(insMode, "bubble") || !strcmp(insMode, "BUBBLE") || !strcmp(insMode, "bub")) {
timer = clock();
bubbleSort(myArray, arrayLength);
}
elseif (!strcmp(insMode, "selection") || !strcmp(insMode, "SELECTION") || !strcmp(insMode, "sel")) {
timer = clock();
selectionSort(myArray, arrayLength);
}
elseif (!strcmp(insMode, "quick") || !strcmp(insMode, "QUICK") || !strcmp(insMode, "qui")) {
cout << "Quick sort is starting" << endl;
timer = clock();
quickSort(myArray, arrayLength);
}
elseif (!strcmp(insMode, "counting") || !strcmp(insMode, "COUNTING") || !strcmp(insMode, "count")) {
timer = clock();
countingSort(myArray, arrayLength, 0, 10);
}
elseif (!strcmp(insMode, "merge") || !strcmp(insMode, "MERGE") || !strcmp(insMode, "mer")) {
timer = clock();
mergeSort(myArray, 0, arrayLength);
}
elseif (!strcmp(insMode, "zero") || !strcmp(insMode, "ZERO")) {
timer = clock();
zeroArray(myArray, arrayLength);
}
timer = clock() - timer;
printArray(myArray, arrayLength);
cout << "Time spent: " << (float)timer / CLOCKS_PER_SEC << endl;
cout << "Another try? (1/0)" << endl;
key = 0;
cin >> key;
}
return0;
}