- Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbubble_sort.cpp
More file actions
Latest commit
30 lines (25 loc) · 684 Bytes
/
Copy pathbubble_sort.cpp
File metadata and controls
30 lines (25 loc) · 684 Bytes
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
// BUBBLE SORT - repeatedly swaps adjacent elements until all are in order
// O(n^2)
#include<vector>
#include<iostream>
intmain() {
std::vector<int> test;
test.push_back(9);
test.push_back(1);
test.push_back(2);
test.push_back(7);
test.push_back(5);
for (int j = 0; j < test.size() - 1; ++j) {
for (int i = 0; i < test.size() - j - 1; ++i) {
if (test[i] > test[i + 1]) {
int temp = test[i + 1];
test[i + 1] = test[i];
test[i] = temp;
}
}
}
for (int i = 0; i < test.size(); ++i) {
std::cout << test[i] << "";
}
return0;
}