Skip to content

Instantly share code, notes, and snippets.

@myusufid
Created October 15, 2017 15:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save myusufid/583f30c1059a4bef01458bcebee4ff3e to your computer and use it in GitHub Desktop.
Save myusufid/583f30c1059a4bef01458bcebee4ff3e to your computer and use it in GitHub Desktop.
Selection Sort C++ Ascending
#include <cstdlib>
#include <iostream>
using namespace std;
int main(int argc, char** argv) {
int i,j,n,loc,temp,min,a[30];
cout<<"Enter the number of elements:";
cin>>n;
cout<<"\nEnter the elements\n";
for(i=0;i<n;i++)
{
cin>>a[i];
}
for(i=0;i<n-1;i++)
{
min=a[i];
loc=i;
for(j=i+1;j<n;j++)
{
if(min>a[j])
{
min=a[j];
loc=j;
}
}
temp=a[i];
a[i]=a[loc];
a[loc]=temp;
}
cout<<"\nSorted list is as follows\n";
for(i=0;i<n;i++)
{
cout<<a[i]<<" ";
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment