经典排序算法

六种种排序算法:冒泡排序、选择排序、插入排序、快速排序、合并排序、堆排序。这里包含了所有的测试代码以及相应的测试用例。这里的代码,对于快速排序、合并排序都是使用的递归的算法。这里没有列出堆排序,是因为堆排序可以作为动态集合的排序算法,由下一篇博客来介绍。

#include <iostream>
#include <ctime>
#include <climits>
#include <cstdlib>
#include <algorithm>

#define RESET   "\033[0m"
#define BLACK   "\033[30m"      /* Black */
#define RED     "\033[31m"      /* Red */
#define GREEN   "\033[32m"      /* Green */
#define YELLOW  "\033[33m"      /* Yellow */
#define BLUE    "\033[34m"      /* Blue */
#define MAGENTA "\033[35m"      /* Magenta */
#define CYAN    "\033[36m"      /* Cyan */
#define WHITE   "\033[37m"      /* White */
#define BOLDBLACK   "\033[1m\033[30m"      /* Bold Black */
#define BOLDRED     "\033[1m\033[31m"      /* Bold Red */
#define BOLDGREEN   "\033[1m\033[32m"      /* Bold Green */
#define BOLDYELLOW  "\033[1m\033[33m"      /* Bold Yellow */
#define BOLDBLUE    "\033[1m\033[34m"      /* Bold Blue */
#define BOLDMAGENTA "\033[1m\033[35m"      /* Bold Magenta */
#define BOLDCYAN    "\033[1m\033[36m"      /* Bold Cyan */
#define BOLDWHITE   "\033[1m\033[37m"      /* Bold White */
using namespace std;

class sort_algorithm
{
public:
    void swap(int &a, int &b)
    {
        if(&a==&b) return;
        int tmp=a;
        a=b;
        b=tmp;
    }

    void bubble_sort(int A[], int n)
    {
        if (A==NULL) return;
        for(int i=0;i<n;++i)
        {
            for(int j=1;j<n-i;++j)
            {
                if(A[j-1]>A[j]) swap(A[j-1],A[j]);
            }
        }
    }

    void selection_sort(int A[], int n)
    {
        if (A==NULL) return;
        for(int i =0;i<n;++i)
        {
            int min_index=i;
            for(int j=i+1;j<n;++j)
            {
                if(A[min_index]>A[j]) min_index=j;
            }
            swap(A[i],A[min_index]);
        }
    }

    void insertion_sort(int A[], int n)
    {
        if (A==NULL) return;
        for(int i=1;i<n;++i)
        {
            int tmp=A[i];
            int j=i-1;
            while(j>=0 && A[j]>tmp)
            {
                A[j+1]=A[j];
                --j;
            }
            A[j+1]=tmp;
        }
    }

    int partition(int A[], int n)
    {
        srand(time(NULL));
        int sel=rand()%n;
        swap(A[n-1],A[sel]);
        int j=0;
        for(int i=0;i<n-1;++i)
        {
            if(A[i]<A[n-1]) swap(A[j++],A[i]);
        }
        swap(A[j],A[n-1]);
        return j;
    }

    void quick_sort(int A[], int n)
    {
        if(A==NULL) return;
        if (n>1)
        {
            int mid=partition(A,n);
            quick_sort(A,mid);
            quick_sort(A+mid+1,n-mid-1);
        }
    }

    void merge(int A[], int from, int mid, int to)
    {
        int *left=new int[mid-from+1];
        int *right=new int[to-mid+2];
        for(int i=from;i<mid;++i) left[i-from]=A[i];
        for(int i=mid;i<=to;++i) right[i-mid]=A[i];
        left[mid-from]=right[to-mid+1]=INT_MAX;
        int i1=0,i2=0;
        for(int i=from;i<=to;++i)
        {
            if(left[i1]<right[i2])
                A[i]=left[i1++];
            else
                A[i]=right[i2++];
        }
        delete[] left;
        delete[] right;
    }
    
    void merge_sort(int A[], int n)
    {
        if(A==NULL) return;
        if(n>1)
        {
            int mid=n/2;
            merge_sort(A,mid);
            merge_sort(A+mid,n-mid);
            merge(A,0,mid,n-1);
        }
    }

    void test_bubble()
    {
        cout<<"test_bubble begins..."<<endl;
        int *A1=NULL;
        int A2[]={1};
        const int oA2[]={1};
        int A3[]={5,4,3,2,1};
        const int oA3[]={1,2,3,4,5};
        int A4[]={1,2,3,3};
        const int oA4[]={1,2,3,3};
        int A5[]={};
        const int oA5[]={};
        
        bubble_sort(A1,0);
        match(A1,NULL,0);
        bubble_sort(A2,1);
        match(A2,oA2,1);
        bubble_sort(A3,5);
        match(A3,oA3,5);
        bubble_sort(A4,4);
        match(A4,oA4,4);
        bubble_sort(A5,0);
        match(A5,oA5,0);

        for(int i=0;i<10;++i)
        {
            srand(time(NULL));
            int *A=new int[i];
            int *oA=new int[i];
            for(int j=0;j<i;++j)
            {
                A[j]=rand()%10;
                oA[j]=A[j];
            }
            bubble_sort(A,i);
            sort(oA,oA+i);
            match(A,oA,i);
            delete[] A;
            delete[] oA;
        }
        cout<<"test_bubble ends..."<<endl;
    }

