Direct Selection Sort
#include "stdafx.h"
#include <iostream>
void DirectSelectSort(int* a, int iLen);
void Swap(int* a, int index1, int index2);
void PrintArray(int a[], int iLen);
int _tmain(int argc, _TCHAR* argv[])
{
int arrayToSort[] = {20, 7, 3, 4, 25, 15, 29, 12, 4, 1};
int n = sizeof(arrayToSort)/sizeof(int);
PrintArray(arrayToSort, n);
DirectSelectSort(arrayToSort, n);
PrintArray(arrayToSort, n);
return 0;
}
void DirectSelectSort(int* a, int iLen)
{
for(int i=0; i<=iLen-2; i++)
{
int min = a[i];
int imin = i;
for(int j=i+1;j<iLen;j++)
{
if(a[j]<min)
{
min=a[j];
imin=j;
}
}
if(i!=imin)
{
Swap(a,i,imin);
}
}
}
void Swap(int* a, int index1, int index2)
{
if(index1!=index2&&a!=NULL)
{
int temp = a[index1];
a[index1] = a[index2];
a[index2] = temp;
}
}
void PrintArray(int a[], int iLen)
{
for(int i=0; i< iLen; i++)
{
std::cout<<a[i]<<' ';
}
std::cout<<std::endl;
}