-
Notifications
You must be signed in to change notification settings - Fork 31
/
Bubble_Sort.cpp
62 lines (61 loc) · 1.2 KB
/
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#include<iostream>
using namespace std;
void printArray(int *a,int n)
{
for(int i=0;i<n;i++)
{
cout<<a[i]<<" ";
}
cout<<endl;
}
void bubbleSort(int *a,int n)
{
int temp;
for(int j=0;j< n-1;j++)
{
for(int i=0;i< n-1;i++)
{
if(a[i]>a[i+1])
{
temp=a[i];
a[i]=a[i+1];
a[i+1]=temp;
}
}
}
}
void bubbleSortAdaptive(int *a,int n)
{
int temp;
int isSorted=1;
for(int j=0;j< n-1;j++)
// for number of pass
{
cout<<"Program working on pass number "<<j+1<<endl;
for(int i=0;i< n-1;i++)
// for comparison in each pass
{
if(a[i]>a[i+1])
{
temp=a[i];
a[i]=a[i+1];
a[i+1]=temp;
isSorted=0;
}
}
if(isSorted)
{
return;
}
}
}
int main()
{
printf("We are learning about Algorithms\n");
int array[6] ={30,60,10,50,20,40};
int n=6;
printArray(array,n);
bubbleSortAdaptive(array,n);
printArray(array,n);
return 0;
}