Viacheslav

QuickSort Generic

Apr 6th, 2019
161
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.07 KB | None | 0 0
  1. public static void QuickSortClassic<T>(IList<T> collection, int startIndex, int endIndex) where T : IComparable
  2.         {
  3.             if (startIndex >= endIndex)
  4.             {
  5.                 return;
  6.             }
  7.             int pivotIndex = Partition(collection, startIndex, endIndex);
  8.  
  9.             QuickSortClassic(collection, startIndex, pivotIndex - 1);
  10.             QuickSortClassic(collection, pivotIndex + 1, endIndex);
  11.  
  12.         }
  13.  
  14.         public static int Partition<T>(IList<T> collection, int startIndex, int endIndex) where T : IComparable
  15.         {
  16.             int pivotIndex = startIndex;
  17.             var pivotValue = collection[endIndex];
  18.  
  19.             for (int i = startIndex; i < endIndex; i++)
  20.             {
  21.                 if (collection[i].CompareTo(pivotValue) == -1 || collection[i].CompareTo(pivotValue) == 0)
  22.                 {
  23.                     Swap(collection, i, pivotIndex);
  24.                     pivotIndex++;
  25.                 }
  26.             }
  27.             Swap(collection, pivotIndex, endIndex);
  28.  
  29.             return pivotIndex;
  30.  
  31.         }
Advertisement
Add Comment
Please, Sign In to add comment