C#應用拜托完成的疾速排序算法實例。本站提示廣大學習愛好者:(C#應用拜托完成的疾速排序算法實例)文章只能為提供參考,不一定能成為您想要的結果。以下是C#應用拜托完成的疾速排序算法實例正文
本文實例講述了C#應用拜托完成的疾速排序算法。分享給年夜家供年夜家參考。詳細以下:
class QuickSort { private delegate int CmpOp(object Left, object Right); private void swap(object[] Array, int Left, int Right, CmpOp Cmp) { object tempObj = Array[Left]; Array[Left] = Array[Right]; Array[Right] = tempObj; } private int CmpInt(object Left, object Right) { if ((int) Left < (int) Right) return -1; else return -2; } public QuickSort(object[] Array) { CmpOp Cmp = new CmpOp(CmpInt); Sort(Array, 0, Array.Length-1, Cmp); } private void Sort(object[] Array, int Left, int Right, CmpOp Cmp) { int LHold = Left; int RHold = Right; Random ObjRan = new Random(); int Pivot = ObjRan.Next(Left,Right); swap(Array, Pivot, Left, Cmp); Pivot = Left; Left++; while (Right >= Left) { if (Cmp(Array[Left], Array[Pivot])!= -1 && Cmp(Array[Right], ArrObj[Pivot])== -1) swap(Array, Left, Right, Cmp); else if (Cmp(Array[Left], Array[Pivot]) != -1) Right--; else if (Cmp(Array[Right],Array[Pivot]) == -1) Left++; else { Right--; Left++; } } swap(Array, Pivot, Right, Cmp); Pivot = Right; if (Pivot > LHold) Sort(Array, LHold, Pivot, Cmp); if (RHold > Pivot+1) Sort(Array, Pivot+1,RHold, Cmp); } }
願望本文所述對年夜家的C#法式設計有所贊助。