C LAB‎ > ‎(Sem2) Data Structures‎ > ‎

Q5. WAP to perform a bubble sort.

posted Jan 7, 2011, 11:50 PM by Neil Mathew   [ updated Jan 8, 2011, 12:06 AM ]

SOURCE CODE:

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
#include<stdio.h>
 
void Bsort(int *a,int n)
{
int i,j,temp;
 
for(i=n-1; i>=0; i--)
for(j=0; j<i; j++)
if(a[j]>a[j+1])
{
temp=a[j+1];
a[j+1]=a[j];
a[j]=temp;
}
 
}
 
main()
{
 
int a[50],n,i;
printf("Enter size of your array: ");
scanf("%d",&n);
 
printf("Enter elements of your array: ");
for(i=0; i<n; i++)
scanf("%d",&a[i]);
 
Bsort(a,n);
 
printf("\nElements: \n");
for(i=0; i<n; i++)
printf("%d ",a[i]);
 
}

OUTPUT:

Enter size of your array: 5
Enter elements of your array: 2 8 4 6 5

Elements: 
2 4 5 6 8 

Comments