Friday, January 16, 2015

Selection Sorting- Learn

 Description:  A Program to illustrate selection sort in an array.

Author: Naman Kumar


#include<iostream.h>
#include<conio.h>

void main()
{
   clrscr();
   int i,j,smallest,pos,temp=0,a[20],n;

   cout<<"enter the no of elements in array";
   cin>>n;
   cout<<"enter array";
   for(i=0;i<n;i++)
     cin>>a[i];

   for(i=0;i<n;i++)
   {
      smallest=a[i];
      pos=i;
      for(j=i+1;j<n;j++)
      {  if(a[j]<smallest)
         {  smallest=a[j];
            pos=j;
         }
      }
      temp=a[i];
      a[i]=a[pos];
      a[pos]=temp;
   }
   for(i=0;i<n;i++)
     cout<<a[i]<<",";

 getch();


}
Please LIKE & SHARE US if you find this helpful!

Share

& Comment

0 comments:

Post a Comment

 

Copyright © 2015 Code Fervor™ is a registered trademark.

Designed by Templateism By Naman Kumar