Which sort is this and is it better than insertion sort?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • depster
    New Member
    • May 2013
    • 7

    Which sort is this and is it better than insertion sort?

    Code:
    #include<stdio.h>
    #include<time.h>
    #include<conio.h>
    #include<assert.h>
    int main()
    {
    
        clock_t start, stop;
        int n,i,j,temp,a[100];
        scanf("%d",&n);
        for(i=0;i<n;i++)
            scanf("%d",&a[i]);
        double t = 0.0;
        assert((start = clock())!=-1);
        for(i=0;i<n-1;i++)
        {
            for(j=i+1;j<n;j++)
            {
                if(a[j]<a[i])
                {
                    temp=a[i];
                    a[i]=a[j];
                    a[j]=temp;
    
                }
            }
        }
    
        stop = clock();
        t = (double) (stop-start)/CLOCKS_PER_SEC;
    
        for(i=0;i<n;i++)
            printf("%d ",a[i]);
        printf("Run time: %f\n", t);
    
        return(0);
    }
  • vijay6
    New Member
    • Mar 2010
    • 158

    #2
    Hey depster, your code is for 'Selection Sort'.

    is it better than insertion sort?
    No. In general Insertion Sort is faster than Selection Sort.

    Comment

    • smashicoder
      New Member
      • May 2013
      • 1

      #3
      Yeah,selection sort, but i think is better using vectors ,in this way u won't have to implement the sorting code yourself :)

      Comment

      Working...