简单选择排序 一个简单示例

来源:互联网 发布:美股交易员知乎 编辑:程序博客网 时间:2024/06/10 05:11
#include <stdio.h>int iList[] = {152, 11, 98, 23, 33};void sort(int iList[], int iLength){int iMostSmallIndex;//save the index of last numberint i, j;int iTemp;//for temporary usefor(i = 1; i < iLength; i++){iMostSmallIndex = iLength - 1;//init as last indexfor(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;}

原创粉丝点击