C#快速排序类

Posted on 2006-10-19 15:31 TangHuawei 阅读(1131) 评论(3)  编辑 收藏 所属分类: C#讨论
快速排序的基本思想是基于分治策略的。对于输入的子序列ap..ar,如果规模足够小则直接进行排序,否则分三步处理:

分解(Divide):将输入的序列ap..ar划分成两个非空子序列ap..aq和aq+1..ar,使ap..aq中任一元素的值不大于aq+1..ar中任一元素的值。
递归求解(Conquer):通过递归对p..aq和aq+1..ar进行排序。
合并(Merge):由于对分解出的两个子序列的排序是就地进行的,所以在ap..aq和aq+1..ar都排好序后不需要执行任何计算ap..ar就已排好序。
这个解决流程是符合分治法的基本步骤的。因此,快速排序法是分治法的经典应用实例之一。

using System;

namespace VcQuickSort
{
/// <summary>
/// ClassQuickSort 快速排序。
/// 范维肖
/// </summary>
public class QuickSort
{
public QuickSort()
{
}

private void Swap(ref int i,ref int j)
//swap two integer
{
int t;
t=i;
i=j;
j=t;
}

public void Sort(int [] list,int low,int high)
{
if(high<=low)
{
//only one element in array list
//so it do not need sort
return;
}
else if (high==low+1)
{
//means two elements in array list
//so we just compare them
if(list[low]>list[high])
{
//exchange them
Swap(ref list[low],ref list[high]);
return;
}
}
//more than 3 elements in the arrary list
//begin QuickSort
myQuickSort(list,low,high);
}

public void myQuickSort(int [] list,int low,int high)
{
if(low<high)
{
int pivot=Partition(list,low,high);
myQuickSort(list,low,pivot-1);
myQuickSort(list,pivot+1,high);
}
}

private int Partition(int [] list,int low,int high)
{
//get the pivot of the arrary list
int pivot;
pivot=list[low];
while(low<high)
{
while(low<high && list[high]>=pivot)
{
high--;
}
if(low!=high)
{
Swap(ref list[low],ref list[high]);
low++;
}
while(low<high && list[low]<=pivot)
{
low++;
}
if(low!=high)
{
Swap(ref list[low],ref list[high]);
high--;
}
}
return low;
}

}
}

Feedback

#1楼   回复  引用    

2007-05-31 15:42 by 赵海燕[未注册用户]
/*
* Copyright (c) 2006,赵海燕 GS0621411
* All rights reserved.
*
* 文件名称:bubblesort.cpp
* 文件标识:
* 摘 要:冒泡排序的实现 ,调试环境Dev-C++ 4.9.9.2
*
* 当前版本:1.0
* 作 者:赵海燕
* 完成日期:2007年5月18日
*
* 取代版本:1.0
* 原作者 :赵海燕
* 完成日期:2007年5月18日
*/
using System;
using System.Collections;
using System.Collections.Specialized;
using System.Data.Common;
namespace bubblesort
{

/// <summary>
/// Class1 的摘要说明。
/// </summary>
///
public delegate bool CompObj(object lho,object rho);
public class Employee
{
string name;
string id;
public Employee(string id,string name)
{
this.name = name;
this.id = id;
}
public string ID
{
get{return id;}
set{id=value;}
}
public string Name
{
get{return name;}
set{name = value;}
}
public static bool CompEmp(object lhe,object rhe)
{
return (((Employee)lhe).ID.CompareTo(((Employee)rhe).ID) == 1 ) ? true : false;
}


}
public class Sort
{
public static void BubbleSort(object[] src,CompObj cmp)
{
if(src==null)
{
return;
}
else
{
object temp;
for(int i = src.Length;i > 0 ;i-- )
{
for(int j = 0;j < i-1;j++)
{
if(cmp(src[j],src[j+1]))
{
temp = src[j];
src[j] = src[j+1];
src[j+1] = temp;
}
}
}

}
}
}
class Class1
{
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]

static void Main(string[] args)
{
//
// TODO: 在此处添加代码以启动应用程序
//
Employee[] emp = new Employee[4];

emp[0]=new Employee("001","zhaohy");

emp[1]=new Employee("004","zhaohb");

emp[2]=new Employee("003","zhaoht");

emp[3]=new Employee("004","zhangql");

CompObj obj = new CompObj(Employee.CompEmp);
Sort.BubbleSort(emp,obj);
Console.WriteLine("排序后结果为:");
foreach(Employee a in emp)
Console.WriteLine(a.ID +" "+ a.Name );
}
}
}

#2楼   回复  引用    

2008-02-07 16:49 by 曹源[未注册用户]
本人刚学C#半年,急需高手指点!
下面是我写的快速排序:

static void QuickSort(int[] ar, int a, int z)
{
if (a != z)
{
int i = Partition(ar, a, z);
if (a < i - 1)
{
QuickSort(ar, a, i - 1);
}
if (i + 1 < z)
{
QuickSort(ar, i + 1, z);
}
}
}

static int Partition(int[] ar, int low, int high)
{
for (; high > low; high--)
{
if (ar[low] > ar[high])
{
Swap(ar, low, high);
if (low + 1 == high)
{
return high;
}
break;
}
if (low + 1 == high)
{
return low;
}
}

for (; low + 1 < high; low++)
{
if (ar[low + 1] > ar[high])
{
Swap(ar, low + 1, high);
if (low + 2 == high)
{
return low + 1;
}
return Partition(ar, low + 1, high - 1);
}
if (low + 2 == high)
{
return high;
}
}
}

static void Swap(int[] ar, int a, int z)
{
int hold;
hold = ar[a];
ar[a] = ar[z];
ar[z] = hold;
}

enum Bit
{
GE_SHU = 10,//可修改此处的数组元素总数
}

static void Main(string[] args)
{
for (; ; )
{
Console.WriteLine("本程序进行" + (int)Bit.GE_SHU + "个元素数组的排序。");
Console.WriteLine("请输入" + (int)Bit.GE_SHU + "个整数初始化数组:");
int[] ar = new int[(int)Bit.GE_SHU];
for (int j = 0; j < (int)Bit.GE_SHU; j++)
{
ar[j] = Int32.Parse(Console.ReadLine());
}
QuickSort(ar, 0, (int)Bit.GE_SHU - 1);
Console.WriteLine("快速排序结果为:");
string s = "";
for (int j = 0; j < (int)Bit.GE_SHU; j++)
s += " " + ar[j];
Console.WriteLine(s);
}
}

QQ:776410680

#3楼   回复  引用    

2008-02-27 15:09 by ·!#¥·#[未注册用户]
if(Me == "努力"+"努力"+"再努力") { Me == "成功!"; }
大哥,你语法怎么学的?



发表评论

昵称: [登录] [注册]

主页:

邮箱:(仅博主可见)

评论内容:

  登录  注册

[使用Ctrl+Enter键快速提交评论]

0 533711




相关文章:

相关链接: