C++ program to implement Selection Sort

#include<iostream>
using namespace std;
void sort(int *,int);

 

int main( )

{

     int n;

     int a[20];

     cout<<endl<<"Enter how many elements :";

     cin >> n;

     for(int i=0;i<n;i++)

     cin>>a[i];

     sort(a,n);

     for(i=0;i<n;i++)

     cout<<a[i];

     return 0;

};

 

void sort(int *a,int n)

{

     for(int i=0;i<n;i++)

     {

              int j=i;

              for(int k=i+1;k<n;k++)

              {

                  if(a[k]<a[j])

                  j=k;

              }

              int temp=a[i];

              a[i]=a[j];

              a[j]=temp;

     }

}

Input

Enter how many elements : 10

9  8  7  6  5  4  3  2  1  21

Output

1  2  3  4  5  6  7  8  9  21

Leave a comment

Your email address will not be published. Required fields are marked *