C++算法七:插入排序

低级排序
  1. 冒泡排序(低级排序中速度最慢)
  2. 选择排序
  3. 插入排序(低级排序算法中最快的)
#include<iostream>
using namespace std;
template<class T>
void InsertionSort(T *a,int n);
void main()
{
    double x[] = {2.5,4,6.6,8.8,0,1.5,3,5,7,9};
    InsertionSort(x,10);
    for(int i=0;i<10;i++)
        cout<<x[i]<<endl;
//    return 0;
}
template<class T>
void InsertionSort(T *a,int n)
{
    
    int in,out;
    for(out=1;out<n;++out)
    {
        int temp= a[out];
        in = out;
        while(in>0 && a[in-1]>=temp)
        {
            a[in] = a[in-1];
            --in;
        }
        a[in] = temp;
    }
}

猜你喜欢

转载自blog.csdn.net/ziyouyi111/article/details/80416839