Bubble Sort in C
BUBBLE SORT
#include <stdio.h>
#define MAX 10
void swap(int *x,int *y)
{
int temp;
temp = *x;
*x = *y;
*y = temp;
}
void bsort(int list[])
{
int i,j;
for(i=0;i<(MAX-1);i++){
for(j=0;j<(MAX-(i+1));j++){
if(list[j] > list[j+1]){
swap(&list[j],&list[j+1]);
}
}
}
}
void printlist(int list[])
{
int i;
printf("The elements of the list are: \n");
for(i=0;i<MAX;i++)
printf("%d\t",list[i]);
}
void main()
{
int list[MAX];
list[0] = 2; list[1] = 1; list[2] = 4; list[3] = 3; list[4] = 9;
list[5] = 19; list[6] = 17; list[7] = 11; list[8] = 5; list[9] = 6;
printf("The list before sorting is:\n");
printlist(list);
bsort(list);
printf("The list after sorting is:\n");
printlist(list);
}
Comments
Post a Comment