-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbubblesort.cpp
61 lines (60 loc) · 1.15 KB
/
bubblesort.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
#include<iostream>
using namespace std;
void bubblesort(int a[],int n)
{
int c;
for(int i=0;i<n-1;i++)
{
cout<<"working on pass no.:"<<i+1<<endl;
for(int j=0;j<n-1-i;j++)
{
if(a[j]>a[j+1])
{
c=a[j];
a[j]=a[j+1];
a[j+1]=c;
}
}
}
}
void bubblesortadaptive(int a[],int n)
{
int c;
int issorted=0;
for(int i=0;i<n-1;i++)
{
cout<<"working on pass no.:"<<i+1<<endl;
issorted=1;
for(int j=0;j<n-1-i;j++)
{
if(a[j]>a[j+1])
{
c=a[j];
a[j]=a[j+1];
a[j+1]=c;
issorted=0;
}
}
if(issorted)
{
return;
}
}
}
int printarray(int a[],int n)
{
for(int i=0;i<n;i++)
{
cout<<a[i]<<" ";
}
}
int main()
{
int a[]={2,6,5,9,7,34,67};
int size=7;
printarray(a,size);
cout<<endl;
// bubblesort(a,size);
bubblesortadaptive(a,size);
printarray(a,size);
}