数据结构与算法(C语言版)__插入排序

低级排序算法:冒泡排序,选择排序,插入排序
高级排序算法:归并排序,堆排序,快速排序
插入排序就是在已经排序的数据中从大往小比较,出现比该数小的就插入到该位置后面。

#include<iostream>
using namespace std;

void InsertionSort(int *a, int n);
int main(){
    int x[] = { 2, 4, 6, 8, 0, 1, 3, 5, 7, 9 };
    InsertionSort(x, 10);
    for (int i = 0; i < 10; ++i){
        cout << x[i] << endl;
    }
    system("pause");
    return 0;
}
void InsertionSort(int *a, int n){
    int in, out;
    //out = 0这个人已经出去了
    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;
    }
}

如果要对其他类型的数据进行排序,使用这个方法就不行,所以可以使用函数模板:

#include<iostream>

using namespace std;
template<class T>
void InsertionSort(T *a, int n);

int main(){
    double x[] = { 2.5, 4.3, 6.1, 8, 0, 1, 3, 5, 7, 9 };
    InsertionSort(x, 10);
    for (int i = 0; i < 10; ++i){
        cout << x[i] << endl;
    }
    system("pause");
    return 0;
}
template<class T>
void InsertionSort(T *a, int n){
    int in, out;
    //out = 0这个人已经出去了
    for (out = 1; out < n; ++out){
        T temp = a[out];
        in = out;
        while (in>0 && a[in-1]>=temp)
        {
            a[in] = a[in - 1];
            --in;
        }
        a[in] = temp;
    }
}

这里写图片描述
这样的话不论是int,long,double都可以使用,就不限于int类型了。

还可以使用函数模板对插入排序进行优化:
每次循环时都要比较是不是大于0,简化不用每次比较

#include<iostream>

using namespace std;

template<class T>
void InsertionSort_2(T *a, int n);

int main(){
    double x[] = { 0, 2.5, 4.3, 6.1, 8, 0, 1, 3, 5, 7, 9 };
    InsertionSort_2(x, 10);
    for (int i = 1; i <= 10; ++i){
        cout << x[i] << endl;
    }
    system("pause");
    return 0;
}

template<class T>
void InsertionSort_2(T *a, int n){
    //a[0]用来排序使用,不能保存原始数据
    for (int j = 2; j <= n; ++j){
        T temp = a[j];
        a[0] = temp;
        int i = j-1;
        while (temp < a[i]){
            a[i + 1] = a[i];
            i--;
        }
        a[i + 1] = temp;
    }
}

还可以把插入排序分成两个函数:

#include<iostream>

using namespace std;

template<class T>
void InsertionSort_2(T *a, int n);

template<class T>
void Insert(const T& e, T *a, int i);

int main(){
    double x[] = { 0, 2.5, 4.3, 6.1, 8, 0, 1, 3, 5, 7, 9 };
    InsertionSort_2(x, 10);
    for (int i = 1; i <= 10; ++i){
        cout << x[i] << endl;
    }
    system("pause");
    return 0;
}

template<class T>
void InsertionSort_2(T *a, int n){
    //a[0]用来排序使用,不能保存原始数据
    for (int j = 2; j <= n; ++j){
        T temp = a[j];
        Insert(temp, a, j - 1);
        /*a[0] = temp;
        int i = j-1;
        while (temp < a[i]){
            a[i + 1] = a[i];
            i--;
        }
        a[i + 1] = temp;*/
    }
}

template<class T>
void Insert(const T& e, T *a, int i){
    a[0] = e;
    while (e < a[i]){
        a[i + 1] = a[i];
        i--;
    }
    a[i + 1] = e;
}

总结:使用C++的一些高级编程方法,并知道算法经过细微的修改可以改变算法时间复杂度,使算法时间更短。自己设计排序算法一般也是选择插入排序,因为插入排序速度最快。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值