Skip to content

Commit 062ab2b

Browse files
Added Bubble sort program
I've made a PR by adding the bubble sort program to Cpp data structure folder as per your request. Kindly review it and merge it. Thanks.
1 parent 26fc63c commit 062ab2b

File tree

1 file changed

+47
-0
lines changed

1 file changed

+47
-0
lines changed

data_structures/Cpp/BubbleSort.c

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
#include <stdio.h>
2+
3+
void swap(int *a,int *b)
4+
{
5+
int temp;
6+
temp=*a;
7+
*a=*b;
8+
*b=temp;
9+
}
10+
11+
void Bubble(int A[],int n)
12+
{
13+
int i,j,flag=0;
14+
for(i=0;i<n-1;i++)
15+
{
16+
flag=0;
17+
for(j=0;j<n-i-1;j++)
18+
{
19+
if(A[j]>A[j+1])
20+
{
21+
swap(&A[j+1],&A[j]);
22+
flag=1;
23+
}
24+
}
25+
if(flag==0)
26+
{
27+
break ;
28+
}
29+
}
30+
}
31+
32+
int main()
33+
{
34+
int A[]={12,3,2,34,86,98,23};
35+
int n=7;
36+
for(int i=0;i<n;i++)
37+
{
38+
printf("%d\t",A[i]);
39+
}
40+
printf("\nAfter swapping\n");
41+
Bubble(A,n);
42+
for(int i=0;i<n;i++)
43+
{
44+
printf("%d\t",A[i]);
45+
}
46+
return 0;
47+
}

0 commit comments

Comments
 (0)