//C语言选择排序
#include <stdio.h>
#include <conio.h>
void SelectSort ( int array[], int nSize )
{
int nMinIndex;
int nIndex_1, nIndex_2;
for (nIndex_1 = 0;nIndex_1 < nSize - 1 ;nIndex_1++) //这个呢负责把待排序的数列走几趟..
{
nMinIndex = nIndex_1;
for (nIndex_2 = nIndex_1 + 1 ; nIndex_2 < nSize;nIndex_2++) //这个呢..是走一趟.然后找到最值.
{
if ( array[nMinIndex] > array[nIndex_2] )
{
nMinIndex = nIndex_2;
}
}
if ( nMinIndex != nIndex_1 ) //一趟找到最值后,就交换到未排好序的序列的最前面了...
{
int temp = array[nIndex_1];
array[nIndex_1] = array[nMinIndex];
array[nMinIndex] = temp ;
}
}
}
void ShowArray(int array[], int nSize)
{
int i;
for (i = 0; i < nSize; i++)
{
printf("%d/t", array[i]);
if ((i+1)%10 == 0)
{
printf("/n");
}
}
printf("/n");
}
int main()
{
int array[7] = {6, 8, 10, 1, 2, 7, 9};
printf("Old Array:/n");
ShowArray(array, 7);
SelectSort(array, 7);
printf("Sorted Array:/n");
ShowArray(array, 7);
printf("Press any key to exit");
getch();
}