- class Quicksort {
- private void swap(int[] Array, int Left, int Right) {
- int temp = Array[Right];
- Array[Right] = Array[Left];
- Array[Left] = temp;
- }
- public void sort(int[] Array, int Left, int Right) {
- int LHold = Left;
- int RHold = Right;
- Random ObjRan = new Random();
- int Pivot = ObjRan.Next(Left,Right);
- swap(Array,Pivot,Left);
- Pivot = Left;
- Left++;
- while (Right >= Left) {
- if (Array[Left] >= Array[Pivot]
- && Array[Right] < Array[Pivot])
- swap(Array, Left, Right);
- else if (Array[Left] >= Array[Pivot])
- Right--;
- else if (Array[Right] < Array[Pivot])
- Left++;
- else {
- Right--;
- Left++;
- } }
- swap(Array, Pivot, Right);
- Pivot = Right;
- if (Pivot > LHold)
- sort(Array, LHold, Pivot);
- if (RHold > Pivot+1)
- sort(Array, Pivot+1, RHold);
- } }
- //该片段来自于http://www.codesnippet.cn/detail/080620133920.html
来源: http://www.codesnippet.cn/detail/080620133920.html