《数据结构:邓俊辉版》——插入排序
void InsertSort(int nLen) { for (int i = 1; i < nLen; i++) { int j = i - 1; while (g_szArray[j] > g_szArray[i]) { j--; if (j < 0) { break; } } if (j == i - 1) { continue; } int tmp = g_szArray[i]; int k = i; while (k > j) { g_szArray[k] = g_szArray[k - 1]; k--; } g_szArray[j + 1] = tmp; } }
优化一下代码的逻辑:
void InsertSort2(int nLen) { for (int i = 1; i < nLen; i++) { int tmp = g_szArray[i]; int j = i - 1; while (g_szArray[j] > tmp && j > 0) { g_szArray[j + 1] = g_szArray[j--]; } g_szArray[j + 1] = tmp; } }
相关推荐
koushr 2020-11-12
zhangxiafll 2020-11-13
kikaylee 2020-10-31
范范 2020-10-28
MILemon 2020-10-22
hugebawu 2020-10-12
LauraRan 2020-09-28
shenwenjie 2020-09-24
omyrobin 2020-09-23
guangcheng 2020-09-22
qiangde 2020-09-13
hanyujianke 2020-08-18
晨曦之星 2020-08-14
xiesheng 2020-08-06
KAIrving 2020-08-02
xiesheng 2020-08-02
范范 2020-07-30
chenfei0 2020-07-30