-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathBubble.cpp
44 lines (37 loc) · 883 Bytes
/
Bubble.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
/*C++ Example
Let us see a programming Example to demonstrate the bubble sort.*/
#include<iostream>
using namespace std;
int main ()
{
int i, j,temp,pass=0;
int a[10] = {10,2,0,14,43,25,18,1,5,45};
cout <<"Input list ...\n";
for(i = 0; i<10; i++) {
cout <<a[i]<<"\t";
}
cout<<endl;
for(i = 0; i<10; i++) {
for(j = i+1; j<10; j++)
{
if(a[j] < a[i]) {
temp = a[i];
a[i] = a[j];
a[j] = temp;
}
}
pass++;
}
cout <<"Sorted Element List ...\n";
for(i = 0; i<10; i++) {
cout <<a[i]<<"\t";
}
cout<<"\nNumber of passes taken to sort the list:"<<pass<<endl;
return 0;
}
/*Output:
Input list …
10 2 0 14 43 25 18 1 5 45
Sorted Element List …
0 1 2 5 10 14 18 25 43 45
Number of passes taken to sort the list:10*/