Quick sort C# code(2)
2008-07-01 10:45
267 查看
public class QuickSortNonRecursion
{
public int Split(int[] data,int low,int high)
{
if(data == null) throw new ArgumentNullException();
if(low<0 || high >= data.length) throw new ArgumentOutOfRangeException();
int pivot = data[low];
while(low < high){
while(low < high && data[high] >= pivot) high--;
data[low] = data[high];
while(low < high && data[low]<= pivot) low++;
data[high] = data[low];
}
data[low] = pivot;
return low;
}
public void QuickSort(int[] data, int low, int high)
{
if(low < high)
{
Stack<int> stc = new Stack<int>();
int pivot = Split(data,low,high);
stc.push(low);
stc.push(pivot -1);
stc.push(pivot +1);
stc.push(high);
while(stc.count>0)
{
high = stc.pop();
low = stc.pop();
int temp;
if(low<high)
{
pivot = Split(data,low,high);
temp = pivot-1;
if(low < pivot)
{
stc.push(low);
stc.push(temp);
}
temp = pivot +1;
if(high>pivot)
{
stc.push(temp);
stc.push(high);
}
}
}
}
}
{
public int Split(int[] data,int low,int high)
{
if(data == null) throw new ArgumentNullException();
if(low<0 || high >= data.length) throw new ArgumentOutOfRangeException();
int pivot = data[low];
while(low < high){
while(low < high && data[high] >= pivot) high--;
data[low] = data[high];
while(low < high && data[low]<= pivot) low++;
data[high] = data[low];
}
data[low] = pivot;
return low;
}
public void QuickSort(int[] data, int low, int high)
{
if(low < high)
{
Stack<int> stc = new Stack<int>();
int pivot = Split(data,low,high);
stc.push(low);
stc.push(pivot -1);
stc.push(pivot +1);
stc.push(high);
while(stc.count>0)
{
high = stc.pop();
low = stc.pop();
int temp;
if(low<high)
{
pivot = Split(data,low,high);
temp = pivot-1;
if(low < pivot)
{
stc.push(low);
stc.push(temp);
}
temp = pivot +1;
if(high>pivot)
{
stc.push(temp);
stc.push(high);
}
}
}
}
}
相关文章推荐
- Quick sort C# code
- Merge sort code using C# (Arithmetic)
- C# 数组集合 <一>基本概念(Array&ArrayList&List<T>&SortList&LinkList的实现Code)
- Test C# code
- C#调用进程退出时ExitCode的作用
- C# GetHashCode与Equals在HashTable表查找时的关系
- MergeSort and Quick Sort
- managed and unmanaged code in c#
- c#List.Sort(比较的方法)返回值意义
- [C#]Array.Sort和List<T>.Sort的委托版本比IComparer版本效率低
- http://www.codefans.net/sort/list_5_214_3.shtml源码
- Cocos Code IDE执行效率太慢,Sublime+QuickXDev依然是最佳的cocos2d-x + Lua编辑器
- 利用QrCode.Net生成二维码 asp.net mvc c#
- .Net C# 为什么dataTable.DefaultView.Sort不起作用?
- Quick sort VS Merge sort
- C# 字符串性能说想 (Code Project 精选翻译)
- Quick_Sort
- Tip #8 在app_code文件夹内同时使用C#和VB.Net
- [C#/ASP.NET]List<>中Sort()、Find()、FindAll()、Exist()的使用方法
- 内部排序之Quick Sort