1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
|
#include <iostream>
using namespace std;
const int arraySize = 10;
int main (int argc, char const *argv[])
{
// array to be sorted
int array[arraySize] = {5, 3, 7, 2, 6, 9, 0, 8, 1, 4};
int minIndex;
int minValue;
//selection sort for array
for (size_t i = 0; i < (arraySize - 1); ++i)
{
//initialize both to zero, then increment them using i
minIndex = i;
minValue = array[i];
//finds smallest value in remaining section of array
for (int index = i + 1; index < (arraySize); index++)
{
// if index is less than the minValue variable,
if (array[index] < minValue)
{
//change minValue to whatever array element the loop is on
minValue = array[index];
// increment minValue with index
minIndex = index;
}
}
//replace the minIndex variable's companion in the array with the current starter value
array[minIndex] = array[i];
//replace the current element of array with the minValue
array[i] = minValue;
}
for (size_t i = 0; i < arraySize; ++i)
{
cout << array[i] << " ";
}
return 0;
}
|
Output from /users/max/tmp/c++/testprs/untitled.cc
0 1 2 3 4 5 6 7 8 9 |