常用排序算法的比较

#include <iostream>
#include <vector>
#include <random>
#include <time.h>
using namespace std;

/*
2015.5.4 quicksort
*/
void swap(int &a, int &b)
{
    int temp = a;
    a = b;
    b = temp;
}
//分割取下标
int partition_QuickSort(vector<int>& nums, int begin, int end)
{
    if (nums.size() == 0)return -1;
    int pivot = nums[begin];
    int i = begin+1, j = end;
    while (i <= j)
    {
        while (i <=j&&nums[i] < pivot){ ++i; }
        while (j>=i&&nums[j] >= pivot){ --j; }
        if (i <= j)
        {
            swap(nums[i++], nums[j--]);
        }
    }
    swap(nums[begin], nums[j]);
    return j;
}
void quicksort(vector<int>& nums,int begin,int end)
{
    if (begin >=end)return;
    int Sortedindex=partition_QuickSort(nums, begin, end);    
    quicksort(nums, begin, Sortedindex - 1);
    quicksort(nums, Sortedindex + 1, end);
}
void qsort(vector<int>& nums)
{
    quicksort(nums, 0, nums.size() - 1);
}
/*
endofqsort;
*/



/*
bubblesort 2014.5.4
*/
void bubblesort(vector<int> & nums)
{
    for (int  i = 1; i < nums.size(); i++)
    {
        for (int j = 0; j < nums.size()-i; j++)
        {
            if (nums[j]>nums[j + 1])swap(nums[j], nums[j + 1]);
        }        
    }
}
/*
endof bubblesort
*/

/*
heapsort
*/
void sink(vector<int>& ATemp, int i)//i 是下沉index
{
    int j = 2 * i + 1;
    while (j<ATemp.size())
    {
        if (j + 1 < ATemp.size())
        {
            if (ATemp[j] < ATemp[j + 1])++j;//选最da 
        }
        if (ATemp[i] < ATemp[j])
        {
            auto temp = ATemp[i];
            ATemp[i] = ATemp[j];
            ATemp[j] = temp;
            i = j;
            j = 2 * j + 1;
        }
        else
            break;
    }
}
void sink(vector<int>& ATemp, int i,int n)//i 是下沉index
{
    int j = 2 * i + 1;
    while (j<n)
    {
        if (j + 1 < n)
        {
            if (ATemp[j] < ATemp[j + 1])++j;//选最da 
        }
        if (ATemp[i] < ATemp[j])
        {
            auto temp = ATemp[i];
            ATemp[i] = ATemp[j];
            ATemp[j] = temp;
            i = j;
            j = 2 * j + 1;
        }
        else
            break;
    }
}
void swim(vector<int>& A, int i)
{
    int j = (i - 1) / 2;
    while (j >= 0 && i != 0)
    {
        if (A[i] > A[j])
        {
            auto temp = A[j];
            A[j] = A[i];
            A[i] = temp;
            i = j;
            j = (j - 1) / 2;
        }
        else
            break;
    }
}
void insert(vector<int>& A, int num)
{
    A.push_back(num);
    swim(A, A.size() - 1);
}
int deletemax(vector<int> &A)
{
    if (A.size() > 0)
    {
        int temp = A[0];
        A[0] = A[A.size() - 1];
        A.pop_back();
        sink(A, 0);
        return temp;
    }
    else
    {
        return -1;//errrrrrrrrrrro
    }
}
void heapify(vector<int> &A)
{
    for (int i = A.size() / 2 - 1; i >= 0; i--)
    {
        sink(A, i);
    }
}
void heapsort(vector<int> & nums)
{
    heapify(nums);
    int N = nums.size();
    while (N>0)
    {
        swap(nums[0], nums[--N]);
        sink(nums, 0, N);
    }
}
/*
end of heapsort
*/

