-
Notifications
You must be signed in to change notification settings - Fork 0
/
BubbleSortAndItsOptimizations.cpp
67 lines (60 loc) · 1.64 KB
/
BubbleSortAndItsOptimizations.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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#include <iostream>
using namespace std;
void swap(int &a,int &b){
int temp=a;
a=b;
b=temp;
}
void bubblesort(int arr[],int n){
for(int i=0;i<n-1;i++){
for(int j=0;j<n-1;j++){ //j till n-2 because we are comparing arr[j+1] here, finally it'll lead to arr[n-1]
if(arr[j]>arr[j+1]){
swap(arr[j],arr[j+1]);
}
}
}
for(int i=0;i<n;i++){
cout<<arr[i]<<" ";
}
}
void optimizedbubble(int arr[],int n){
for(int i=0;i<n-1;i++){
for(int j=0;j<n-i-1;j++){ /*After i'th iteration, i elements are fixed to the rightmost correct positions
that is the ith largest elements so we don't even need to go to that side.
You can dry run to understand better*/
if(arr[j]>arr[j+1]){
swap(arr[j],arr[j+1]);
}
}
}
for(int i=0;i<n;i++){
cout<<arr[i]<<" ";
}
}
void ultraoptimizedBubble(int arr[],int n){
for(int i=0;i<n-1;i++){
bool swap=false;
for(int j=0;j<n-i-1;j++){
if(arr[j]>arr[j+1]){
swap(arr[j],arr[j+1]);
swap=true;
}
}
if(swap==false){
break; //No swaps required at the current iteration this implies that the array has became sorted, no more iterations to be considered
}
}
}
int main()
{
//Write a program for bubble sort
//Remember: n-1 iterations/checks for the same array
int arr[]={10,8,20,5};
int n=4;
bubblesort(arr,n);
cout<<endl;
optimizedbubble(arr,n);
cout<<endl;
ultraoptimizedBubble(arr,n);
return 0;
}