参考: 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 (22)
- adf (11)
- android (3)
- article (236)
- astronomy (1)
- block chain (8)
- C# Code (9)
- c/c++ (2)
- cache (8)
- cloud (2)
- consensus (3)
- css (2)
- cve (1)
- db (55)
- digest (1)
- english (1)
- finance (2)
- go (3)
- gps (2)
- hardware (1)
- html (2)
- http (2)
- info (19)
- iot (1)
- it (3)
- java (32)
- javascript (7)
- jsp (2)
- linux (76)
- mail (14)
- math (1)
- message (8)
- mood (4)
- mq (2)
- network (22)
- php (9)
- protocol (4)
- push/pull (2)
- python (5)
- rpc (2)
- search (4)
- servlet (1)
- space (24)
- storage (15)
- technologys (103)
- templete (1)
- virtual machine (7)
- web server (37)
- windows (12)
-
近期文章
其他操作
链接