#include "util.h"
//选择排序:
//第一轮找出最小的数值放在iArray[0]
//第二轮找出次最小数值放在数组iArray[1]
//依次类推
void sort(int iArray[], int aSize)
{
int i, j;
for ( i = 0; i < aSize; i++ )
{
int min = i;
for ( j = i + 1; j < aSize; j++ )
{
if ( less(iArray[j], iArray[min]) )
{
min = j;
}
}
exch(iArray, i, min);
}
}
int main()
{
int test[] = {2, 5, 7, 30, 12, 14, 3, 6};
sort(test, sizeof(test)/sizeof(test[0]));
arrayShow(test, sizeof(test)/sizeof(test[0]));
return 0;
}
<pre name="code" class="cpp">#include <stdio.h>
#include "util.h"
void arrayShow(int iArray[], int size)
{
int i = 0;
for ( i = 0; i < size; i++ )
{
printf("%d ", iArray[i]);
}
printf("\r\n");
}
int less(int a, int b)
{
if ( a < b ) return 1;
return 0;
}
int compare(int a, int b)
{
if ( a > b ) return 1;
if ( a < b ) return -1;
return 0;
}
void exch(int iArray[], int indexA, int indexB)
{
int tmp = iArray[indexA];
iArray[indexA] = iArray[indexB];
iArray[indexB] = tmp;
}