Skip to content

Instantly share code, notes, and snippets.

@XcqRomance
Created November 13, 2018 14:13
Show Gist options
  • Save XcqRomance/26582c958cab23dbda6c06f345991ded to your computer and use it in GitHub Desktop.
Save XcqRomance/26582c958cab23dbda6c06f345991ded to your computer and use it in GitHub Desktop.
插入排序 时间复杂度:O(n^2) 空间复杂度:O(1),原地排序 稳定排序: 插入排序比冒泡排序更受欢迎:冒泡排序中的数据交换需要3个赋值语句,插入排序中的数据移动操作1个操作
// 插入排序 打扑克抓牌,左手是已经排好序的牌,右手是桌上的牌
void inserSort(int *arr,int arrSize) {
for (int j = 1; j < arrSize; j++) {
int key = arr[j]; // 右手里拿的牌
int i = j - 1;
while (i >= 0 && arr[i] > key) { // 左手已经排好序的牌和key进行比较
arr[i+1] = arr[i];
i--;
}
arr[i+1] = key;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment