/********************************************************************************************
select.cpp
Jim Millard
for CO311
Selection Sort
********************************************************************************************/
#include <iostream>
#include <string>
#include "sorttool.h"
using namespace std;
void SelectSort(int [], const int maxsize, const int used);
int IndexOfSmallest(int [], const int maxsize, const int begin, const int end);
void main()
{
const int MaxSize = 1000;
int Array[MaxSize];
int toSort;
cout << "How many numbers to sort (up to " << MaxSize << "): ";
cin >> toSort;
FillWithRand(Array, MaxSize, toSort); //setup the array with X numbers
cout << "Unsorted:" << endl;
ParseAndPrint(Array, MaxSize, toSort); //show the unsorted array
SelectSort(Array, MaxSize, toSort); //sort the first X elements of the array
cout << "Sorted:" << endl;
ParseAndPrint(Array, MaxSize, toSort); //show the (hopefully) sorted array
}
void SelectSort(int Array[], const int maxsize, const int count)
{
for (int current = 0; (current < count) && (current < maxsize); current++)
{
int target = IndexOfSmallest(Array, maxsize, current, count);
if (target > current)
Swap(Array[target], Array[current]);
}
return;
}
int IndexOfSmallest(int Array[], const int maxsize, const int begin, const int end)
{
int smallest = Array[begin];
int position = begin;
for (int i = begin+1; (i < end) && (i < maxsize); i++)
if (Array[i] < smallest)
{
smallest = Array[i];
position = i;
}
return position;
}