- internal void QuickSort(int left, int right)
- {
- do
- {
- int low = left;
- int hi = right;
- int median = Array.GetMedian(low, hi);
- this.SwapIfGreaterWithItems(low, median);
- this.SwapIfGreaterWithItems(low, hi);
- this.SwapIfGreaterWithItems(median, hi);
- object y = this.keys.GetValue(median);
- do
- {
- try
- {
- while (this.comparer.Compare(this.keys.GetValue(low), y) < 0)
- {
- low++;
- }
- while (this.comparer.Compare(y, this.keys.GetValue(hi)) < 0)
- {
- hi--;
- }
- }
- catch (IndexOutOfRangeException)
- {
- throw new ArgumentException(Environment.GetResourceString("Arg_BogusIComparer", new object[] { y, y.GetType().Name, this.comparer }));
- }
- catch (Exception exception)
- {
- throw new InvalidOperationException(Environment.GetResourceString("InvalidOperation_IComparerFailed"), exception);
- }
- if (low > hi)
- {
- break;
- }
- if (low < hi)
- {
- object obj3 = this.keys.GetValue(low);
- this.keys.SetValue(this.keys.GetValue(hi), low);
- this.keys.SetValue(obj3, hi);
- if (this.items != null)
- {
- object obj4 = this.items.GetValue(low);
- this.items.SetValue(this.items.GetValue(hi), low);
- this.items.SetValue(obj4, hi);
- }
- }
- if (low != 0x7fffffff)
- {
- low++;
- }
- if (hi != -2147483648)
- {
- hi--;
- }
- }
- while (low <= hi);
- if ((hi - left) <= (right - low))
- {
- if (left < hi)
- {
- this.QuickSort(left, hi);
- }
- left = low;
- }
- else
- {
- if (low < right)
- {
- this.QuickSort(low, right);
- }
- right = hi;
- }
- }
- while (left < right);
- }
-
-
复制代码- internal void SwapIfGreaterWithItems(int a, int b)
- {
- if (a != b)
- {
- try
- {
- if (this.comparer.Compare(this.keys.GetValue(a), this.keys.GetValue(b)) > 0)
- {
- object obj2 = this.keys.GetValue(a);
- this.keys.SetValue(this.keys.GetValue(b), a);
- this.keys.SetValue(obj2, b);
- if (this.items != null)
- {
- object obj3 = this.items.GetValue(a);
- this.items.SetValue(this.items.GetValue(b), a);
- this.items.SetValue(obj3, b);
- }
- }
- }
- catch (IndexOutOfRangeException)
- {
- throw new ArgumentException(Environment.GetResourceString("Arg_BogusIComparer", new object[] { this.keys.GetValue(b), this.keys.GetValue(b).GetType().Name, this.comparer }));
- }
- catch (Exception exception)
- {
- throw new InvalidOperationException(Environment.GetResourceString("InvalidOperation_IComparerFailed"), exception);
- }
- }
- }
-
-
复制代码- [ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success)]
- private static int GetMedian(int low, int hi)
- {
- return (low + ((hi - low) >> 1));
- }
-
-
复制代码 |