#include<stdio.h>
static void Insert_Sort(int *arr, int sz);
int buf[] = {1,3,6,100,4,2,8,13,45,2,54,6,45};
#define SIZE_LEN ((sizeof(buf))/(sizeof(buf[0])))
int
main(int argc, char *argv[])
{
printf("initial array is:");
for (int i = 0; i < SIZE_LEN; i++)
printf("%d ", buf[i]);
printf("\n");
Insert_Sort(buf, SIZE_LEN);
printf("Insert Sorted Array Is:");
for (int i = 0; i < SIZE_LEN; i++)
printf("%d ", buf[i]);
printf("\n");
return 0;
}
static void
Insert_Sort(int *arr, int sz)
{
int i = 0;
int j = 0;
for (i = 1; i < sz; i++){
int temp = arr[i];
for (j = i; (j > 0) && (arr[j - 1] > temp); j--){
arr[j] = arr[j-1];
}
arr[j] = temp;
}
}
/**
**************************************
initial array is:1 3 6 100 4 2 8 13 45 2 54 6 45
Insert Sorted Array Is:1 2 2 3 4 6 6 8 13 45 45 54 100
**************************************
*/
数据结构-插入排序
猜你喜欢
转载自blog.csdn.net/tyustli/article/details/86613426
今日推荐
周排行