像打扑克牌一样,每次摸一张牌,把牌插入正确位置。
#include <stdio.h>void Insert_Sort_Small_To_Big(unsigned char* input_data, unsigned int input_length)
{int i = 0, j = 0;unsigned char temp = 0;for (i = 1; i < input_length; i++){temp = input_data[i];for(j = i - 1; j >= 0; j--){if (input_data[j] > temp){input_data[j + 1] = input_data[j];}else{break;}}input_data[j + 1] = temp;}
}int main()
{int i = 0;unsigned char buff[] = { 54, 78, 61, 46, 18, 56, 14, 51, 65, 97, 79, 13 };Insert_Sort_Small_To_Big(buff, sizeof(buff));for (i = 0; i < sizeof(buff); i++){printf("%d ", buff[i]);}printf("\r\n");}