Click here to hide categories Click here to show left categories

User: Home          welcome : Guest          Log In / Register here     




Bubble Sort

Bubble sort is a simple algorithm based on making comparison of two values and switch their position if not in correct order. E.g we will take an example of 12546 first compare will be of 1-2 both are in correct format so no need to swith. then comparison of 2-5 these are also in correct format. after that 5-4.both are not in correct order. so we will swith their position. after that 5-6 Now comparison ends, list will be in sorted order.

/****************************************/
/*PROGRAM TO PERFORM BUBBLE SORT*/
/*****************************************/

#include<stdio.h>
#include<conio.h>

void main()
{
       int a[10],n,i,j,temp,flag;

       clrscr();

       printf("Enter the size of array: ");
       scanf("%d",&n);
       printf("\nEnter the elements of array:\n");
       for(i=0;i<n;i++)
       {
              scanf("%d",&a[i]);
       }

       for(i=0;i<n-1;i++)
       {
              flag=0;
              for(j=0;j<n-i;j++)
              {
                     if(a[j]>a[j+1])
                    {
                            temp=a[j];
                            a[j]=a[j+1];
                            a[j+1]=temp;
                            flag=1;
                     }
              }
              if(flag==0)
              {
                     break;
              }
       }

       printf("\nAfter sorting the elements of array are:\n");
       for(i=0;i<n;i++)
       {   
              printf("%d ",a[i]);
       }
       getch();
}

Share this article   |    Print    |    Article read by 1682 times
Author:
Rohit kakria
I am software developer, moderator of xpode.com
Related Articles:
Related Interview Questions: No related interview question