Tuesday, 31 July 2018

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

 #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;

         }   //if end
    }//j loop end
  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]<<" ";
 }// i loop end

}// function end

No comments:

Post a Comment