常用排序的C++实现,附源码

一丶插入排序 

 二丶希尔排序

三丶冒泡排序 

        1.0数组版

         2.0链表版

 源代码

#include<iostream>
#include<vector>
#include<ctime>
#include<random>
#include<list>
#include<functional>
#include<algorithm>

//插入排序
void InsertSort(std::vector<int>&array,int n){
    int i , j , temp;
    for(i=1;i<n;i++){
        if(array[i]<array[i-1]){
            temp = array[i];
            for(j = i-1;j>=0&&array[j]>temp;j--){
                array[j+1] = array[j];
            }
            array[j+1] = temp;
        }
    }
}

//希尔排序
void ShellSort(std::vector<int>&array,int n){
    int d,i,j,temp;
    for(d = n/2;d >=1;d=d/2){
        for(i = d;i<n;++i){
                temp = array[i];
                for(j = i-d; j>= 0&&temp<array[j];j-=d){ 
                    array[j+d] = array[j];
                }
                array[j+d] = temp;  
        }
    }
}

//冒泡排序
void BubbleSort_array(std::vector<int>&array,int n){
    // //1.0
    // int i,num;
    // //用num来确定每次排序比较次数,每次循环,比较次数-1,因为,每次排序都将最小数置于最前方,因此开头不需要对比
    // for(num = n-1;num>0;num--){
    //     int t = num;
    //     bool change = false;//标志,当某次排序不移动任意一位元素时,整体有序
    //     for(i = n-1;t>0;i--,t--){
    //         if(array[i]<array[i-1]){
    //             std::swap(array[i-1],array[i]);
    //             change = true;
    //         }
    //     }
    //     if(change == false)
    //         break;
    // }

    //2.0
    int i , j;
    for(i = 0;i<n-1;i++){
        bool flag = false;
        for(j = n-1;j>i;j--){
            if(array[j-1]>array[j]){
                    std::swap(array[j-1],array[j]);
                    flag = true;
                }
        }
        if(flag==false)
            return ;
    }
}

bool compareison(const int a,const int b){
    return a>b;
}

void BubbleSort_list(std::list<int>&list,int n){
    bool swapped;
    do{
        swapped = false;
        auto it = list.begin();
        while(it!=list.end()&&std::next(it)!=list.end()){
            auto next = std::next(it);
            if(*it>*next){
                std::swap(*it,*next);
                swapped = true;
            }   
            it++;
        }
    }while(swapped);
}




int main()
{
    std::vector<int>array;
    std::uniform_int_distribution<int>u(1,50);
    std::default_random_engine e;
    std::list<int>L;
    e.seed(time(0));
    L.push_back(u(e));
    L.push_back(u(e));
    L.push_back(u(e));
    L.push_back(u(e));
    L.push_back(u(e));
    L.push_back(u(e));
    L.push_back(u(e));
    L.push_back(u(e));
    L.push_back(u(e));
    L.push_back(u(e));
    L.push_back(u(e));


    // BubbleSort_list(L,L.size());
    L.sort(compareison);
   
    array.push_back(u(e));   
    array.push_back(u(e));   
    array.push_back(u(e));   
    array.push_back(u(e));   
    array.push_back(u(e));   
    array.push_back(u(e));   
    array.push_back(u(e));   
    array.push_back(u(e));   
    array.push_back(u(e));   
    array.push_back(u(e));   
    array.push_back(u(e));   

    // // InsertSort(array,array.size());
    // // ShellSort(array,array.size());
    BubbleSort_array(array,array.size());

    for(auto p:array){
        std::cout << p <<std::endl;
    }

    for(auto p : L){
        std::cout << p<<std::endl;
    }
    return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值