Selection Sort of array in C++
Using Visual Studio 2015
===============================================================
using namespace std;
#include<conio.h>
int main()
{
int i, j, n, loc, temp, min, arr[30];
cout << "Enter the number of elements:";
cin >> n;
for (i = 0;i<n;i++)
{
cout << "Enter the number=";
cin >> arr[i];
}
for (i = 0;i<n - 1;i++)
{
min = arr[i];
loc = i;
for (j = i + 1; j<n; j++)
{
if (min>arr[j])
{
min = arr[j];
loc = j;
}
}
temp = arr[i];
arr[i] = arr[loc];
arr[loc] = temp;
}
cout << "\nSorted list is as follows"<<endl;
for (i = 0;i<n; i++)
{
cout << arr[i] << " ";
}
_getche();
}
=========================================================================
Comments
Post a Comment