随笔 - 70  文章 - 160  trackbacks - 0

公告:
知识共享许可协议
本博客采用知识共享署名 2.5 中国大陆许可协议进行许可。本博客版权归作者所有,欢迎转载,但未经作者同意不得随机删除文章任何内容,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。 具体操作方式可参考此处。如您有任何疑问或者授权方面的协商,请给我留言。

常用链接

留言簿(8)

随笔档案

文章档案

搜索

  •  

积分与排名

  • 积分 - 176398
  • 排名 - 146

最新评论

阅读排行榜

评论排行榜

推荐先看看前言:http://www.cppblog.com/tanky-woo/archive/2011/04/09/143794.html

其实这一篇我老早就写过了,只不过最近在总结《算法导论》,而第七章就是快速排序,我当初总结的快排也是根据算法导论来的,为了方便大家阅读,我在这里把曾经写过的重新再贴一遍。


前几天写过一个堆排序的文章(http://www.wutianqi.com/?p=1820),里面谢了很多讲解和代码注释,个人感觉快速排序不是很难,所以不想写讲解,也不需要写注释,大家如果不明白什么是快速排序,可以去看下文章最后我推荐的几个链接。

 

我查过网上很多关于快排的文章和代码,但是基本都是最原始的快排,即霍尔(Hoare)快排。想必大家也没有注意这些,我准备把霍尔快排,算法导论上的快排和随机化快排的代码一起发出来,供大家对比与学习,欢迎大家和我探讨

代码一.霍尔快排:

/*
 * Author: Tanky Woo
 * Blog:   www.WuTianQi.com
 * Note:   快速排序版本1 --- Hoare-Partition
 
*/
 
#include 
<iostream>
using namespace std;
 
int num;
 
void swap(int &a, int &b)
{
    
int temp = a;
    a 
= b;
    b 
= temp;
}
 
void PrintArray(int *arr)
{
    
for(int i=1; i<=num; ++i)
        cout 
<< arr[i] << " ";
    cout 
<< endl;
}
 
int Partition1(int *arr, int beg, int end)
{
    
int low = beg, high = end;
    
int sentinel = arr[beg];
    
while(low < high)
    {
        
while(low<high && arr[high]>=sentinel)
            
--high;
        arr[low] 
= arr[high];
        
while(low<high && arr[low]<=sentinel)
            
++low;
        arr[high] 
= arr[low];
    }
    arr[low] 
= sentinel;
 
    cout 
<< "排序过程:";
    PrintArray(arr);
    
return low;
}
 
void QuickSort(int *arr, int beg, int end)
{
    
if(beg < end)
    {
        
int pivot = Partition1(arr, beg, end);
        QuickSort(arr, beg, pivot
-1);
        QuickSort(arr, pivot
+1, end);
    }
}
 
int main()
{
    
int arr[100];
    cout 
<< "Input the num of the elements:\n";
    cin 
>> num;
    cout 
<< "Input the elements:\n";
    
for(int i=1; i<=num; ++i)
        cin 
>> arr[i];
    QuickSort(arr, 
1, num);
    cout 
<< "最后结果:";
    PrintArray(arr);
    
return 0;
}

代码二.《算法导论》里讲的快排:

/*
 * Author: Tanky Woo
 * Blog:   www.WuTianQi.com
 * Note:   快速排序版本2---《算法导论》
 
*/
 
#include 
<iostream>
using namespace std;
 
int num;
 
void swap(int &a, int &b)
{
    
int temp = a;
    a 
= b;
    b 
= temp;
}
 
void PrintArray(int *arr)
{
    
for(int i=1; i<=num; ++i)
        cout 
<< arr[i] << " ";
    cout 
<< endl;
}
 
int Partition2(int *arr, int beg, int end)
{
    
int sentinel = arr[end];
    
int i = beg-1;
    
for(int j=beg; j<=end-1++j)
    {
        
if(arr[j] <= sentinel)
        {
            i
++;
            swap(arr[i], arr[j]);
        }
    }
    swap(arr[i
+1], arr[end]);
 
    cout 
<< "排序过程:";
    PrintArray(arr);
    
return i+1;
}
 
void QuickSort(int *arr, int beg, int end)
{
    
if(beg < end)
    {
        
int pivot = Partition2(arr, beg, end);
        QuickSort(arr, beg, pivot
-1);
        QuickSort(arr, pivot
+1, end);
    }
}
 
int main()
{
    
int arr[100];
    cout 
<< "Input the num of the elements:\n";
    cin 
>> num;
    cout 
<< "Input the elements:\n";
    
for(int i=1; i<=num; ++i)
        cin 
>> arr[i];
    QuickSort(arr, 
1, num);
    cout 
<< "最后结果:";
    PrintArray(arr);
    
return 0;
}

代码三.随机快排:

/*
 * Author: Tanky Woo
 * Blog:   www.WuTianQi.com
 * Note:   快速排序版本3 --- 随机化版本
 * 解决待排序元素相差很大的情况
 
*/
 
 
#include 
<iostream>
#include 
<cstdlib>
using namespace std;
 
int num;
 
void swap(int &a, int &b)
{
    
int temp = a;
    a 
= b;
    b 
= temp;
}
 
void PrintArray(int *arr)
{
    
for(int i=1; i<=num; ++i)
        cout 
<< arr[i] << " ";
    cout 
<< endl;
}
 
int Partition3(int *arr, int beg, int end)
{
    
int sentinel = arr[end];
    
int i = beg-1;
    
for(int j=beg; j<=end-1++j)
    {
        
if(arr[j] <= sentinel)
        {
            i
++;
            swap(arr[i], arr[j]);
        }
    }
    swap(arr[i
+1], arr[end]);
 
    cout 
<< "排序过程:";
    PrintArray(arr);
    
return i+1;
}
 
int RandomPartition(int *arr, int beg, int end)
{
    
int i = beg + rand() % (end-beg+1);
    swap(arr[i], arr[end]);
    
return Partition3(arr, beg, end);
}
 
 
void RandomQuickSort(int *arr, int beg, int end)
{
    
if(beg < end)
    {
        
int pivot = RandomPartition(arr, beg, end);
        RandomQuickSort(arr, beg, pivot
-1);
        RandomQuickSort(arr, pivot
+1, end);
    }
}
 
int main()
{
    
int arr[100];
    cout 
<< "Input the num of the elements:\n";
    cin 
>> num;
    cout 
<< "Input the elements:\n";
    
for(int i=1; i<=num; ++i)
        cin 
>> arr[i];
    RandomQuickSort(arr, 
1, num);
    cout 
<< "最后结果:";
    PrintArray(arr);
    
return 0;
}

最后,我想说下,随机化的快排一般适用于待排序的数据之间相差较大的情况下。

这里给出几篇网上讲得不错的文章:

1.http://bbs.chinaunix.net/viewthread.php?tid=1011316

算是一个讨论帖。很给力!

2.http://www.javaeye.com/topic/561718

讲得比较详细,不过代码是Java的。

3.http://tayoto.blog.hexun.com/25048556_d.html

4.http://www.360doc.com/content/10/1106/11/1317564_67067368.shtml

概念上很详细。

5.http://blog.csdn.net/wssxy/archive/2008/12/05/3448642.aspx

一篇讲快排的佳作!

6.http://www.cnblogs.com/chinazhangjie/archive/2010/12/09/1901491.html

小杰的文章,用C++模板类写的。大家可以去学习学习。


在我独立博客上的原文:http://www.wutianqi.com/?p=2368
欢迎大家互相学习,互相探讨。
posted on 2011-04-19 18:08 Tanky Woo 阅读(2004) 评论(7)  编辑 收藏 引用

FeedBack:
# re: 《算法导论》学习总结 — 6.第七章 快速排序 2011-04-20 09:28 Cunch
LZ讲的很好啊,资料确实很全啊,想问下LZ平时是如何收集资料的。  回复  更多评论
  
# re: 《算法导论》学习总结 — 6.第七章 快速排序 2011-04-20 11:56 Tanky Woo
@Cunch
收集资料?我一般遇到不会的算法就在网上各种搜,一般找个N个讲解的文章差不多就能看懂了吧。。。实在不行再让我朋友一起看,然后讨论下。  回复  更多评论
  
# re: 《算法导论》学习总结 — 6.第七章 快速排序 2011-04-22 21:57 tianxiaogang12
最近每天看一个算法,lz继续总结  回复  更多评论
  
# re: 《算法导论》学习总结 — 6.第七章 快速排序 2011-04-23 08:27 Tanky Woo
@tianxiaogang12
嗯,谢谢你的支持!^_^  回复  更多评论
  
# re: 《算法导论》学习总结 — 6.第七章 快速排序 2011-04-25 21:35 KingJ
很感激lz的这些文章,上学期买了这本书看了一点就放弃了,偶然看到lz的博客,看到这一系列的文章,决定认真的从新学起,现在慢慢体会到本书确实有点“圣经”的感觉,lz总结的很好,今天终于注册上了,顶一下,持续关注ing~  回复  更多评论
  
# re: 《算法导论》学习总结 — 6.第七章 快速排序 2011-04-25 22:23 Tanky Woo
@KingJ
很高兴你能从此刻开始认真学习这本书,因为这本书太好了,所以我希望任何喜欢算法的朋友都看看这本书。

正好以后咱两也可以多多互相交流了,加油吧!  回复  更多评论
  
# re: 《算法导论》学习总结 — 6.第七章 快速排序 2011-04-26 23:09 dwtsteven
请问第二个算法是不是稳定的。我觉得好像是稳定的,相等的元素之间的顺序并没有发生变化。  回复  更多评论
  

只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理