-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathBubble.cpp
More file actions
69 lines (55 loc) · 1.11 KB
/
Bubble.cpp
File metadata and controls
69 lines (55 loc) · 1.11 KB
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
#include <iostream>
using namespace std;
void swap(int *x, int *y){
int temp = *x;
*x = *y;
*y = temp;
};
void Bubble(int A[], int n){
int i, j;
for(i = 0; i < n-1; i++){
for(j = 0; j < n-i-1; j++){
if(A[j]>A[j+1]){
swap(&A[j], &A[j+1]);
}
}
}
};
int main(){
int A[] = {10,50,20,60,25,65,30,45,85,1};
int n = 10, i;
Bubble(A,n);
for(i = 0; i < n; i++)
cout<<A[i]<<endl;
return 0;
}
**********************using Flag**************
#include <iostream>
using namespace std;
void swap(int *x, int *y){
int temp = *x;
*x = *y;
*y = temp;
};
void Bubble(int A[], int n){
int i, j,Flag=0;
for(i = 0; i < n-1; i++){
Flag = 0;
for(j = 0; j < n-i-1; j++){
if(A[j]>A[j+1]){
swap(&A[j], &A[j+1]);
Flag = 1;
}
}
if(Flag == 0)
break;
}
};
int main(){
int A[] = {10,50,20,60,25,65,30,45,85,1};
int n = 10, i;
Bubble(A,n);
for(i = 0; i < n; i++)
cout<<A[i]<<endl;
return 0;
}