    void test_selection()
    {
        cout<<"test_selection begins..."<<endl;
        int *A1=NULL;
        int A2[]={1};
        const int oA2[]={1};
        int A3[]={5,4,3,2,1};
        const int oA3[]={1,2,3,4,5};
        int A4[]={1,2,3,3};
        const int oA4[]={1,2,3,3};
        int A5[]={};
        const int oA5[]={};
        
        selection_sort(A1,0);
        match(A1,NULL,0);
        selection_sort(A2,1);
        match(A2,oA2,1);
        selection_sort(A3,5);
        match(A3,oA3,5);
        selection_sort(A4,4);
        match(A4,oA4,4);
        selection_sort(A5,0);
        match(A5,oA5,0);

        for(int i=0;i<10;++i)
        {
            srand(time(NULL));
            int *A=new int[i];
            int *oA=new int[i];
            for(int j=0;j<i;++j)
            {
                A[j]=rand()%10;
                oA[j]=A[j];
            }
            selection_sort(A,i);
            sort(oA,oA+i);
            match(A,oA,i);
            delete[] A;
            delete[] oA;
        }
        cout<<"test_selection ends..."<<endl;
    }

    void test_insertion()
    {
        cout<<"test_insertion begins..."<<endl;
        int *A1=NULL;
        int A2[]={1};
        const int oA2[]={1};
        int A3[]={5,4,3,2,1};
        const int oA3[]={1,2,3,4,5};
        int A4[]={1,2,3,3};
        const int oA4[]={1,2,3,3};
        int A5[]={};
        const int oA5[]={};
        
        insertion_sort(A1,0);
        match(A1,NULL,0);
        insertion_sort(A2,1);
        match(A2,oA2,1);
        insertion_sort(A3,5);
        match(A3,oA3,5);
        insertion_sort(A4,4);
        match(A4,oA4,4);
        insertion_sort(A5,0);
        match(A5,oA5,0);

        for(int i=0;i<10;++i)
        {
            srand(time(NULL));
            int *A=new int[i];
            int *oA=new int[i];
            for(int j=0;j<i;++j)
            {
                A[j]=rand()%10;
                oA[j]=A[j];
            }
            insertion_sort(A,i);
            sort(oA,oA+i);
            match(A,oA,i);
            delete[] A;
            delete[] oA;
        }
        cout<<"test_insertion ends..."<<endl;
    }

    void test_quick()
    {
        cout<<"test_quick begins..."<<endl;
        int *A1=NULL;
        int A2[]={1};
        const int oA2[]={1};
        int A3[]={5,4,3,2,1};
        const int oA3[]={1,2,3,4,5};
        int A4[]={1,2,3,3};
        const int oA4[]={1,2,3,3};
        int A5[]={};
        const int oA5[]={};
        
        quick_sort(A1,0);
        match(A1,NULL,0);
        quick_sort(A2,1);
        match(A2,oA2,1);
        quick_sort(A3,5);
        match(A3,oA3,5);
        quick_sort(A4,4);
        match(A4,oA4,4);
        quick_sort(A5,0);
        match(A5,oA5,0);

        for(int i=0;i<10;++i)
        {
            srand(time(NULL));
            int *A=new int[i];
            int *oA=new int[i];
            for(int j=0;j<i;++j)
            {
                A[j]=rand()%10;
                oA[j]=A[j];
            }
            quick_sort(A,i);
            sort(oA,oA+i);
            match(A,oA,i);
            delete[] A;
            delete[] oA;
        }
        cout<<"test_quick ends..."<<endl;
    }

    void test_merge()
    {
        cout<<"test_merge begins..."<<endl;
        int *A1=NULL;
        int A2[]={1};
        const int oA2[]={1};
        int A3[]={5,4,3,2,1};
        const int oA3[]={1,2,3,4,5};
        int A4[]={1,2,3,3};
        const int oA4[]={1,2,3,3};
        int A5[]={};
        const int oA5[]={};
        
        merge_sort(A1,0);
        match(A1,NULL,0);
        merge_sort(A2,1);
        match(A2,oA2,1);
        merge_sort(A3,5);
        match(A3,oA3,5);
        merge_sort(A4,4);
        match(A4,oA4,4);
        merge_sort(A5,0);
        match(A5,oA5,0);

        for(int i=0;i<10;++i)
        {
            srand(time(NULL));
            int *A=new int[i];
            int *oA=new int[i];
            for(int j=0;j<i;++j)
            {
                A[j]=rand()%10;
                oA[j]=A[j];
            }
            merge_sort(A,i);
            sort(oA,oA+i);
            match(A,oA,i);
            delete[] A;
            delete[] oA;
        }
        cout<<"test_merge ends..."<<endl;
    }
    void test()
    {
        test_bubble();
        test_selection();
        test_insertion();
        test_quick();
        test_merge();
    }

    bool match(const int A[], const int B[], int n)
    {
        bool ret=true;
        if (A==NULL && B==NULL) ret=true;
        else if (A==NULL || B==NULL) ret=false;
        else
        {
            for(int i=0;i<n;++i)
                if (A[i]!=B[i]) ret=false;
        }
        if(ret)
            cout<<GREEN<<"SUCCEEDED. ";
        else
            cerr<<RED<<"FAILED. ";
        cout<<"A: {";
        for(int i=0;i<n;++i)
            cout<<A[i]<<',';
        cout<<"}. B: {";
        for(int i=0;i<n;++i)
            cout<<B[i]<<',';
        cout<<"}."<<RESET<<endl;;
        return ret;
    }
};

int main()
{
    sort_algorithm s;
    s.test();
    return 0;
}

你可能感兴趣的:(经典排序算法)