-
Notifications
You must be signed in to change notification settings - Fork 0
/
bubblesort_rand.cpp
46 lines (40 loc) · 1001 Bytes
/
bubblesort_rand.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
#include <iostream>
#include <vector>
#include <ctime>
#include <cstdlib>
#include <algorithm>
#define VEC_SIZE 5
#define START_RANGE -100 // zakres liczb do losowania
#define RANGE 200
void fill(std::vector<int>& v) {
int num;
for (size_t i = 0; i < VEC_SIZE; i++) {
num = START_RANGE + rand() % RANGE;
v.push_back(num);
}
}
void print(const std::vector<int>& v) {
for (auto i : v)
std::cout << i << " ";
std::cout << "\n";
}
void bsort(std::vector<int>& v) {
for (std::vector<int>::const_iterator jt = v.end(); jt != v.begin(); --jt) {
for (std::vector<int>::iterator it = v.begin(); it < jt-1; ++it) {
if (*it > *(it + 1)) {
std::iter_swap(it, it + 1);
}
}
}
}
int main() {
std::vector<int> v;
srand(time(NULL));
fill(v);
std::cout << "Before bubble sort: \n";
print(v);
bsort(v);
std::cout << "After bubble sort: \n";
print(v);
return 0;
}