-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
248 lines (204 loc) · 5.85 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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
#include <iostream>
#include <time.h>
#include <vector>
#include <algorithm>
using namespace std;
// start from 0, find the minimum of the array and swap it with the first number.
// next from 1, find the minimum of the array and swap it with the second number and so on...
void selectionSort(int ar[], int n){
int min_index;
for (int i = 0; i < n-1; i++) {
min_index = i;
for (int j = i; j <= n-1; j++) {
if (ar[j] < ar[min_index]){
min_index = j;
}
}
int tmp = ar[min_index];
ar[min_index] = ar[i];
ar[i] = tmp;
}
}
// push bigger number further back untill next number is bigger for the whole array.
// do this n times. bigger numbers float to the end of the array.
void bubbleSort(int ar[], int n){
for (int i = 0; i < n-1; i++) {
for (int j = 0; j < n-i-1; j++) {
if (ar[j] > ar[j+1]){
int tmp = ar[j];
ar[j] = ar[j+1];
ar[j+1] = tmp;
}
}
}
}
// same as before but with recursion instead of for loop
void recursiveBubbleSort(int ar[], int n){
if (n == 1){
return;
}
for (int i = 0; i < n - 1; i++) {
if (ar[i] > ar[i+1]){
int tmp = ar[i];
ar[i] = ar[i+1];
ar[i+1] = tmp;
}
}
recursiveBubbleSort(ar, n-1);
}
// store value being checked in key(starts from 1). check if previous value is bigger than value at key.
// if previous number is bigger change value where key was to previous number.
// go back checking if previous.previous value is greater than key, if so change previous to previous.previous, effectivly sorting the list from start to key position.
// when previous...previous is not greater than key, write key at previous...previous+1;
void insertionSort(int ar[], int n){
int key, j;
for (int i = 1; i < n; i++) {
key = ar[i];
j = i -1;
while (j>=0 && ar[j] > key){
ar[j+1] = ar[j];
j = j-1;
}
ar[j+1] = key;
}
}
// same as above
void recursiveInsertionSort(int ar[], int n){
if (n <= 1){
return;
}
recursiveInsertionSort(ar, n-1);
int last = ar[n-1];
int j = n-2;
while ( j >=0 && ar[j]>last ){
ar[j+1] = ar[j];
j--;
}
ar[j+1] = last;
}
// splits array in half recursively until it cant be split anymore.
// puts left half of the smallest array in L and the right in R.
// compares L and R until one runs out, and appends them to main array.
// then checks if there are any elements left in L or R, if so put them in main array.
void merge(int ar[], int l, int m, int r){
int n1 = m-l+1;
int n2 = r -m;
int L[n1], R[n2];
for (int i = 0; i < n1; i++) {
L[i] = ar[l+i];
}
for (int i = 0; i < n2; i++) {
R[i] = ar[m + 1 +i];
}
int i = 0;
int j = 0;
int k = l;
while (i<n1 && j<n2){
if (L[i] <= R[j]){
ar[k] = L[i];
i++;
} else{
ar[k] = R[j];
j++;
}
k++;
}
while (i < n1){
ar[k] = L[i];
i++;
k++;
}
while (j <n2){
ar[k] = R[j];
j++;
k++;
}
}
void mergeSort(int ar[], int l, int r){
if (l>=r){
return;
}
int m = (l + (r-1))/2;
mergeSort(ar,l,m);
mergeSort(ar,m+1,r);
merge(ar,l,m,r);
}
// kako pivot zemi go posledniot element
// i = -1, j = 0, ako vrednosta vo [j] e pomala od pivot: [i++], i smeni gi mestata na [j] i [i]
// na krajot sporedba koja ne pominuva e [j] < [j], pa mora uste edna smena so: [i++], smena na [i] i pivot [high]
// rekurzivno se povikuva quicksort za levata i desnata strana no bez pivot tockata koja sto e vo tocnoto mesto posle izminuvanje na partition()
int partition(int ar[], int low, int high){
int pivot = ar[high];
int i = (low -1);
for (int j = low; j <= high ; j++) {
if (ar[j] < pivot){
i++;
int tmp = ar[i];
ar[i] = ar[j];
ar[j] = tmp;
}
}
int tmp = ar[i+1];
ar[i+1] = ar[high];
ar[high] = tmp;
return (i+1);
}
void quickSort(int ar[], int low, int high){
if (low < high){
int pi = partition(ar, low, high);
quickSort(ar, low, pi-1);
quickSort(ar, pi+1, high);
}
}
// Obicno se koristi so float za grupiranje na vrednosti kako 0.2, 0.23, 0.21 vo ista koficka, n*ar[i] ke ni dade 2.1 2.3 i slicno, so znaci ce bidat staveni vo koficka b[2]
// potoa se sortirat kofickite individualno so insertion sort
// i se dodavaat na main array
void bucketSort(float ar[], int n){
vector<float> b[n];
for (int i = 0; i < n; i++) {
int bi = n*ar[i];
b[bi].push_back(ar[i]);
}
for (int i = 0; i < n; i++) {
sort(b[i].begin(),b[i].end());
//insertionSort
}
int index = 0;
for (int i = 0; i < n; i++) {
for (int j = 0; j < b[i].size(); j++) {
ar[index++] = b[i][j];
}
}
}
int main() {
srand(time(0));
//Generate array
cout << "Enter lenght of array: " << endl;
int n;
cin>>n;
int array[n];
for (int i = 0; i < n; i++) {
array[i]=rand()%100;
}
//Print unsorted array
cout<<"Elements of unsorted array:"<<endl;
for (int i = 0; i < n; i++) {
cout<<i+1<<": "<<array[i]<<endl;
}
//Sorting methods
//
//selectionSort(array, n);
//bubbleSort(array, n);
//recursiveBubbleSort(array, n);
//insertionSort(array, n);
//recursiveInsertionSort(array,n);
//mergeSort(array, 0, n-1);
//quickSort(array,0, n-1);
//bucketSort(array, n); - doesnt work, wrong implementation in function
//Print sorted array array
cout<<"Elements of sorted array:"<<endl;
for (int i = 0; i < n; i++) {
cout<<i+1<<": "<<array[i]<<endl;
}
return 0;
}