// Selection sort
#include
<iostream>
using namespace std;
void print(int a[],
int size);
void sort(int a[],
int size);
void
swap(int& a, int& b);
int main()
{
int array[] = { 7,2,9,5,3 };
int size = sizeof(array) / sizeof(int);
cout << "Before the sort" << endl;
print(array,size);
// print before the sort
cout << endl;
sort(array,size);
cout << endl << "After the sort"
<< endl;
print(array,size);
// print after the sort
}
void print (int a[],
int size)
{
int i;
for (i = 0; i < size * 4 + 1; i++) cout << "-";
cout << endl;
cout << "| ";
for (i = 0; i < size; i++) cout << a[i]
<< " | "; cout << endl;
for (i = 0; i < size * 4 + 1; i++) cout <<
"-"; cout << endl;
}
void sort(int a[],
int size)
{
int minIndex;
for (int i = 0; i < size - 1; i++)
{
minIndex = i;
for (int j = i+1; j < size; j++)
{
if (a[j] < a[minIndex])
{
minIndex = j;
}
}
if(minIndex != i)
{
swap(a[i],a[minIndex]);
}
print(a,size);
}
}
void
swap(int& a, int& b)
{
int temp;
temp = a;
a = b;
b = temp;
}