-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsolution.cpp
More file actions
30 lines (27 loc) · 780 Bytes
/
Copy pathsolution.cpp
File metadata and controls
30 lines (27 loc) · 780 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
// Stepsort · Bubble Sort
// Category: Sorting
// Animated walkthrough: https://stepsort.prakashraj.me/algorithm/bubble-sort
#include <bits/stdc++.h>
using namespace std;
// Repeatedly swap adjacent out-of-order pairs; stop early if a pass is clean.
void bubbleSort(vector<int>& arr) {
int n = (int)arr.size();
for (int i = 0; i < n - 1; i++) {
bool swapped = false;
for (int j = 0; j < n - 1 - i; j++) {
if (arr[j] > arr[j + 1]) {
swap(arr[j], arr[j + 1]);
swapped = true;
}
}
if (!swapped) break;
}
}
int main() {
vector<int> data = {5, 1, 4, 2, 8};
bubbleSort(data);
cout << "sorted:";
for (int x : data) cout << " " << x;
cout << endl;
return 0;
}