Sel sort in c ++

Sel sort program 


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

void selsort(int[],int);

void main()
{
clrscr();
int ar[50],item,n,index;
cout<<"how many array do you want to create array with?";
cin>>n;
cout<<"\nenter the array elements.....";
for(int i=0;i<n;i++)
cin>>ar[i];
selsort(ar,n);
cout<<"\n\n sorted array is shown below.....\n";

for(i=0;i<n;i++)
cout<<ar[i]<<" ";
cout<<endl;
getch();
}

void selsort(int ar[],int size)
{
int small,pos ,tmp;
for(int i=0;i<size;i++)
{
small=ar[i];
for(int j=i+1;j<size;j++)
{
   if(ar[j]<small)
    {
      small=ar[j];
      pos=j;
    }
}

tmp=ar[i];
ar[i]=ar[pos];
ar[pos]=tmp;
cout<<"\narray after pass-"<<i+1<<"-is:";
for(j=0;j<size;j++)
cout<<ar[j]<<" ";
}

}

Post a Comment

0 Comments