Bubble Sort


#include<stdio.h>
#include<sys/time.h>
#define max 10
void bubble(int [],int);
void main()
{
            int a[max],i,n;
            int t1,t2;
            struct timeval tt;
            struct timezone tz;

            printf("How many element you want to short: ");
            scanf("%d",&n);
            printf("Enter array: ");
            for(i=0;i<n;i++)
               scanf("%d",&a[i]);
           
            gettimeofday(&tt,&tz);
            t1=tt.tv_usec;
            bubble(a,n);
            getimeofday(&tt,&tz);
            t2=tt.tv_usec;
            printf("\n\nTime required= %d",t2-t1);
}
void bubble(int a[],int n)
{
            int i,j,t;
           
            for(i=0;i<n-1;i++)
            {
                        for(j=0;j<n-1-i;j++)
                        {
                                    if(a[j]>a[j+1])
                                    {
                                                t=a[j];
                                                a[j]=a[j+1];
                                                a[j+1]=t;
                                    }
                        }
            }
            for(i=0;i<n;i++)
              printf("%d ",a[i]);
}

Output :-



No comments:

Post a Comment

ADA Programs

Home Bubble sort Insertion sort Selection sort Quick sort Merge sort Linear Search Binary Search Implementation and Time a...