1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
| using System;
namespace ACMethod { class Program { public delegate bool cmp(int a, int b);
public int[] Arr;
public Program(int[] arr) { this.Arr = arr; }
public void Sort(cmp fun) { QuickSort(0, Arr.Length, fun); }
static void Main(string[] args) { Program p = new Program(new int[] {5, 77, 1, -13, 48, 1, 31, -9, 478, 1, 1, 8, 6, 6, -85, 0, 0, -135, 48, -9, 89, 12}); p.Print(); p.Sort(delegate(int a, int b) { return a > b; }); p.Print();
p = new Program(new int[] {5, 77, 1, -13, 48, 1, 31, -9, 478, 1, 1, 8, 6, 6, -85, 0, 0, -135, 48, -9, 89, 12}); p.Print(); p.Sort(delegate(int a, int b) { return a < b; }); p.Print(); Console.ReadLine(); }
void Print() { foreach (var item in Arr) { Console.Write(item + " "); } Console.WriteLine(); }
void QuickSort(int bgn, int end, cmp fun) { if (bgn >= end - 1) return;
int leftIndex = bgn; int rightIndex = end - 1; int std = Arr[leftIndex]; while (leftIndex < rightIndex) { while (leftIndex < rightIndex) { if (fun(Arr[rightIndex], std)) { Arr[leftIndex++] = Arr[rightIndex]; break; }
--rightIndex; }
while (leftIndex < rightIndex) { if (!fun(Arr[leftIndex], std)) { Arr[rightIndex--] = Arr[leftIndex]; break; }
++leftIndex; } }
Arr[leftIndex] = std; QuickSort(bgn, leftIndex, fun); QuickSort(rightIndex + 1, end, fun); } } }
|