/** * @brief - sort an array by insert sort * @param[in, out] array - pointer of the array [not null] * @param[in] size - size of the array [>0] * @retval 0 - succeed * @retval -1 - fail */ int insert_sort(int *array, int size) { if ((NULL == array) || (size < 1)) { return -1; } if (1 == size) { return 0; } int i = 0; int j = 0; int tmp = 0; // 依次把第二个元素~最后一个元素插入 for (i = 1; i < size; ++i) { tmp = array[i]; j = i; // 寻找插入位置 while ((j >= 1) && (array[j - 1] > tmp)) { array[j] = array[j - 1]; --j; } array[j] = tmp; } return 0; }