算法描述:
- 从第一个元素开始,该元素可以认为已经被排序
- 取出下一个元素,在已经排序的元素序列中从后向前扫描
- 如果该元素(已排序)大于新元素,将该元素移到下一位置
- 重复步骤3,直到找到已排序的元素小于或者等于新元素的位置
- 将新元素插入到该位置后
- 重复步骤2~5
度量:
数据结构 数组
最差时间复杂度 O(n^2)
最优时间复杂度 O(n)
平均时间复杂度 O(n^2)
最差空间复杂度 总共O(n) ,需要辅助空间O(1)
最佳算法 No
源码:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <time.h>
void print_ary( int * ary, unsigned int len )
{
while( len-- > 0 ) {
printf("%d ", *ary++ );
}
putchar('\n');
}
void create_rondom_number(int **parray, unsigned int number)
{
struct timeval tpstart;
unsigned int i =0;
if (NULL != *parray)
{
return;
}
*parray = (int *)malloc(sizeof(unsigned int) * number);
(void)memset(*parray, 0, sizeof(unsigned int) * number);
gettimeofday(&tpstart,NULL);
//srandom(time(NULL));
srandom(tpstart.tv_usec);
for (i=0; i<number; ++i)
{
//(*parray)[i] = random()%number; // range is [0, number-1]
//(*parray)[i] = random()%(b-a) + a; //range is [a, b)
((*parray)[i] = 1 + (int) ((double)number * rand() / (RAND_MAX + 1.0))); // [1, number]
}
}
void insertion_sort(int array[], unsigned int n)
{
int i,j;
int temp;
int first = 0;
int last = n - 1;
for (i = first + 1; i <= last; i++)
{
temp = array[i];
j=i-1;
//printf("\n\ni=%d, j=%d temp=%d: ", i, j, temp);
printf("\n\nstart from array[%d], temp=array[%d]=%d: ", j, i, temp);
print_ary( array, n);
/*与已排序的數逐一比較,大於temp時,該數向後移*/
while((j>=first) && (array[j] > temp)) //当first=0,j循环到-1时,由于[[短路求值]],
//不会运算array[-1], 但j和first必须是int性,否则会有隐性的强制转换
{
array[j+1] = array[j];
array[j] = 100000;
printf("--array[%d] move to array[%d]: ", j, j+1);
print_ary( array, n);
j--;
}
array[j+1] = temp; //被排序数放到正确的位置
printf("**insert temp=%d to array[%d]: ", temp, j+1);
print_ary( array, n);
}
}
int main( int argc, char ** argv )
{
int *pary = NULL;
if (argc !=2)
{
printf("\nUsage: %s Number\n", argv[0]);
return -1;
}
create_rondom_number(&pary, (unsigned int)atoi(argv[1]));
printf("\n\nrandom number: ");
print_ary( pary, (unsigned int)atoi(argv[1]) );
printf("Start sortting: \n");
insertion_sort( pary, (unsigned int)atoi(argv[1]));
printf("\n\nafter sorted: ");
print_ary( pary, (unsigned int)atoi(argv[1]) );
return 0;
}
运行结果:
[root@localhost sort]# ./insert 10
random number: 1 6 4 6 9 9 2 1 3 5
Start sortting:
start from array[0], temp=array[1]=6: 1 6 4 6 9 9 2 1 3 5
**insert temp=6 to array[1]: 1 6 4 6 9 9 2 1 3 5
start from array[1], temp=array[2]=4: 1 6 4 6 9 9 2 1 3 5
--array[1] move to array[2]: 1 100000 6 6 9 9 2 1 3 5
**insert temp=4 to array[1]: 1 4 6 6 9 9 2 1 3 5
start from array[2], temp=array[3]=6: 1 4 6 6 9 9 2 1 3 5
**insert temp=6 to array[3]: 1 4 6 6 9 9 2 1 3 5
start from array[3], temp=array[4]=9: 1 4 6 6 9 9 2 1 3 5
**insert temp=9 to array[4]: 1 4 6 6 9 9 2 1 3 5
start from array[4], temp=array[5]=9: 1 4 6 6 9 9 2 1 3 5
**insert temp=9 to array[5]: 1 4 6 6 9 9 2 1 3 5
start from array[5], temp=array[6]=2: 1 4 6 6 9 9 2 1 3 5
--array[5] move to array[6]: 1 4 6 6 9 100000 9 1 3 5
--array[4] move to array[5]: 1 4 6 6 100000 9 9 1 3 5
--array[3] move to array[4]: 1 4 6 100000 6 9 9 1 3 5
--array[2] move to array[3]: 1 4 100000 6 6 9 9 1 3 5
--array[1] move to array[2]: 1 100000 4 6 6 9 9 1 3 5
**insert temp=2 to array[1]: 1 2 4 6 6 9 9 1 3 5
start from array[6], temp=array[7]=1: 1 2 4 6 6 9 9 1 3 5
--array[6] move to array[7]: 1 2 4 6 6 9 100000 9 3 5
--array[5] move to array[6]: 1 2 4 6 6 100000 9 9 3 5
--array[4] move to array[5]: 1 2 4 6 100000 6 9 9 3 5
--array[3] move to array[4]: 1 2 4 100000 6 6 9 9 3 5
--array[2] move to array[3]: 1 2 100000 4 6 6 9 9 3 5
--array[1] move to array[2]: 1 100000 2 4 6 6 9 9 3 5
**insert temp=1 to array[1]: 1 1 2 4 6 6 9 9 3 5
start from array[7], temp=array[8]=3: 1 1 2 4 6 6 9 9 3 5
--array[7] move to array[8]: 1 1 2 4 6 6 9 100000 9 5
--array[6] move to array[7]: 1 1 2 4 6 6 100000 9 9 5
--array[5] move to array[6]: 1 1 2 4 6 100000 6 9 9 5
--array[4] move to array[5]: 1 1 2 4 100000 6 6 9 9 5
--array[3] move to array[4]: 1 1 2 100000 4 6 6 9 9 5
**insert temp=3 to array[3]: 1 1 2 3 4 6 6 9 9 5
start from array[8], temp=array[9]=5: 1 1 2 3 4 6 6 9 9 5
--array[8] move to array[9]: 1 1 2 3 4 6 6 9 100000 9
--array[7] move to array[8]: 1 1 2 3 4 6 6 100000 9 9
--array[6] move to array[7]: 1 1 2 3 4 6 100000 6 9 9
--array[5] move to array[6]: 1 1 2 3 4 100000 6 6 9 9
**insert temp=5 to array[5]: 1 1 2 3 4 5 6 6 9 9
after sorted: 1 1 2 3 4 5 6 6 9 9
[root@localhost sort]#
参考:
http://zh.wikipedia.org/wiki/%E6%8F%92%E5%85%A5%E6%8E%92%E5%BA%8F
http://baike.baidu.com/view/396887.htm