Algorithm For Selection Sort
#include "stdafx.h"
#include <iostream>
using namespace std;
int main()
{
int i, j, first, temp,a[10];
int numLength = 10;
cout<<"Enter array values"<<endl;
for(i=0;i<10;i++)
{
cin>>a[i];
}
for (i= 10- 1; i > 0; i--)
{
first = 0; // initialize to subscript of first element
for (j=1; j<=i; j++) // locate smallest between positions 1 and i.
{
if (a[j] > a[first])
first = j;
}
temp = a[first]; // Swap smallest found with element in position i.
a[first] =a[i];
a[i] = temp;
}
cout<<"After sorting array is"<<endl;
for(i=0;i<10;i++)
{
cout<<a[i]<<endl;
}
return 0;
}
Comments
Post a Comment