Chinaunix首页 | 论坛 | 博客
  • 博客访问: 102832
  • 博文数量: 23
  • 博客积分: 555
  • 博客等级: 中士
  • 技术积分: 320
  • 用 户 组: 普通用户
  • 注册时间: 2011-11-14 11:04
文章分类

全部博文(23)

文章存档

2012年(23)

我的朋友

分类: C/C++

2012-05-10 22:47:50

插入排序:
1,第0个数,其自己是有序的,因此从后面开始处理。
2,将数向后移动。
3,插入位置:j+1。注意!

白盒阅读:
①下标是否越界
②是否是“插入”的思想
③能否“排序”

黑盒测试:
使用一个简化的用例,排序过程是否正确,结果是否正确

源码:
  1. void insert_sort(int a[],int n)
  2. {
  3.     // the single a[0] is sorted.
  4.     for(int i=1; i<n; i++)
  5.     {
  6.         int current = a[i];
  7.         
  8.         int j = i-1;
  9.         for( ;j >= 0; j--)
  10.         {
  11.             if (a[j] > current)
  12.             {
  13.                 a[j+1] = a[j];
  14.             }
  15.             else
  16.             {
  17.                 break;
  18.             }
  19.         }
  20.         // when it comes here,j's value may be -1.
  21.         a[j+1] = current;

  22.         output(a,n);
  23.     }
  24. }
阅读(748) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~