C Program Bubble Sort

#include<stdio.h>
#include<conio.h>
void main()
{
    clrscr();
    int n, i, array[50], j, temp;
    printf("Enter total number of elements :");
    scanf("%d",&n);
    printf("Enter %d numbers :",n);
    for(i=0; i<n; i++)
    {
        scanf("%d",&array[i]);
    }
    printf("Sorting array using bubble sort technique...\n");
    for(i=0; i<(n-1); i++)
    {
        for(j=0; j<(n-i-1); j++)
        {
            if(array[j]>array[j+1])
            {
                temp=array[j];
                array[j]=array[j+1];
                array[j+1]=temp;
            }
        }
    }
    printf("Elements sorted successfully..!!\n");
    printf("Sorted list in ascending order :\n");
    for(i=0; i<n; i++)
    {
        printf("%d  ",array[i]);
    }
    getch();
}


Run:

No comments:

Post a Comment

100 C Programs with Code and Output

Program 1: C Program To Read Two Numbers And Print The Sum Of Given Two Numbers. Program 2: C Program To Read Three Numbers And Prin...