Tuesday, 14 July 2015

C Timing Program for Selection Sort.

C Timing Program for Selection Sort


#include<stdio.h>
#include<stdlib.h>
#include<time.h>
Void selection(int [10000], int);
Int main()
   {
       Int a[10000],n,I;
       Clock_t start,end;
       Printf(“Enter the length of Array::\t”);
       Scanf(“%d”,&n);
       Printf(“generate random element::\n”);
       For(i=1;i<n;i++)
         {
            a[i]=rand()%10000;
            printf(“%d\t”,a[i]);
         }
       Start=clock();
       Selection(a,n);
       End=clock();
       Printf(“\n The Sorted list as follows::\n”);
       For(i=0;i<n;i++)
         {
          Printf(“%d\t”,a[i]);
         }
        Printf(“\n running time of the selection sort is ::%f\n”,(end-start)/(double) CLOCKS_PER_SEC);
       Return 0;
}
Void selection(int a[],int n)
{
    Int I,j,larg,pos;
    For(i=n-1;i>0;i--)
       {
           Larg=a[0];
          Pos=0;
          For(j=1;j<=I;j++)
            {
               If(a[j]>larg)
                {
                     Larg=a[j];
                     Pos=j;
                 }
            }
       a[pos]=a[i];
       a[i]=larg;
     }

}

0 comments:

Post a Comment