-
Notifications
You must be signed in to change notification settings - Fork 81
Expand file tree
/
Copy pathbubbleSort.cpp
More file actions
31 lines (26 loc) · 803 Bytes
/
bubbleSort.cpp
File metadata and controls
31 lines (26 loc) · 803 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
#include<bits/stdc++.h>
using namespace std;
void bubbleSort(int arr[], int n){
bool swapped;
for(int i = 0;i < n; i++){
swapped = false; // initialising the variable as false, and will change it to true if swapping occurs.
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;
// the loop will break if no swapping takes place
// Hence making the time complexity O(n) in the best case which will occur if the given input array is already sorted.
}
}
int main() {
int a[] = {2, 1, 3, 4};
bubbleSort(a, 4);
for(int i = 0; i < 4; i++){
cout<<a[i]<<" ";
}
return 0;
}