#include <stdio.h>
#include <stdlib.h>
#define ASIZE(a) (sizeof(a)/sizeof(a[0]))
void insert_sort(int* a, int size)
{
for (int i = 1; i < size; i++)
{
int value = a[i];
int j = i - 1;
for (; j >= 0; j--)
{
// 它的后面位置可以插入
if (a[j] <= value)
{
a[j+1] = value;
break;
}
// 移动到后面
a[j+1] = a[j];
}
}
}
void rand_array(int* a, int size)
{
for (int i = 0; i < size; i++)
{
a[i] = rand();
}
}
void print_array(int* a, int size)
{
for (int i = 0; i < size; i++)
{
printf("%d, ", a[i]);
}
printf("\n");
}
int main()
{
int a[10];
rand_array(a, ASIZE(a));
print_array(a, ASIZE(a));
insert_sort(a, ASIZE(a));
print_array(a, ASIZE(a));
}
标签:sort,int,插入排序,sizeof,include,size From: https://www.cnblogs.com/kehuadong/p/18391787