# 准备从头复习算法

		static void StackQuickSort<T>(T[] array, int start, int end) where T : IComparable<T>{
var stack = new Stack<int>();
stack.Push(start);stack.Push(end);

int left, right;
T pivot;
while (stack.Count > 0) {
end = right = stack.Pop(); start = left = stack.Pop();
pivot = array[left];

while (true)
{
while (array[right].CompareTo(pivot) > 0) right--;
if (right == left) break;
Swap(array, left++, right);

while (array[left].CompareTo(pivot) < 0) left++;
if (right == left) break;
Swap(array, left, right--);
}

if (left - start > 1) {
//如子区间块长大于1，则继续对区间排序
stack.Push(start);
stack .Push(left-1);
}
if (end - left > 1) {
stack.Push(left + 1);
stack.Push(end);
}
}
}


时间对比是(ms):

        internal static void QuickSort<T>(T[] array, int start, int end) where T : IComparable<T>
{
var left = start;
var right = end;
var pivot = array[start];

while (true)
{
while (array[right].CompareTo(pivot) > 0) right--;
if (right == left) break;
Swap(array, left++, right);

while (array[left].CompareTo(pivot) < 0) left++;
if (right == left) break;
Swap(array, left, right--);
}

if (left - start > 1) QuickSort(array, start, left - 1);  //如子区间块长大于1，则继续对区间排序
if (end - left > 1) QuickSort(array, left + 1, end);
}

——

posted on 2013-03-03 22:09  小城故事  阅读(457)  评论(0编辑  收藏  举报