#include <stdio.h>
void display(int array[], int len)
{
for(int i = 0; i < len; i++)
printf("%d ", array[i]);
printf("\n");
}
void insertsort(int array[], int arrlen)
{
if(arrlen < 0) {
printf("当前输入个数为0,重新输入");
}
for (int orderedNum = 1; orderedNum < arrlen; orderedNum++)
{
int insertValue = array[orderedNum];
int orderedIndex = orderedNum-1;
while(orderedIndex >= 0)
{
if(insertValue < array[orderedIndex])
array[orderedIndex + 1] = array[orderedIndex];
else
break;
orderedIndex--;
}
array[orderedIndex + 1] = insertValue;
printf("第%d趟插入排序", orderedNum);
display(array, arrlen);
}
}
int main () {
int array1[] = {4, 5, 6, 1, 3, 2};
int array2[] = {6, 5, 2, 4, 1, 3, 7, 8};
int array3[] = {4, 1, 9};
insertsort(array1, 6);
printf("============\n");
insertsort(array2, 8);
printf("============\n");
insertsort(array3, 3);
return 0;
}