几种排序算法 C++实现

没啥好说的,直接上代码吧
目前包括:冒泡 选择 插入 快速
MySort.hcp

//
// Created by Administrator on 2022/10/21 0021.
//

#include <iostream>
#include <random>
#include <string>
#include <vector>
#include <cstdlib>


using std::vector;
using std::endl;
using std::cout;
using std::cin;
using std::default_random_engine;
using std::uniform_int_distribution;



//using std::vector;

//交换函数
template<class T>
void swap(T &a,T &b){
    T t;
    t = a;
    a = b;
    b = t;
}

//打印函数
template<class T>
void print_vector(const vector<T> &v){
    for(auto it:v){
        cout<<it<<" ";
    }
    cout<<endl;
}

//得到一个随机的数组
template<class T=int>
vector<int> get_a_vec(int len){
    vector<T> v;
    default_random_engine e;
    uniform_int_distribution<int> u(0, 100);
    while (len--){
        v.push_back(u(e));
    }
    return v;
}

//冒泡
template<class T>
void bubble_sort(vector<T> &v, int type = 0){
    int len = v.size();
    T temp = v[0];
    for(int i=0;i<len;i++){
        for(int j=0;j<len - 1 - i;j++){
            switch (type) {
                case 0:
                    if(v[j] < v[j+1])
                        swap(v[j], v[j + 1]);
                    break;
                case -1:
                    if(v[j] > v[j+1])
                        swap(v[j],v[j+1]);
                    break;
            }
        }
    }
}

//选择
template<class T>
void select_sort(vector<T> &v,int type = 0){
    int min;
    for(int i = 0; i < v.size(); i++){
        min = i;
        for(int j = i + 1; j < v.size(); j++){
            switch(type){
                case 0:
                    if(v[j] < v[min])
                        min = j;
                    break;
                case -1:
                    if(v[j] > v[min])
                        min = j;
                    break;
            }
        }
        swap(v[min],v[i]);
    }
}

//插入
template<class T>
void insert_sort(vector<T> &v){
    int right = 0;
    for(int i=1;i<v.size();i++){
            //如果未排序元素比 排序元素大,直接放在队尾
            T temp = v[i];
            int j = i - 1;
//            v[i] 就是未排序元素
        //如果未排序元素比已排序元素小,从后往前扫描已排序队列,向后移位置 直到找到合适的位置
            while(j>=0 && temp < v[j]){
                v[j + 1] = v[j];
                j--;
            }
            //找到合适的位置后 跳出while
            v[j+1] = temp;
    }
}

//希尔排序
template<class T>
void shell_sort(vector<T> &v){

}


//快排
int partition2(vector<int> &v,int left,int right){
    //快排的改进集中在pivot的选取上
    //找到一个基值 将其放到left上  
    //最后再将基值和right交换 (因为最后right就是基值应该待的地方)
    int pivot = left;
    left ++;
    while(true){
        while(left <= right && v[left]< v[pivot]){
            left ++;
        }
        while(right >= left && v[right] > v[pivot]){
            right --;
        }
        if(left >= right){
            break;
        }
        swap(v[left],v[right]);
        left ++;
        right --;
    }
    swap(v[pivot],v[right]);
    pivot = right;
    return pivot;
}

void quick_sort(vector<int> &v, int left, int right){
    if(left>=right){
        return;
    }
    int pivot_index = partition2(v,left,right);
    quick_sort(v,left,pivot_index-1);
    quick_sort(v,pivot_index+1,right);
}
//快排^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值