八种常见的排序算法

算法性质

算法稳定性时间复杂度
计数排序稳定O(n)
基数排序稳定O(n)
桶排序不稳定O(n)
插入排序稳定O(n^2)
归并排序稳定O(nlogn)
快速排序不稳定O(nlogn)
希尔排序不稳定O(n^2)
堆排序不稳定O(nlogn)

代码实现

#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;
    }
}
/** * 快速排序 **/
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);
    }
}
};

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::quick_sort(data);
    //sort::insert_sort(data);
    //sort::shell_sort(data);
    sort::heap_sort(data);
    for (auto ele : data) cout<<"\t"<<ele;
    cout<<endl;
    return 0;
}
点赞

发表评论

电子邮件地址不会被公开。 必填项已用*标注