正向插入排序

/*
* =====================================================================================
*
* Filename: insert_sort.c
*
* Description: 插入排序
*
* Version: 1.0
* Created: 2011年08月16日 07时22分18秒
* Revision: none
* Compiler: gcc
*
* Author: WangRan (), [email protected]
* Company:
*
* =====================================================================================
*/
#include <stdio.h>
#include <string.h>
int main(void)
{
int array[10]={11,10,9,8,7,6,5,4,3,2};
int i = 0;
int j = 0;
int key = 0;
/*将array[i]以前的序列看作有序序列,每次选中的key都是待排序列的第一位*/

for(i=1; i<10; i++)
{
key = array[i];
j = i-1;

while(j>=0 && array[j]>key)
{
array[j+1] = array[j];//前覆盖后
j--;
}

array[j+1] = key;//到不能移动的时候,即是key该存放的位置

}

printf("%d,%d,%d,%d,%d,%d,%d,%d,%d,%d\n ",array[0],array[1],array[2],array[3],array[4],array[5],array[6],array[7],array[8],array[9]);
return 1;
} /* ----- end of function insert_sort ----- */

猜你喜欢

转载自vergilwang.iteye.com/blog/2011442