Wednesday, 2 October 2013

selection sort

It is simplest method of sorting.In this first we compare 0th element to all other element & swap values if any element found to be greater than 0th elemnt. SO after first iteration 0th elemnt is smallest no.than again choose 1st elemnt & compare it with all element come after 1st position.Than choose second..& repeat above procedure  till  (n-1)th element .
now our list is sorted..

                                                                     C code ::



 // selection sort ::   
 #include<stdio.h>  
 main()  
 {  
      int s,i,t,j;  
      printf("\n\n enter size of array : ");  
      scanf("%d",&s);  
      int arr[s];  
      for(i=0;i<s;i++)  
      {  
        printf("\n enter %dth element :",i+1);  
        scanf("%d",&arr[i]);       
      }  
      printf("\n\n list before sorted \n");  
      for(i=0;i<s;i++)  
      {  
        printf(" %d ",arr[i]);  
      }  
      for(i=0;i<s-1;i++)  
      {  
           for(j=i+1;j<s;j++)  
           {  
                if(arr[i]>arr[j])  
                {  
                     t=arr[i];  
                     arr[i]=arr[j];  
                     arr[j]=t;  
                }  
           }  
      }  
      printf("\n\n sorted list is \n");  
      for(i=0;i<s;i++)  
      {  
        printf(" %d ",arr[i]);  
      }  
 }  

OUTPUT :-

1 comment:

  1. wow.. thanks bro what a great blog I like it.. thanks for ur content..
    be_A_hacker

    ReplyDelete

THANKS FOR UR GREAT COMMENT

Blogger Widgets