Quick Sort


#include<stdio.h>
#include<sys/time.h>
#define max 20
void quick(int [],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;
            quick(a,0,n-1);
            gettimeofday(&tt,&tz);
            t2=tt.tv_usec;
           
            printf("\n\nTime required= %d\n",t2-t1);
            for(i=0;i<n;i++)
              printf("%d ",a[i]);
}
void quick(int a[],int low,int high)
{
            int i,j,t,pivot;
            if(low<high)
           {
                        pivot=low;
                        i=low;
                        j=high;            
                        while(i<j)
                         {
                                    while((a[i]<=a[pivot]) && (i<high))
                                                i++;
                                    while(a[j]>a[pivot])
                                                j--;
                                    if(i<j)
                                   {
                                                t=a[i];
                                                a[i]=a[j];
                                                a[j]=t;
                                    }
                        }
                        t=a[pivot];
                        a[pivot]=a[j];
                        a[j]=t; 
                        quick(a,low,j-1);
                        quick(a,j+1,high);
    }

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...