zoukankan      html  css  js  c++  java
  • 算法导论学习笔记1---排序算法(平台:gcc 4.6.7)

    平台:Ubuntu 12.04/gcc 4.6.7

    1. 插入排序
       1 #include<vector>
       2 #include <algorithm>
       3 #include<iostream>
       4 using namespace std;
       5 
       6 template <typename T>
       7 void insertSort(vector<T>& vec){
       8     //vector<T>::iterator ite;
       9     for(auto j=1;j<vec.size();j++)
      10     {
      11         T key=vec[j];
      12         int i=j-1;
      13         while(i>=0&&vec[i]>key)
      14         {
      15             vec[i+1]=vec[i];
      16             i--;
      17         }
      18         vec[i+1]=key;
      19     }
      20 }
      21 
      22 template<typename T>
      23 void printVector(const vector<T>& vec)
      24 {
      25     for(auto i=0;i<vec.size();i++)
      26     cout<<vec[i]<<" ";
      27 
      28     cout<<endl;
      29 }
      30 
      31 int main()
      32 {
      33     
      34     //void insertSort(vector<int>& vec);
      35     //void printVector(const vector<int>& vec);
      36 
      37     vector<int> vec;
      38     for(int i=10;i<20;i++)
      39     {
      40         vec.push_back(i);
      41     }
      42     printVector(vec);
      43     random_shuffle(vec.begin(),vec.end());
      44     printVector(vec);
      45     insertSort(vec);
      46     printVector(vec);
      47 
      48     return 0;
      49 }
      View Code

       运行结果:

  • 相关阅读:
    图片《小美眉》
    redhat基本知识
    Linux 求助。设置分辨率?

    PHP close
    别想一个人承担一切
    java charAt返回char,不是int
    我的计算器
    支付宝面试小贴士
    java string charAt length()疑惑
  • 原文地址:https://www.cnblogs.com/sansan/p/3434334.html
Copyright © 2011-2022 走看看