-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathbubble_sort.cpp
More file actions
38 lines (34 loc) · 973 Bytes
/
Copy pathbubble_sort.cpp
File metadata and controls
38 lines (34 loc) · 973 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
31
32
33
34
35
36
37
38
/**
* Bubble sort
* https://en.wikipedia.org/wiki/Bubble_sort
*
* 冒泡排序
* 时间复杂度 O(n^2),最优O(n)
* 空间复杂度O(n),辅助空间O(1)
*/
#include <iostream>
#include <vector>
void bubble_sort(std::vector<int> *array) {
auto size = array->size();
bool already_sorted = false; // break loop if remaining elements are already sorted
for (auto i = 0; (i < size) && !already_sorted; ++i) {
already_sorted = true;
for (auto j = 0; j < size - i - 1; ++j) {
if ((*array)[j] > (*array)[j + 1]) {
already_sorted = false;
// swap
auto v = (*array)[j];
(*array)[j] = (*array)[j + 1];
(*array)[j + 1] = v;
}
}
}
}
int main() {
std::vector<int> array{3, 7, 10, 4, 1, 9, 5, 8};
bubble_sort(&array);
for (const auto &e : array) {
std::cout << e << ", ";
}
std::cout << std::endl;
}