Thursday, January 21, 2016

Bubble Sort in C Programming

Bubble Sort in C Programming

#include<stdio.h>

int main()
{
int n,i,j;
int num[100];
int swap;

printf("How many element do you want to sort? \n");
scanf("%d",&n);

for(i=0 ; i<n; i++)
{
scanf("%d",&num[i]);
}

for(i=0; i<n-1; i++)
{
for(j=0 ; j< (n-i-1);j++)
{
if(num[j]>num[j+1]){
swap=num[j];
num[j]=num[j+1];
num[j+1]=swap;
}


}
}
printf("Your sorted list is :\n");

for(i=0; i<n; i++)
{
printf("%d\n",num[i]);
}

}

No comments:

Post a Comment

Thank you for commenting. Please wait for response :)