#include <stdio.h>
int iList[] = {152, 11, 98, 23, 33};
void sort(int iList[], int iLength)
{
int iMostSmallIndex;//save the index of last number
int i, j;
int iTemp;//for temporary use
for(i = 1; i < iLength; i++)
{
iMostSmallIndex = iLength - 1;//init as last index
for(j = iMostSmallIndex - 1; j >= i - 1; j--)//ergodic from right to left
{
if(iList[j] < iList[iMostSmallIndex])
{
iMostSmallIndex = j;//save index of most small number
}
}
if(iMostSmallIndex != i-1)//the most small number is not the first one,exchange them
{
iTemp = iList[i-1];
iList[i-1] = iList[iMostSmallIndex];
iList[iMostSmallIndex] = iTemp;
}
}
}
int main(void)
{
int i;
int iLength = sizeof(iList) / sizeof(iList[0]);
sort(iList, iLength);
for(i = 0; i < iLength; i++)
{
printf(i == iLength - 1 ? "%d\n" : "%d,", iList[i]);
}
return 0;
}
简单选择排序 一个简单示例
最新推荐文章于 2022-08-05 17:31:33 发布