-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathbubbleSort.cpp
52 lines (46 loc) · 1.14 KB
/
bubbleSort.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
#include <iostream>
#include <vector>
#include <utility>
void bubble_sort(std::vector<int> &vec) {
for (int i = 0; i < vec.size(); ++i) {
for (int j = i + 1; j < vec.size(); ++j) {
if (vec[j] < vec[i]) {
std::swap(vec[i], vec[j]);
}
}
}
}
void bubble_sort_optimized(std::vector<int> &vec) {
for (int i = 0; i < vec.size(); ++i) {
bool has_swapped = false;
for (int j = i + 1; j < vec.size(); ++j) {
if (vec[j] < vec[i]) {
std::swap(vec[i], vec[j]);
has_swapped = true;
}
}
// If we haven't swapped after one iteration, everything is sorted
if (!has_swapped) return;
}
}
int main() {
std::vector<int> arr = { 5, 10, 30, 30, 21, 3, 1, 2 };
std::vector<int> arr2 = { 5, 10, 30, 30, 21, 3, 1, 2 };
std::cout << "Before" << std::endl;
for (int i : arr) {
std::cout << i << ", ";
}
std::cout << std::endl;
bubble_sort(arr);
std::cout << "After bubble_sort" << std::endl;
for (int i : arr) {
std::cout << i << ", ";
}
std::cout << std::endl;
bubble_sort_optimized(arr2);
std::cout << "After bubble_sort_optimized" << std::endl;
for (int i : arr2) {
std::cout << i << ", ";
}
std::cout << std::endl;
}