Selection Sort using C++
Posted by Samath
Last Updated: January 05, 2017
  692

This program ask the user for 8 integer and sort the entered integers using selection sort.

Selection sort is a sorting algorithm, specifically an in-place comparison sort. It has O(n2) time complexity, making it inefficient on large lists, and generally performs worse than the similar insertion sort. Selection sort is noted for its simplicity, and it has performance advantages over more complicated algorithms in certain situations, particularly where auxiliary memory is limited.

#include<iostream>
#include<iomanip>

using namespace std;

int loc=0;
int min(int sort[],int k);
int main()
{
    char ch;

          int select[8],k,c,temp,i;
          cout<<"Please enter 8 integer separated by space: "<<endl;
          for (i=0;i<8;i++)
          {
               cin>>select[i];
          }
          cout<<endl;
          for(k=0;k<8;k++)
          {
               c=min(select,k);   
               temp=0;
               temp=select[k];
               select[k]=select[loc];
               select[loc]=temp;
          } 
         cout<<"array after sort"<<endl;
         for ( i=0;i<8;i++)
         {
              cout<<select[i]<<'\t';
         }
         cout<<endl;
 
      return 0;

}

int min(int sort[],int k)
{
           int mini;
           mini=sort[k];
           loc=k;
          for(int j=k+1;j<=7;j++)
          {
               if(mini>sort[j])
               {
                   mini=sort[j];
                   loc=j;
                   
               }
          }
          return mini;
}