Skip to content

Commit 6d5f8b7

Browse files
authored
Create bubblesort.c
1 parent 4636f91 commit 6d5f8b7

File tree

1 file changed

+40
-0
lines changed

1 file changed

+40
-0
lines changed

bubblesort.c

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
#include<stdio.h>
2+
#include<stdlib.h>
3+
4+
void swap(int *p, int *q)
5+
{
6+
int temp;
7+
temp = *p;
8+
*p = *q;
9+
*q = temp;
10+
}
11+
12+
void BubbleSort(int A[],int n)
13+
{
14+
int i,j,flag;
15+
for (i=0;i<n-1;i++)
16+
{
17+
flag = 0;
18+
for(j=0;j<n-i-1;j++)
19+
{
20+
if(A[j]>A[j+1])
21+
{
22+
swap(&A[j],&A[j+1]);
23+
flag = 1;
24+
}
25+
}
26+
if(flag==0) break;
27+
}
28+
}
29+
30+
int main()
31+
{
32+
int A[10] = {1,5,2,1,6,3,6,2,5,10};
33+
int i;
34+
BubbleSort(A,10);
35+
for(i=0;i<10;i++)
36+
{
37+
printf("%d ",A[i]);
38+
}
39+
return 0;
40+
}

0 commit comments

Comments
 (0)