Pages

Monday, 20 September 2021

Selection sort in c++


#include<iostream.h>
#include<conio.h>
#include<iomanip.h>
class selectionsort
{
 public:
int a[33],i,j,n,min,temp;
 public:
void getdata();
void sort();
void display();
};
void selectionsort::getdata()
{
 cout<<"Enter the size :"<<ends;
 cin>>n;
 for(i=0;i<n;i++)
  {
  cout<<"Enter the Element a["<<i<<"] :"<<ends;
  cin>>a[i];
  }
}
void selectionsort::sort()
{
  temp=0;
  for(i=0;i<n;i++)
    {
     min=i;
     for(j=i+1;j<n;j++)
      {
if(a[j]<a[min])
{
min=j;
}
      }
      temp=a[i];
      a[i]=a[min];
      a[min]=temp;
     cout<<"\n Pass"<<ends<<i+1<<ends<<"value that is to swap"<<ends<<temp<<ends<<"with minimum value"<<setw(5)<<a[i];
    }
}

void selectionsort::display()
{
    cout<<"\n Sorted Array elements :";
    for(i=0;i<n;i++)
     {
     cout<<a[i]<<" ";
     }
    for(i=0;i<n;i++)
    {
     cout<<"\nAddress:"<< &a[i] <<"\tArray:"<<"a["<<i<<"]"<<"\tData:"<<a[i]<<"\tLocation or Position:"<<i+1;
    }
}


void main()
{
selectionsort ss;
clrscr();
ss.getdata();
ss.sort();
ss.display();
getch();
}

Output:

Selection sort animation video link:
https://www.youtube.com/watch?v=g-PGLbMth_g

No comments:

Post a Comment

Constructors & Destructors in c++

  Constructors :  A Constructor is a special member function, which is used to initialize the objects of its class. The Constructor is invok...