Sunday, 26 May 2013

0 A C - PROGRAM TO BUBBLE SORT THE GIVEN NUMBERS.

BUBBLE SORT :
Bubble sort, sometimes incorrectly referred to as sinking sort, is a simple sorting algorithm that works by repeatedly stepping through the list to be sorted, comparing each pair of adjacent items and swapping them if they are in the wrong order. The pass through the list is repeated until no swaps are needed, which indicates that the list is sorted. The algorithm gets its name from the way smaller elements "bubble" to the top of the list. Because it only uses comparisons to operate on elements, it is a comparison sort. Although the algorithm is simple, most of the other sorting algorithms are more efficient for large lists.

THE PROGRAM IS :
#include<stdio.h>
#include<conio.h>
void main()
{
    int i,n,j,t,a[100];
    printf("Enter the no of elements \n");
    scanf("%d",&n);
    printf("Enter the elements of the array \n");
    for(i=0;i<n;i++)
    {
        scanf("%d",&a[i]);
    }
    for(j=1;j<=n;j++)
    {
        for(i=0;i<n-1;i++)
        {
            if(a[i]>a[i+1])
            {
                t=a[i];
                a[i]=a[i+1];
                a[i+1]=t;
            }
        }
    }
    printf("The sorted array is \n");
    for(i=0;i<n;i++)
    {
        printf("%d",a[i]);
        printf("\n");
    }
    getch();
}
 


Just copy and paste into your Microsoft Visual C++ or Turbo C (these are program developing software)
Or else you can try this demo file and use it ...
Click HERE to download this file.

(PRESS ctrl+z to end the program)..
~NOTE~ : While executing if you get any warning then just ignore it and continue with your program...


IF FACE ANY PROBLEM DON'T FORGET TO COMMENT ...!!

Don't Forget to share.... 

Follow on Facebook : Ethical Hacking.

No comments:

Post a Comment