算法学习——六个经典排序算法

六个经典排序算法

最近在开始看算法书,从最经典的排序算法开始,用C++实现了六个经典的排序算法,记录下来,以便日后翻阅。

Show me the Code

#include <iostream>
#include <vector>
using namespace std;

class Sort{
public:
    vector<int> A;
    Sort(vector<int> A): A(A){}

    void display(vector<int> &A){
        for(auto c:A) cout<<c;
        cout<<endl;
    }

    // Bubble Sort
    void bubble_sort(vector<int> &A) {
        // If the vector is empty
        if(A.empty()) return ;

            // If the vector is not empty
        else{
            for(int i=0; i<A.size()-1; ++i){
                for(int j=0; j<A.size()-1-i; ++j){
                    if(A[j] > A[j+1]){
                        swap(A[j], A[j+1]);
                    }
                }
            }
        }
    }

    // Selection Sort
    void selection_sort(vector<int> &A) {
        // If the vector is empty
        if(A.empty()) return ;

            // If the vector is not empty
        else{
            for(int i=0; i<A.size()-1; ++i){
                int min_index = i;
                for(int j=i+1; j<A.size(); ++j){
                    // Get the index of the min value
                    if(A[j] < A[min_index]){
                        min_index = j;
                    }
                }
                swap(A[i], A[min_index]);
            }
        }
    }

    // Insertion Sort
    void insertion_sort(vector<int> &A) {
        // If the vector is empty
        if(A.empty()) return ;

            // If the vector is not empty
        else{
            for(int j=1; j<A.size(); ++j){
                int key = A[j]; // waiting for sort
                // start from the previous one of the key
                int i = j-1;
                while(i>=0 && key<A[i]){
                    A[i+1] = A[i]; // move one by one
                    --i;
                }
                A[i+1] = key; // insert
            }
        }
    }

    // Quik sort
    void quik_sort(vector<int> &A, int left, int right){
        if(left>=right) return ;

        int i = left, j = right;
        int pivot = A[left];
        while(i<j){
            while(i<j && A[j]>=pivot) j--;
            swap(A[i], A[j]);
            while(i<j && A[i]<=pivot) i++;
            swap(A[i], A[j]);
        }
        A[i] = pivot;

        quik_sort(A, left, i-1);
        quik_sort(A, i+1, right);
    }


    // Merge sort
    void merge_sort(vector<int> &A, vector<int> &Reg, int left, int right){
        if(left >= right) return ;

        int len = right-left;
        int mid = left + (len>>1);
        int left1 = left, right1 = mid;
        int left2 = mid+1, right2 = right;

        merge_sort(A, Reg, left1, right1);
        merge_sort(A, Reg, left2, right2);

        int k = left;
        while(left1<=right1 && left2<=right2){
            Reg[k++] = (A[left1]<A[left2] ? A[left1++] : A[left2++]);
        }
        while(left1<=right1){
            Reg[k++] = A[left1++];
        }
        while(left2<=right2){
            Reg[k++] = A[left2++];
        }

        for(k = left; k<=right; k++){
            A[k] = Reg[k];
        }
    }

    // Heap Sort
    void max_heapify(vector<int> &A, int start, int end){
        int dad = start;
        int son = dad * 2 + 1;

        while(son <= end){
            if(son+1 <= end && A[son]<A[son+1]) son++;
            if(A[dad] >= A[son]) break;
            else{
                swap(A[dad], A[son]);
                dad = son;
                son = dad * 2 + 1;
            }
        }
    }

    void heap_sort(vector<int> &A){
        for(int i = A.size()/2-1; i>=0; --i){
            // start from the last dad
            max_heapify(A, i, A.size()-1);
        }

        for(int i=A.size()-1; i>0; --i){
            swap(A[0], A[i]);// swap the max and the back
            max_heapify(A, 0, i-1);
        }
    }
};



int main() {
    vector<int> A{3, 5, 1, 4, 2};

    Sort sort_test(A);
    cout<<"Origin: "<<endl;
    sort_test.display(A);

    cout<<"After bubble sort: "<<endl;
    vector<int> bubble_temp(A);
    sort_test.bubble_sort(bubble_temp);
    sort_test.display(bubble_temp);

    cout<<"After selection sort: "<<endl;
    vector<int> selection_temp(A);
    sort_test.selection_sort(selection_temp);
    sort_test.display(selection_temp);

    cout<<"After insertion sort: "<<endl;
    vector<int> insertion_temp(A);
    sort_test.insertion_sort(insertion_temp);
    sort_test.display(insertion_temp);

    cout<<"After quik sort: "<<endl;
    vector<int> quik_temp(A);
    sort_test.quik_sort(quik_temp, 0, quik_temp.size()-1);
    sort_test.display(quik_temp);

    cout<<"After merge sort: "<<endl;
    vector<int> merge_temp(A), merge_reg(A.size());
    sort_test.merge_sort(merge_temp, merge_reg, 0, merge_temp.size()-1);
    sort_test.display(merge_temp);

    cout<<"After heap sort: "<<endl;
    vector<int> heap_temp(A);
    sort_test.heap_sort(heap_temp);
    sort_test.display(heap_temp);

    return 0;
}

Result

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值