码迷,mamicode.com
首页 > 编程语言 > 详细

快速排序

时间:2018-05-11 17:33:30      阅读:195      评论:0      收藏:0      [点我收藏+]

标签:快速   info   实现   span   play   while   open   cut   分而治之   

算法概述

一、分而治之

技术分享图片

什么十快速排序算法的最好情况?

每次正好中分:T(N) = O(NlogN)

技术分享图片
void Quicksort(ElementType A[], int N)
{
    pivot = 从A[]中选一个主元;
    将S = { A[] \ pivot } 分成2个独立子集:
        A1 = { a属于S | a≤ pivot } 和
        A2 = { a属于S | a≥ pivot };
    A[] = Quicksort(A1, N1) U { pivot} U Quicksort(A2, N2);
}
伪代码

 

 

实现算法

void Quicksort(ElementType A[], int Left, int Right)
{
    if(Cutoff <= Right-Left) {
        Pivot = Median3(A, Left, Right);
        i = Left; j = Right - 1;
        for( ; ; ) {
            while(A[++i] < Pivot) { }
            while(A[--j] > Pivot) { }
            if(i < j)
                Swap(&A[i], &A[j]);
            else
                break;
        }
        Swap(&A[i], &A[Right-1]);
        Quicksort(A, Left, i-1);
        Quicksort(A, i+1, Right);
    } else
        Insertion_Sort(A+Left, Right-Left+1);
}

void Quick_Sort(ElementType A[], int N)
{
    Quicksort(A, 0, N-1);
}

 

快速排序

标签:快速   info   实现   span   play   while   open   cut   分而治之   

原文地址:https://www.cnblogs.com/ch122633/p/9025283.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!