Sunday, May 22, 2011

Write a program to sort a user inputted list of integers using Bubble sort. In this method of sorting multiple swapping take place in one pass and required (n-1) power to sort an array of n elements. Smaller elements move to top of list, which is also known as bubble up.

#include
#include
#define MAX 30
void bubblesort(int[],int);
main()
{
int arr[MAX],eleM,var;
printf("\n enter the size of array");
scanf("%d",&eleM);
for(var=1;var<=eleM;var++) { printf("\nenter the elements"); scanf("%d",&arr[var]); } printf("sorted array is"); bubblesort(arr,eleM); return 0; } void bubblesort(int arr[],int eleM) { int count,v,var; for(count=0;count<=eleM-1;count++) { for(v=0;v<=eleM-count-1;v++) { if(arr[v]>arr[v+1])
{
var=arr[v];
arr[v]=arr[v+1];
arr[v+1]=var;
}
}
}
for(count=0;count<=eleM-1;count++)
{
printf("\t%d",arr[count]);
}
}


OUTPUT

enter the size of array 6
enter the elements 3
enter the elements 5
enter the elements 1
enter the elements 0
enter the elements 4
enter the elements 6
sorted array is 0 1 3 4 5 6

No comments:

Post a Comment