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

六个经典排序算法

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

  • 时间复杂度O(N*N)
    • 冒泡排序
    • 选择排序
    • 插入排序
  • 时间复杂度O(NlgN)
    • 快排 in detail
    • 合并排序
    • 堆排序 in detail

Show me the Code

#include 
#include 
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<cout<// 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; i1; ++i){
                for(int j=0; j1-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; i1; ++i){
                int min_index = i;
                for(int j=i+1; 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; jint key = A[j]; // waiting for sort
                // start from the previous one of the key
                int i = j-1;
                while(i>=0 && key1] = 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(iwhile(i=pivot) j--;
            swap(A[i], A[j]);
            while(i1);
        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]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]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: "<cout<<"After bubble sort: "<vector<int> bubble_temp(A);
    sort_test.bubble_sort(bubble_temp);
    sort_test.display(bubble_temp);

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

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

    cout<<"After quik sort: "<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: "<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: "<vector<int> heap_temp(A);
    sort_test.heap_sort(heap_temp);
    sort_test.display(heap_temp);

    return 0;
}

算法学习——六个经典排序算法_第1张图片

你可能感兴趣的:(算法)