Advertisement
tockata

Untitled

Mar 14th, 2015
266
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.27 KB | None | 0 0
  1. public static IComparable[] QuickSortRecursive(IComparable[] arr, int left, int right)
  2.         {
  3.             IComparable[] resultArr = arr;
  4.             if (left < right)
  5.             {
  6.                 int pivot = Partition(resultArr, left, right);
  7.  
  8.                 if (pivot > 1)
  9.                     QuickSortRecursive(resultArr, left, pivot - 1);
  10.  
  11.                 if (pivot + 1 < right)
  12.                     QuickSortRecursive(resultArr, pivot + 1, right);
  13.             }
  14.  
  15.             return resultArr;
  16.         }
  17.  
  18.         public static int Partition(IComparable[] arr, int left, int right)
  19.         {
  20.             IComparable pivot = arr[left];
  21.             while (true)
  22.             {
  23.                 while (arr[left].CompareTo(pivot) < 0)
  24.                 {
  25.                     left++;  
  26.                 }
  27.  
  28.                 while (arr[right].CompareTo(pivot) > 0)
  29.                 {
  30.                     right--;
  31.                 }
  32.                
  33.                 if (left < right)
  34.                 {
  35.                     IComparable temp = arr[right];
  36.                     arr[right] = arr[left];
  37.                     arr[left] = temp;
  38.                 }
  39.                 else
  40.                 {
  41.                     return right;
  42.                 }
  43.             }
  44.         }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement