Wednesday, 16 August 2017

12C Program-15 Write a program to implement Selection Sort using 1-D Array


//Progr-15 selection sort
#include<iostream.h>
clrscr();
void selsort(int[],int);
int main()
{int ar[60],item,n,index;
cout<<"how many elements you want to create with array?";
cin>>n;
cout<<"\nenter array elements...";
for(int i=0;i<n;i++)
cin>>ar[i];
selsort(ar,n);
cout<<"\n\nthe stored array is nas shown below...\n";
for(i=0;i<n;i++)
cout<<ar[i]<<"  ";
cout<<endl;
return 0;
}
void selsort(int ar[],int size)
{
int small,pos,tmp;
for(int i=0;i<size;i++)
{small=ar[i];
pos=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]<<" ";
}}



No comments:

Post a Comment