快速排序(QuickSort)

QuickSort

一  Introduction
 
   快速排序的最坏运行时间是Θ(n²);但在实践上它常常是最好的算法,因为它在平均情况下的期望运行时间是Θ(n lg n),而且隐藏在Θ(n lg n)里面的常量因子是很小的。同时,它是一种原地排序算法,在虚拟内存上也工作良好。
   另外,通过随机函数可以极大降低最坏情况的出现概率。

二  算法描述
 
   快速排序算法和归并排序一样,是一种基于分治思想(divide-and-conquer)的排序算法。以下是对数组A[p...r]中的元素进行快速排序的过程。

   Divide: Partition (rearrange) the array A[p ‥ r] into two (possibly empty) subarrays A[p ‥ q - 1] and A[q + 1 ‥ r] such that each element of A[p ‥ q - 1] is less than or equal to A[q], which is, in turn, less than or equal to each element of A[q + 1 ‥ r]. Compute the index q as part of this partitioning procedure.

   Conquer: Sort the two subarrays A[p ‥ q -1] and A[q +1 ‥ r] by recursive calls to quicksort.

   Combine: Since the subarrays are sorted in place, no work is needed to combine them: the entire array A[p ‥ r] is now sorted.

下面是快速排序算法的伪代码:

QUICKSORT(A, p, r)
1 if p < r
2    pivot = RANDOM(p,r)
3    then q ← PARTITION(A, p, r,pivot)
4         QUICKSORT(A, p, q - 1)
5         QUICKSORT(A, q + 1, r)


在这个算法中,PARTITION()是关键的一个方法,它把A[pivot]放在数组中的正确位置上,并将数组划分成两个子数组。左边的子数组中的任一个元素都小于A[pivot],右边的子数组中的任一个元素都不小于A[pivot]。以下是PARTITION()方法的伪代码:

PARTITION(A, p, r,pivot)
1  swap(A, r, pivot)
2  x ← A[r]
3  i ← p - 1
4  for j ← p to r - 1
5       do if A[j] ≤ x
6             then i ← i + 1
7                  exchange A[i] ↔ A[j]
8  swap(A, i + 1, r)
9  return i + 1

三  分析

略。

四  代码清单

#include <time.h>
#include <cstdlib>
#include <iostream>

using namespace std;
//函数声明,以便在main函数里调用。
void quickSort(int [],int,int);

int main()
{
    cout << "main() run." << endl;
    srand(time(0));
    int aryTest[10000];
    int iNumberCounts = 10;

    while (iNumberCounts)
    {
        cout << "The numbers to be sorted are: (" << iNumberCounts << ")"  << endl;
        for (int i = 0; i < iNumberCounts; i++)
        {//生成随机数
            aryTest[i] = rand() % 65535;
            cout << aryTest[i] << "  ";
        }
        cout << endl;

  //对生成的随机数组调用快速排序算法。
        quickSort(aryTest,0,iNumberCounts - 1);

        bool bCorrect = true;
        cout << "The sorted numbers are:" << endl;
        for(int i = 0; i < iNumberCounts; i++)
        {//输出并检查排序結果。
            cout << aryTest[i] << "  ";
            if (i < iNumberCounts - 2)
            {
                if(aryTest[i] > aryTest[i + 1])
                {
                    bCorrect = false;
                    break;
                }
            }
        }

        if (bCorrect)
        {
            cout << endl << "OK!The result is correct! "<< endl << endl;
        }
        else
        {
            cout << endl << "The result is wrong! "<< endl << endl;
        }


        cout << "Input count of numbers:" << endl;
        cin >> iNumberCounts;
    }

    return 0;
}


void swap(int intArray[], int left, int right)
{
    if (left == right)
    {
        return;
    }
    intArray[left] = intArray[left] +  intArray[right];
    intArray[right] = intArray[left] - intArray[right];
    intArray[left] = intArray[left] - intArray[right];
}





int partition(int intArray[], int begin, int end, int pivot)
{
    swap(intArray, end, pivot);
    int i = begin - 1;
    int iPivot = intArray[end];
    for (int j = begin; j < end ; j++)
    {
        if (intArray[j] < iPivot)
        {
            i++;
            swap(intArray ,i ,j);
        }
    }
    swap(intArray, i + 1, end);
    return (i + 1);
}



void quickSort(int intArray[], int begin, int end)
{
    if (begin < end)
    {
        srand(time(0));
        int pivot = begin + rand() % (end - begin + 1);
        int p = partition(intArray, begin, end, pivot);
        quickSort(intArray, begin, p - 1);
        quickSort(intArray, p + 1, end);
    }
}


五  结语

略。

你可能感兴趣的:(Quicksort)