-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBubbleSortVeryEasyOptimized.cpp
More file actions
43 lines (36 loc) · 1.02 KB
/
BubbleSortVeryEasyOptimized.cpp
File metadata and controls
43 lines (36 loc) · 1.02 KB
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
#include <iostream>
#include<algorithm>
using namespace std;
// implementation of the Bubble Sort algorithm
int main() {
int arr[] = {9, 8, 7, 6, 5, 4, 3, 2, 1};
int n = 9; // I should use n = 9 for an array of size 9
// Original array
cout << "Original array: ";
for (int i = 0; i < n; i++) {
cout << arr[i] << " ";
}
// Bubble sort optimized
for (int i = 0; i < n - 1; i++) { // n-1 passes
// Traverse
bool flag = true;
for (int j = 0; j < n - 1 - i; j++) {
if (arr[j] > arr[j + 1]) { // Swap
swap(arr[j], arr[j + 1]); // InBuilt SWAP Function
flag = false;
}
}
if (flag==true) { //swap didn't happen
break;
}
}
// Time complexity:
// Worst Case: "O(n^2)"
// Avg. Case: "O(n^2)"
// Best Case: "O(n)"
cout << endl << "Sorted array: ";
for (int i = 0; i < n; i++) {
cout << arr[i] << " ";
}
return 0;
}