C#堆栈、队列、递归实现的快速排序

参考: https://zh.wikipedia.org/zh-hant/%E5%BF%AB%E9%80%9F%E6%8E%92%E5%BA%8F#C.23

static void Qsort(int[] data, int low, int high)//递归实现
        {
            if (low >= high) return;
            int i, j, pivot;
            i = low;
            j = high;
            pivot = data[low];
            while (i < j)
            {
                while (data[j] > pivot) j--;
                data[i] = data[j];
                while (i < j && data[i] <= pivot) i++;
                data[j] = data[i];
            }
            data[i] = pivot;
            Qsort(data, low, i - 1);
            Qsort(data, i + 1, high);
        }
       //堆栈实现
        public static void StackQS(int[] data)
        {
            int i, j, low, high, pivot;
            Stack<int> MyStack = new Stack<int>();
            MyStack.Push(0);
            MyStack.Push(data.Length - 1);
            while (MyStack.Count > 0)
            {
                j = high = MyStack.Pop();
                i = low = MyStack.Pop();
                if (low >= high) continue;
                pivot = data[low];
                while (i < j)
                {
                    while (data[j] > pivot) j--;
                    data[i] = data[j];
                    while (i < j && data[i] <= pivot) i++;
                    data[j] = data[i];
                }
                data[i] = pivot;
                MyStack.Push(low);
                MyStack.Push(i - 1);
                MyStack.Push(i + 1);
                MyStack.Push(high);
            }
        }
      //队列实现
       public static void QueueQS(int[] data)
        {
            int i, j, low, high, pivot;
            Queue<int> MyQueue = new Queue<int>();
            MyQueue.Enqueue(0);
            MyQueue.Enqueue(data.Length-1);
            while(MyQueue.Count>0)
            {
                i = low = MyQueue.Dequeue();
                j = high = MyQueue.Dequeue();
                if (low >= high) continue;
                pivot = data[low];
                while(i<j)
                {
                    while (data[j] > pivot) j--;
                    data[i] = data[j];
                    while (i < j && data[i] <= pivot) i++;
                    data[j] = data[i];
                }
                data[i] = pivot;
                MyQueue.Enqueue(low);
                MyQueue.Enqueue(i-1);
                MyQueue.Enqueue(i+1);
                MyQueue.Enqueue(high);
            }
        }
此条目发表在.net分类目录,贴了, 标签。将固定链接加入收藏夹。