史上最强排序算法合集

/************************************************************************* > File Name: sort_v2.cpp > Author: ma6174 > Mail: [email protected] > Created Time: 一 5/22 19:29:07 2017 ************************************************************************/
#include<iostream>
#include<vector>

using namespace std;

namespace sort{
/** * 计数排序 **/
void count_sort(vector<int>& in) {
    int max_val = 0;
    for (auto ele : in) {
        if (ele < 0) return;
        max_val = max(max_val, ele);
    }
    vector<int> counter(max_val, 0);
    for (auto ele : in) counter[ele] += 1;
    int cur = 0;
    for (int i = 0; i < counter.size(); ++i) {
        if (counter[i] != 0) {
            int tmp = cur;
            cur += counter[i];
            while(tmp < cur) in[tmp++] = i;
        }
    }
}
/** * 基数排序 **/
int get_digit(int n, int pos) {
    while (pos > 0) {
        n /= 10;
        pos--;
    }
    return n % 10;
}
int get_digit_length(int n) {
    int len = 0;
    while (n > 0) {
        n /= 10;
        len++;
    }
    return len;
}
void radix_sort(vector<int>& in) {
    int max_len = 0;
    for (auto ele : in) max_len = max(max_len, get_digit_length(ele));
    for (int k = 0; k < max_len; ++k) {
        int count[10] = {0};
        for (auto ele : in) {
            int radix = get_digit(ele, k);
            count[radix]++;
        }
        int pos[10] = {0};
        for (int i = 1; i < 10; ++i) pos[i] = pos[i - 1] + count[i - 1];
        vector<int> in_tmp(in.begin(), in.end());
        for (auto ele : in_tmp) {
            int radix = get_digit(ele, k);
            in[pos[radix]++] = ele;
        }
        cout<<">>Loop:"<<k<<"<<"<<endl;
        for (auto ele : in) cout<<ele<<",";
        cout<<endl;
    }
}
/** * 桶排序 **/
void insert(vector<int>& in, int ele) {
    in.push_back(0);
    int i = in.size() - 2;
    while (i >= 0 && in[i] > ele) {
        in[i + 1] = in[i];
        i--;
    }
    in[i + 1] = ele;
}
int get_bucket_index(int ele, int min_val, int max_val, int bucket_num) {
    int bucket_range = (max_val + 1 - min_val) / bucket_num;
    int ret = (ele - min_val) / bucket_range;
    return ret;
}
void bucket_sort(vector<int>& in, int bucket_num = 10) {
    vector<vector<int> > bucket(bucket_num + 1, vector<int>());
    int max_val = numeric_limits<int>::min();
    int min_val = numeric_limits<int>::min();
    for (auto ele : in) {
        min_val = min(ele, min_val);
        max_val = max(ele, max_val);
    }
    for (auto ele : in) {
        int key = get_bucket_index(ele, min_val, max_val, bucket_num);
        insert(bucket[key], ele);
    }
    int j = 0;
    for (int i = 0; i < bucket_num + 1; ++i) {
        for (auto ele : bucket[i]) in[j++] = ele; 
    }
}
/** * 归并排序 **/
vector<int> merge_sort(vector<int>& in, int beg, int end) {
    vector<int> ret;
    if (beg >= end) return ret;
    if (beg == end - 1) {
        ret.push_back(in[beg]);
        return ret;
    }
    int mid = (end + beg) / 2;
    auto left = merge_sort(in, beg, mid);
    auto right = merge_sort(in, mid, end);
    int i = 0, j = 0;
    while (i < left.size() && j < right.size()) {
        if (left[i] <= right[j]) {
            ret.push_back(left[i++]);
        } else {
            ret.push_back(right[j++]);
        }
    }
    while (i < left.size()) {
        ret.push_back(left[i++]);
    }
    while (j < right.size()) {
        ret.push_back(right[j++]);
    }
    return ret;
}
void merge_sort(vector<int>& in) {
    auto ret = merge_sort(in, 0, in.size());
    int i = 0;
    for (auto ele : ret) {
        in[i++] = ele;
    }
}
//并归排序迭代版
void merge_sort_II(vector<int>& in) {
    int seg_len = 1;
    while (seg_len < in.size()) {
        vector<int> in_tmp = in;
        int seg_num = in_tmp.size() / seg_len;
        seg_num = (in_tmp.size() % seg_len == 0 ? seg_num : seg_num + 1);
        for (int i = 0; i < seg_num; i += 2) {
            int li = seg_len * i;
            int ri = seg_len * i + seg_len;
            vector<int> tmp;
            while (li < in_tmp.size() && li < ri && ri < in_tmp.size() && ri < seg_len*(i + 2)) {
                if (in_tmp[li] <= in_tmp[ri]) {
                    tmp.push_back(in_tmp[li++]);
                } else {
                    tmp.push_back(in_tmp[ri++]);
                }
            }
            while (li < in_tmp.size() && li < ri) tmp.push_back(in_tmp[li++]);
            while (ri < in_tmp.size() && ri < seg_len*(i + 2)) tmp.push_back(in_tmp[ri++]);
            int j = seg_len * i;
            for (auto ele : tmp) in[j++] = ele;
        }
        seg_len *= 2;
    }
}
/** * 快速排序 **/
int devide(vector<int>& in, int beg, int end) {
    int pivot = in[beg];
    int e = end - 1, b = beg;
    while (b < e) {
        while (e > b && in[e] >= pivot) e--;
        in[b] = in[e];
        while (b < e && in[b] < pivot) b++;
        in[e] = in[b];
    }
    in[b] = pivot;
    return b;
}
void quick_sort_seg(vector<int>& in, int beg, int end) {
    if (beg < end) {
        int mid = devide(in, beg, end);
        quick_sort_seg(in, beg, mid);
        quick_sort_seg(in, mid + 1, end);
    }
}
void quick_sort(vector<int>& in) {
   quick_sort_seg(in, 0, (int)in.size()); 
}

/** * 直接插入排序 **/
void insert_sort(vector<int>& in) {
    for (int i = 1; i < in.size(); ++i) {
        int tmp = in[i];
        int j = i - 1;
        while (j >= 0 && in[j] > tmp) {
            in[j + 1] = in[j];
            j--;
        }
        in[j + 1] = tmp;
    }
}
/** * 希尔排序 **/
void shell_sort(vector<int>& in) {
    vector<int> steps = {16, 8, 4, 2, 1};
    for (auto step : steps) {
        for (int i = step; i < in.size(); i += step) {
            int tmp = in[i];
            int j = i - step;
            while (j >= 0 && in[j] > tmp) {
                in[j + step] = in[j];
                j -= step;
            }
            in[j + step] = tmp;
        }
    }
}
/** * 堆排序 **/
int l_child(int parent) {
    return ((parent + 1)<<1) - 1;
}
int r_child(int parent) {
    return (parent + 1)<<1;
}
void heapify(vector<int>& in, int top, int end) {
    if (top >= end) return;
    int lc_index = l_child(top);
    int rc_index = r_child(top);
    if (lc_index >= end) lc_index = top;
    if (rc_index >= end) rc_index = top;
    int max_index = top;
    if (in[lc_index] > in[max_index]) max_index = lc_index;
    if (in[rc_index] > in[max_index]) max_index = rc_index;
    if (max_index == top) return;
    swap(in[max_index], in[top]);
    heapify(in, max_index, end);
}
void heap_sort(vector<int>& in) {
    for (int i = in.size() / 2; i >= 0; --i) heapify(in, i, in.size());
    int len = in.size();
    while (len > 0) {
        swap(in[0], in[len - 1]);
        len -= 1;
        heapify(in, 0, len);
    }
}
/** * 冒泡排序 **/
void bubble_sort(vector<int>& in) {
    for (int i = in.size(); i > 0; --i) {
        for (int j = 1; j < i; ++j) {
            if (in[j] < in[j - 1]) {
                swap(in[j], in[j - 1]);
            }
        }
    }
}
/** * 选择排序 **/
void select_sort(vector<int>& in){
    for (int i = in.size(); i > 0; --i) {
        int index = 0;
        int tmp = in[0];
        for (int j = 0; j < i; ++j) {
            if (tmp < in[j]) {
                tmp = in[j];
                index = j;
            }
        }
        swap(in[index], in[i - 1]);
    }
}
};

int main() {
    int n = 0;
    cin>>n;
    vector<int> data;
    for (int i = 1; i <= n; ++i) {
        int tmp;
        cin>>tmp;
        data.push_back(tmp);
    }
    //sort::count_sort(data);
    //sort::radix_sort(data);
    //sort::bucket_sort(data);
    //sort::merge_sort(data);
    //sort::merge_sort_II(data);
    //sort::quick_sort(data);
    //sort::insert_sort(data);
    //sort::shell_sort(data);
    //sort::heap_sort(data);
    //sort::bubble_sort(data);
    //sort::select_sort(data);
    for (auto ele : data) cout<<"\t"<<ele;
    cout<<endl;
    return 0;
}
点赞