Monday 23 June 2014

                                       -:Bubble Sort:-


             Sorting is the process of arranging items in some sequence and/or in different sets

1).Bubble Sort:-
                            Here is an example of how the algorithm works on a list of 5 integers:



code for Bubble Sort


#include<stdio.h>
#include<stdlib.h>

int main()
{
    int i,j,n,m,*arr,temp=0;
    printf("enter the amount of number to be sort\n");
    scanf("%d",&n);
    printf("enter the values to be sort\n");
    arr=(int*)malloc(sizeof(int)*n);
    for(i=0;i<n;i++)
    {
                    scanf("%d",&arr[i]);
    }
    for(i=0;i<n;i++)
    {
                    for(j=0;j<n-i;j++)
                    {
                                      if(arr[j]>arr[j+1])
                                      {
                                                         temp=arr[j];
                                                         arr[j]=arr[j+1];
                                                         arr[j+1]=temp;
                                      }
                    }
    }
    
     for(i=0;i<n;i++)
     {
                     printf("%d\t",arr[i]);
     }
     system("\n pause");
     return 0;
}
    

No comments:

Post a Comment