快速排序的C#实现以及,算法导论上之后一个习题的思考
来源:互联网 发布:php短信接口代码 编辑:程序博客网 时间:2024/05/01 02:19
//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.
namespace FengChen.Practices
{
namespace Chapter7
{
public class QuickSort
{
private static bool m_NonDecreasing;
private static bool Ordered(Int32 A, Int32 B)
{
if (m_NonDecreasing) return (A <= B);
else return (A >= B);
}
private static void QuicksortDriver(Int32[] A, Int32 p, Int32 r)
{
if (p >= r) return;
Int32 q = Partition(A, p, r);
QuicksortDriver(A, p, q - 1);
QuicksortDriver(A, q + 1, r);
}
private static Int32 Partition(Int32[] A, Int32 p, Int32 r)
{
Int32 x = A[r]; // pivot
Int32 i = p - 1;
for (Int32 j = p; j < r; j++)
if (Ordered(A[j], x)) Common.Exchange(ref A[++i], ref A[j]);
Common.Exchange(ref A[++i], ref A[r]);
return i;
}
/// <summary>
/// Sort the array to non decreasing order
/// </summary>
/// <param name="InputArray"></param>
public static void Sort(Int32[] InputArray, bool Increasing)
{
m_NonDecreasing = Increasing;
QuicksortDriver(InputArray, 0, InputArray.Length - 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.
namespace FengChen.Practices
{
namespace Chapter7
{
public class QuickSort
{
private static bool m_NonDecreasing;
private static bool Ordered(Int32 A, Int32 B)
{
if (m_NonDecreasing) return (A <= B);
else return (A >= B);
}
private static void QuicksortDriver(Int32[] A, Int32 p, Int32 r)
{
if (p >= r) return;
Int32 q = Partition(A, p, r);
QuicksortDriver(A, p, q - 1);
QuicksortDriver(A, q + 1, r);
}
private static Int32 Partition(Int32[] A, Int32 p, Int32 r)
{
Int32 x = A[r]; // pivot
Int32 i = p - 1;
for (Int32 j = p; j < r; j++)
if (Ordered(A[j], x)) Common.Exchange(ref A[++i], ref A[j]);
Common.Exchange(ref A[++i], ref A[r]);
return i;
}
/// <summary>
/// Sort the array to non decreasing order
/// </summary>
/// <param name="InputArray"></param>
public static void Sort(Int32[] InputArray, bool Increasing)
{
m_NonDecreasing = Increasing;
QuicksortDriver(InputArray, 0, InputArray.Length - 1);
}
}
}
}
这个实现倒并不难。可是请看算法导论后面的一个习题:
What value of q does PARTITION return when all elements in the array A[p ‥ r] have the same value? Modify PARTITION so that q = (p+r)/2 when all elements in the array A[p ‥ r] have the same value.
后面 的疑问目前我研究看来没有不极大伤害性能的解答。
- 快速排序的C#实现以及,算法导论上之后一个习题的思考
- [算法导论] 快速排序以及最大堆的C++实现
- 算法导论上的快速排序
- 算法导论习题1.2的一些思考。
- 算法导论上的quicksort实现 快速排序的随机化版本
- 算法导论上几个简单的习题
- 【算法导论】c++实现的随机化的快速排序
- 算法导论第二章习题的一些思考
- 《算法导论的Java实现》 8 快速排序
- 算法导论的Java实现----7. 快速排序
- 算法导论第七章快速排序的C++实现
- 算法导论 - 快速排序的 C 语言实现
- 算法导论中快速排序的三步分治实现
- 插入排序,合并排序,堆排序,快速排序,计数排序的实现(算法导论)
- 【算法导论】快速排序实现
- 算法导论快速排序实现
- 【算法】快速排序的一个实现
- 排序算法之快速排序的思想以及Java实现
- DB2客户端连接服务器的配置问题
- 关于判断MDI窗体是否存在,存在则显示,不存在则创建
- MINA Beginning (转载)
- 第一次网上买书 就碰到 当当这么虚情假意的!~
- 多年前的档案找到了
- 快速排序的C#实现以及,算法导论上之后一个习题的思考
- 关于友元
- [转]笔记本上装Vmware巨慢无比的原因探索
- Unix 的入侵追踪
- 一统天下,使用ANT构建VS.NET工程
- OPENGL光照处理、明暗处理、光源(转)
- xml基础入门(1)
- 安装SQL Server 2005 时提示增加计数器注册表项值错误
- 生命中的11种感动