Selection sorting algorithm
Selection sort scans an array of ints to find the smallest, then replaces the first element with the smallest. When(if) that happens, the routine continues again but this time excluding the first element(which is now the smallest), and so on.
When I run the program, I get this:
Code:
{ 3 4 6 5 8 8 65 13 66 748 }
the code:
Code:
#include <iostream>
using namespace std;
const int MAX_ELEMENTS = 10;
void selsort(int [], int);
int main()
{
int unsorted[MAX_ELEMENTS] = {5,3,748,6,8,65,8,66,4,13};
//3,4,5,6,8,8,13,65,66,748
selsort(unsorted, 0);
cout << "{ ";
for (int i=0; i<MAX_ELEMENTS; i++)
{
cout << unsorted[i] << " ";
}
cout << "}" << endl;
cin >> unsorted[0];
return 0;
}
void selsort(int array[], int loc)
{
if (loc == (MAX_ELEMENTS-1))
return;
int tmp = 0;
for (int i=loc; i<MAX_ELEMENTS; i++)
{
if (array[i] < array[loc])
{
tmp = array[loc];
array[loc] = array[i];
array[i] = tmp;
selsort(array, loc + 1);
}
}
//if (locsmall == 0)
// return;
}