/*mergesort
2014.5.4
*/
void mergesortUtil(vector<int>& nums, int begin, int end)
{
    if (begin >= end)return;    
    mergesortUtil(nums, begin, begin + (end - begin) / 2);
    mergesortUtil(nums, begin + (end - begin) / 2 + 1, end);
    int secondbegin = begin + (end - begin) / 2 + 1;
    int i = begin;
    int j = secondbegin;
    vector<int> ans;
    while (i <= begin + (end - begin) / 2 && j <= end)
    {
        if (nums[i] < nums[j])
        {
            ans.push_back(nums[i++]);
        }
        else
        {
            ans.push_back(nums[j++]);
        }
    }
    while (i == begin + (end - begin) / 2+1&&j<=end)
    {
        ans.push_back(nums[j++]);
    }
    while (i <= begin + (end - begin) / 2  && j == end+1)
    {
        ans.push_back(nums[i++]);
    }
    int index = 0;
    for (int i = begin; i <= end; i++)
    {
        nums[i] = ans[index++];
    }
}
void mergesort(vector<int> & nums)
{
    mergesortUtil(nums, 0, nums.size() - 1);
}
/*
endof mergesort
*/

/*
inserctionsort
*/

void inserctionsort(vector<int> &nums)
{
    for (int i = 1; i < nums.size(); i++)
    {
        for (int j = i; j >0&&(nums[j]<nums[j-1]); j--)//nums[i]的信息是从j=i获取的
        {
            swap(nums[j], nums[j - 1]);
        }
    }
}
/*
endofinserctionsort
*/


/*
selectionSort
*/
void selectionsort(vector<int>& nums)
{
    for (int i = 0; i < nums.size(); i++)
    {
        int minindex = i;
        for (int j = i+1; j < nums.size(); j++)
        {
            if (nums[minindex] > nums[j])minindex = j;            
        }
        swap(nums[i], nums[minindex]);
    }
}
/*
endofSelectionSort
*/

/*
shellsort
*/
void shellsort(vector<int> & nums)
{
    int N = nums.size();
    int h = 1;
    while (h<N/4)
    {
        h = h * 4 + 1;
    }
    while (h>=1)
    {
        for (int i = h; i < nums.size(); i++)
        {
            for (int j = i; j >= h && (nums[j] < nums[j - h]); j -= h)
                swap(nums[j], nums[j - h]);
        }
        h /= 4;
    }
}




/*
endofshellsort
*/
bool isordered(vector<int>& nums)
{
    for (size_t i = 0; i < nums.size()-1; i++)
    {
        if (nums[i + 1] - nums[i] < 0)return false;
    }
    return  true;
}


vector<int> good_randvec(int number)
{
    static default_random_engine e;
    static uniform_int_distribution<int> u(-2147483648, 2147483647);
    vector<int> ret;
    for (size_t i = 0; i < number; i++)
    {
        ret.push_back(u(e));
    }
    return ret;
}



#define SIZE (100000)
int main()
{
    auto random_vec = good_randvec(SIZE);
    int time1 = clock();
                                  inserctionsort(random_vec);//插入
    int time2 = clock();
    cout << time2 - time1 << endl;
    cout << isordered(random_vec) << endl;


     random_vec = good_randvec(SIZE);
     time1 = clock();
                                     shellsort(random_vec);//希尔
    time2 = clock();
    cout << time2 - time1 << endl;
    cout << isordered(random_vec) << endl;


    random_vec = good_randvec(SIZE);
    time1 = clock();
                                 selectionsort(random_vec);//选择
    time2 = clock();
    cout << time2 - time1 << endl;
    cout << isordered(random_vec) << endl;


     random_vec = good_randvec(SIZE);
     time1 = clock();
                                            bubblesort(random_vec);//冒泡
     time2 = clock();
    cout << time2 - time1 << endl;
    cout << isordered(random_vec) << endl;
    
    

     random_vec = good_randvec(SIZE);
     time1 = clock();
                                         qsort(random_vec);//快排
      time2 = clock();
    cout << time2 - time1 << endl;
    cout << isordered(random_vec) << endl;
    


      random_vec = good_randvec(SIZE);
      time1 = clock();
                                    heapsort(random_vec);//堆排
     time2 = clock();
    cout << time2 - time1 << endl;
    cout << isordered(random_vec) << endl;


    random_vec = good_randvec(SIZE);
    time1 = clock();
                                   mergesort(random_vec);//归并
    time2 = clock();
    cout << time2 - time1 << endl;
    cout << isordered(random_vec) << endl;
    
    system("pause");
    return 0;
}

10W随机数,下面的O(nlogn)还是很给力的。

原文地址:https://www.cnblogs.com/07lyt/p/4477097.html