您的位置:首页 > 产品设计 > UI/UE

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);

}

}

}

}

}

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: