C Program–To sort a Given set of numbers in ascending order (Bubble Sort)

/* Program to sort the given set of numbers in
ascending order, this sorting is called as bubble sort algorithm
*/

#include <stdio.h>
#include <conio.h>
int main()
{
    int a[10],i,j,temp=0;
    printf("Enter all the 10 numbers");
    for(i=0;i<10;i++)
    scanf("%d",&a[i]);
    for(i=0;i<10;i++)  //This loop is for total array elements (n)
    {
    for(j=0;j<9;j++) //this loop is for total combinations (n-1)
    {
                    if(a[j]>a[j+1]) //if the first number is bigger then swap the two numbers
                    {
                    temp=a[j];
                    a[j]=a[j+1];
                    a[j+1]=temp;
                    }
    }
    }
printf("The ordered array is");
for(j=0;j<10;j++) //Finally print the ordered array
printf("%d \t",a[j]);
getch();
return 0;
}

Comments

  1. it is very useful but the explanation for each line is not clear

    ReplyDelete
  2. the whole program is not correct

    ReplyDelete

Post a Comment

Popular Posts