Site icon T4Tutorials.com

Program to Implement Selection Sort in CPP (C plus plus)

Program to Implement Selection Sort in CPP (C plus plus)

In this tutorial, we will learn about the Program to Implement Selection Sort in CPP (C plus plus).

#include<iostream>
 
using namespace std;
 
int main()
{
    int array[30];
    int l;
	int tmp;
	int h;
    int i;
	int j;
	int k;
	
	
	
    cout<<"Please enter the number of values:" ;
    cin>>k;
    
    cout<<endl<<"Please enter the desired values: "<<endl;
 
    for(i=0;i<k;i++)
    {
        cin>>array[i];
    }
 
    for(i=0;i<k-1;i++)
    {
        h=array[i];
        l=i;
        
        for(j=i+1;j<k;j++)
        {
            if(h>array[j])
            {
                h=array[j];
                l=j;
            }
        }
 
        tmp=array[i];
        array[i]=array[l];
        array[l]=tmp;
    }
 
    cout<<endl<<"Sorted list is shown below: " <<endl;
    for(i=0;i<k;i++)
    {
        cout<<array[i]<<" ";
    }
 
    return 0;
}

Output:

Program to Implement Selection Sort in CPP (C plus plus)
Exit mobile version