Chinaunix首页 | 论坛 | 博客
  • 博客访问: 264230
  • 博文数量: 45
  • 博客积分: 930
  • 博客等级: 准尉
  • 技术积分: 553
  • 用 户 组: 普通用户
  • 注册时间: 2012-01-22 17:53
文章分类

全部博文(45)

文章存档

2013年(5)

2012年(40)

分类: C/C++

2012-07-18 09:38:15


点击(此处)折叠或打开

  1. #include <iostream>
  2. #include <vector>
  3. #include <cstdlib>
  4. #include <ctime>

  5. const int SIZE = 20;
  6. using namespace std;
  7.     
  8. template <typename T>
  9. void CharuSort( vector<T> & array,int lens)
  10. {
  11.     for (int p = 1; p < lens;p++)
  12.     {
  13.         T curItem = array[p];
  14.         int j = p;
  15.         for(;j > 0 && curItem < array[j-1];j--)
  16.             array[j] = array[j-1];
  17.         array[j] = curItem;
  18.         cout<<"第"<<p<<"步: ";
  19.         for (int tmp = 0; tmp <= p; tmp++)
  20.             cout<<array[tmp]<<" ";
  21.         cout<<endl;
  22.     }
  23. }

  24. int main()
  25. {
  26.     vector<int> arr;
  27.     
  28.     srand((unsigned)time(0));
  29.     
  30.     for (unsigned index = 0; index < SIZE;index++)
  31.         arr.push_back(rand()%(10*SIZE));

  32.     cout<<"原始数列:";
  33.     for(int index = 0; index < SIZE;index++)
  34.         cout<<arr[index]<<" ";
  35.     cout<<endl;
  36.     CharuSort(arr,SIZE);
  37.     
  38.     return 0;
  39. }

阅读(1398) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~