SHARE
TWEET

Untitled

a guest Aug 17th, 2019 68 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. public static void Sort(int[] input) {
  2.     if (input.Length <= 1) {
  3.         return;
  4.     }
  5.    
  6.     // Поделили массивы
  7.     var leftSize = input.Length / 2;
  8.     var rightSize = input.Length - leftSize;
  9.     var left = new int[leftSize];
  10.     var right = new int[rightSize];
  11.    
  12.     // Скопировали данные из основного массива
  13.     Array.Copy(input, 0, left, 0, leftSize);
  14.     Array.Copy(input, leftSize, right, 0, rightSize);
  15.    
  16.     Sort(left);
  17.     Sort(right);
  18.    
  19.     Merge(input, left, right);
  20. }
  21.  
  22. public static void Merge(int[] input, int[] left, int[] right) {
  23.     var leftIndex = 0;
  24.     var rightIndex = 0;
  25.     var leftAndRightSize = left.Length + right.Length;
  26.    
  27.     for (int i = 0; i < leftAndRightSize; i++) {
  28.         if (leftIndex >= left.Length) {
  29.             input[i] = right[rightIndex];
  30.             rightIndex++;
  31.         }
  32.         else if (rightIndex >= right.Length) {
  33.             input[i] = left[leftIndex];
  34.             leftIndex++;
  35.         }
  36.         else if (left[leftIndex] < right[rightIndex]) {
  37.             input[i] = left[leftIndex];
  38.             leftIndex++;
  39.         }
  40.         else {
  41.             input[i] = right[rightIndex];
  42.             rightIndex++;
  43.         }
  44.     }
  45. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top