-
Notifications
You must be signed in to change notification settings - Fork 0
/
lec017.cpp
52 lines (43 loc) · 1.07 KB
/
lec017.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
52
//? Bubble Sort
//* TC: O(n^2) (both best & worst case)
#include<iostram>
using namespace std;
void print(vector<int> v) {
for(int i=0; i<v.size(); i++) {
cout<< v[i] <<" ";
}
cout<< endl;
}
void bubbleSort(vector<int>& arr, int n) {
for(int i=0; i<n-1; i++) {
bool swapped = false; //flag to exit if array is already sorted
for(int j=0; j<n-i-1; j++) {
if (arr[j] > arr[j+1]) {
swap(arr[j], arr[j+1]);
swapped = true;
}
}
if (swapped == false) {
break;
}
}
}
void bubbleSortAnother(vector<int>& arr, int n) {
for(int i=1; i<n; i++) {
for(int j=0; j<n-i; j++) {
if (arr[j] > arr[j+1]) {
swap(arr[j], arr[j+1]);
}
}
}
}
int main() {
vector<int> arr = {5,4,3,2,1};
vector<int> arr2 = {3,4,2,5,2,1,};
int n = arr.size();
bubbleSort(arr, n);
bubbleSortAnother(arr2, n);
print(arr)
print(arr2)
return 0;
}