插入排序很简单的了,于是我将算法的优化的第一站选在了这里。《编程珠玑》在第十一章就首先讨论了这个问题:
我写的基本版本
《编程珠玑》给出的基本版本1:
区别就是在for循环的条件不一样:条件判断a[j - 1] > a[j]我放在了for循环里面,而版本1放在了for循环的判断条件里面;
《编程珠玑》给出的基本版本2:
for循环里面只有一次赋值,而原来有3个赋值;
随即生成测试程序结果:
结果:
insertsort1: the cost time is 1359
insertsort3: the cost time is 1000
insertsort4: the cost time is 578