• 十大排序


    冒泡、(插入、希尔)、(选择、堆排)、快速、归并、桶排、基数、计数排序
    sort.h文件
    #ifndef UNTITLED1_SORT_H
    #define UNTITLED1_SORT_H
    #include <iostream>
    #include <vector>
    #include <queue>
    using namespace std;
    struct show_each{
        void operator()(int x){cout<<x<<" ";}
    }se;
    void print(vector<int> v){
        for_each(v.begin(), v.end(), se);
        cout<<endl;
    }
    //十大排序 冒泡、(插入、希尔)、(选择、堆排)、快速、归并、桶排、基数、计数排序
    void buble_sort(vector<int> v){
        for(int i = 0; i < v.size(); i++){
            for(int j = 0; j < v.size() - i - 1; j++){
                if(v[j] > v[j + 1]) swap(v[j], v[j + 1]);
            }
        }
        cout<<"冒泡排序"<<endl;
        print(v);
    }
    //两个插入排序,简单插入
    void insert_sort(vector<int> v){
        for(int i = 1; i < v.size(); i++){
            int cur = v[i];
            int j = i - 1;
            for(; j >= 0; j--){
                if(v[j] > cur)
                    v[j + 1] = v[j];
                else break;
            }
            v[j + 1] = cur;
        }
        cout<<"插入排序"<<endl;
        print(v);
    }
    //希尔排序
    void shell_sort(vector<int> v){
        int n = v.size();
        for(int gap = n / 2; gap >= 1; gap /= 2){
            for(int i = gap; i < n; i++){
                int cur = v[i];
                int j = i;
                while(j - gap >= 0 && v[j - gap] > cur){
                    v[j] = v[j - gap];
                    j -= gap;
                }
                v[j] = cur;
            }
        }
        cout<<"希尔排序"<<endl;
        print(v);
    }
    
    //简单选择排序
    void select_sort(vector<int> v){
        int n = v.size();
        for(int i = 0; i < v.size(); i++){
            int mini = i, minx = v[i];
            for(int j = i + 1; j < v.size(); j++){
                if(minx > v[j]){
                    mini = j; minx = v[j];
                }
            }
            swap(v[i], v[mini]);
        }
        cout<<"选择排序"<<endl;
        print(v);
    }
    
    //堆排序
    void heap_sort(vector<int> v){ //大根堆,从小到大排序 int n = v.size(); for(int i = n - 1; i >= 1; i--){ for(int j = (i - 1) / 2; j >= 0; j--){ if(j * 2 + 2 > i){ if(v[j * 2 + 1] > v[j]){ swap(v[j * 2 + 1], v[j]); } } else { if(v[j * 2 + 1] >= v[j] && v[j * 2 + 1] >= v[j * 2 + 2]){ swap(v[j], v[j * 2 + 1]); } else if(v[j * 2 + 2] >= v[j] && v[j * 2 + 1] <= v[j * 2 + 2]){ swap(v[j], v[j * 2 + 2]); } } } swap(v[0], v[i]); } cout<<"堆排序"<<endl; print(v); } void quick_sort(vector<int> v){ queue<pair<int, int>> q; int n = v.size(); q.push(make_pair(0, n - 1)); while(!q.empty()){ pair<int, int> cur = q.front(); q.pop(); int i = cur.first, j = cur.second; int key = v[i]; while(i < j){ while(i < j && v[j] >= key)--j; v[i] = v[j]; while(i < j && v[i] <= key)++i; v[j] = v[i]; } v[i] = key; if(cur.first < i - 1) q.push(make_pair(cur.first, i - 1)); if(i + 1 < cur.second) q.push(make_pair(i + 1, cur.second)); } cout<<"快速排序"<<endl; print(v); } //迭代归并排序 void merge_sort(vector<int> v){ int n = v.size(); vector<int> tmp(n); for(int gap = 1; gap < n; gap *= 2){ for(int i = 0; i < n; i += gap * 2){ //合并[i, i + gap - 1] 和 [i + gap, i + 2 * gap - 1] int l = i, a = i, b = i + gap; while(a < i + gap && b < i + 2 * gap && b < n){ if(v[a] > v[b]) tmp[l++] = v[b++]; else tmp[l++] = v[a++]; } while(a < i + gap)tmp[l++] = v[a++]; while(b < i + 2 * gap && b < n)tmp[l++] = v[b++]; } v = tmp; } cout<<"归并排序"<<endl; print(v); } void bucket_sort(vector<int> v){ //先分到一定个数的桶内,再对每个桶排序 int BUCKETSIZE = 5; vector<vector<int>> buckets(BUCKETSIZE + 1); int l = *min_element(v.begin(), v.end()); int r = *max_element(v.begin(), v.end()); int bucknum = (r - l) / BUCKETSIZE; for(int& x : v){ buckets[(x - l) / bucknum].push_back(x); } int t = 0; for(int i = 0; i <= BUCKETSIZE; i++){ sort(buckets[i].begin(), buckets[i].end()); for(int j = 0; j < buckets[i].size(); j++){ v[t++] = buckets[i][j]; } } cout<<"桶排序"<<endl; print(v); } void radix_sort(vector<int> v){ //基数排序 int n = v.size(); vector<vector<int>> counter(10); int mod = 10; int dev = 1; while(counter[0].size() != n){ for(int i = 0; i < 10; i++) counter[i].clear(); for(int i = 0; i < n; i++){ counter[v[i] / dev % mod].push_back(v[i]); } int t = 0; for(int i = 0; i < counter.size(); i++){ for(int j = 0; j < counter[i].size(); j++){ v[t++] = counter[i][j]; } } dev *= 10; } cout<<"基数排序"<<endl; print(v); } void count_sort(vector<int> v){ int l = *min_element(v.begin(), v.end()); int r = *max_element(v.begin(), v.end()); vector<int> cnt(r - l + 1, 0); for(int x : v){ cnt[x - l]++; } int t = 0; for(int i = 0; i < r - l + 1; i++){ while(cnt[i]--){ v[t++] = i + l; } } cout<<"计数排序"<<endl; print(v); } #endif //UNTITLED1_SORT_H
  • 相关阅读:
    一类分治问题
    [POI2005]DWU-Double-row
    [SCOI2007]降雨量
    [POI2007]ODW-Weights(贪心)
    bzoj3427小P的牧场(斜率优化dp)
    UVA10559 Blocks(区间dp)
    LOJ6089 小Y的背包计数问题(根号优化背包)
    [APIO2008]免费道路(生成树)
    bzoj4383(拓扑排序)
    [HEOI2014]平衡(整数划分数)
  • 原文地址:https://www.cnblogs.com/Dancing-Fairy/p/15151860.html
Copyright © 2020-2023  润新知