Monday, March 12, 2012

Selection Sort example program in C

 /*  
 This is a basic program of Selection sort.  
 Author: Afiz  
 Date:  
 */  
 #include<stdio.h>  
 main()  
 {  
 int a[5]={9,7,4,8,1};// array with 5 elements.  
 int out,in,min,i;// local variable declarations   
 for(out =0;out<4;out++)// outer loop   
      {  
           min =out;   
      for(in=out+1;in<5;in++)// inner loop   
           {  
                if(a[in]<=a[min])  
                     min=in;   
           }  
      int tmp = a[min]; // swap ....   
      a[min]=a[out];  
      a[out]=tmp;  
      }  
 //printing array   
 for(i=0;i<5;i++)  
 {  
      printf("%d\n",a[i]);   
 }  
 }  

No comments:

Post a Comment