-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBubbleSort.c
More file actions
51 lines (46 loc) · 796 Bytes
/
Copy pathBubbleSort.c
File metadata and controls
51 lines (46 loc) · 796 Bytes
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
#include <stdio.h>
void swap(int*, int*);
void BubbleSort(int[], int );
void printArray(int[],int);
int main()
{
int A[]={2,45,62,7,90,0};
int n=sizeof(A)/sizeof(A[0]);
BubbleSort(A,n);
return 0;
}
void BubbleSort(int A[],int n)
{
int swapped, i, j,temp;
// n=sizeof(A)/sizeof(A[0]); //sizeof is not working when used in a function
for(i=0;i<n;i++)
{
swapped=0;
for(j=0;j<n-i-1;j++)
{
if(A[j]>A[j+1])
{
// temp=A[j];
// A[j]=A[j+1];
// A[j+1]=temp;
swap(&A[j],&A[j+1]);
swapped=1;
}
}
if(swapped==0)
break;
}
printArray(A,n);
}
void swap(int *a, int *b)
{
int temp;
temp=*a;
*a=*b;
*b=temp;
}
void printArray(int A[],int n)
{
for(int i=0;i<n;i++)
printf(" %d ",A[i]);
}