首页 > 其他 > 详细

快速排序

时间:2014-07-16 18:51:45      阅读:383      评论:0      收藏:0      [点我收藏+]

快速排序是从起泡排序改进而得的一种“交换”排序方法。它的基本思想是通过一趟排序将待排记录分割成相邻的两个区域,其中一个区域中的元素均比另一个区域中元素小(区域内不见得有序)则可对这两个区域内的元素进行再排序,以达到整个序列有序。

//快速排序
#include<stdio.h>
int Partition(int a[] , int low , int high) {  //一次划分
    int pivotkey = a[low] ;
    while(low < high)   {
        while(low < high && a[high] >= pivotkey)
            high-- ;
        if(low < high)
            a[low++] = a[high] ;
        while(low < high && a[low] <= pivotkey)
            low++ ;
        if(low < high)
            a[high--] = a[low] ;
    }
    a[low] = pivotkey ;
    return low ;
}
void Qsort(int a[] , int s , int t) {
    if(s < t)   {
        int pivotloc = Partition(a,s,t) ;//一次划分  通过一趟排序将待排元素分割成两个相邻的区域,一个区域中元素都比pivotloc小,另一个区域元素都比pivotloc大
        Qsort(a,s,pivotloc-1) ;         //左边递归   分别对两个区域的元素再排序
        Qsort(a,pivotloc+1,t) ;         //右边递归   分别对两个区域的元素再排序
    }
}
int main()  {
    int a[] = {49,38,65,97,76,13,27,49} ;
    Qsort(a,0,7);
    int i ;
    for(i = 0 ; i < 8 ; i++)
        printf("%d ",a[i]);
    printf("\n");
    return 0 ;
}

 

 

 

快速排序,布布扣,bubuko.com

快速排序

原文:http://www.cnblogs.com/scottding/p/3845009.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!