forked from ShreyaDhir/HacktoberFest2020
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathOptimized_bubble_sort.cpp
44 lines (40 loc) · 1.13 KB
/
Optimized_bubble_sort.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
#include<iostream>
using namespace std;
//- Optimized bubble sort technique (elements gets bubbled up!)
void bubbleSort(int* a,int n)
{
int flag; //-flag to check whether the further array is already sorted or not (This is gonna reduce ur passes!)
for(int i=0;i<n-1;i++){
flag = 0;
for(int j=0;j<n-1-i;j++) //- n-1-i used to reduce the number of comparisons done inside the array
{
if(a[j]>a[j+1]) //-comparing adjacent elements and swapping them
{
int temp = a[j];
a[j] = a[j+1];
a[j+1] = temp;
flag = 1;
}
}
if(flag == 0) //-break out if further array is sorted (if array isn't sorted then flag was to be 1)
{
break;
}
}
cout<<"The sorted array is: \n";
for(int k=0;k<n;k++)
{
cout<<a[k]<<" ";
}
}
int main()
{
int a[10] = {5,4,8,7,3,1,7,45,12,34};
int n = sizeof(a);
int totalElements = n/sizeof(int);
// cout<<totalElements<<endl;
bubbleSort(a,totalElements);
return 0;
}
// Best case O(n)
// Worst Case O(n^2)