Showing posts with label selection sort. Show all posts
Showing posts with label selection sort. Show all posts

Sunday, 9 August 2015

Selection sort using c++

# include<iostream>
using namespace std;
int main()
{       int n,min,i,j,k,temp;
        cout<<"Enter the size of array";
        cin>>n;
        int a[n];
        cout<<"Enter the elements of array";
        for(i=0;i<n;i++)
       cin>>a[i];
        for(j=0;j<n-1;j++)
            {
                  min=j;
                  for(k=j+1;k<n;k++)
                      {
                           if(a[k]<a[min])
                           min=k;
                      }
      
                  temp=a[min];
                 a[min]=a[j];
                 a[j]=temp;
       
           }
    cout<<"The sorted array by selection sort is";
    for(i=0;i<n;i++)
    cout<<a[i]<<endl;
}