stefan1919

MergeSortTest

Oct 23rd, 2015
169
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.58 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace MergeSort
  8. {
  9.     class Program
  10.     {
  11.         static void Main(string[] args)
  12.         {
  13.             int[] arrayStart = new int[] { 5, 4, 1, 8, 90, 3};
  14.             int[] newArray = someReturn(arrayStart);
  15.             foreach (var item in someReturn(arrayStart))
  16.             {
  17.                 Console.WriteLine(item);
  18.             }
  19.  
  20.         }
  21.  
  22.         static int[] someReturn(int[] arrayForReturn)
  23.         {
  24.             int size = arrayForReturn.Length;
  25.  
  26.             if (arrayForReturn.Length == 2)
  27.             {
  28.                 int a = 0;
  29.                 if (arrayForReturn[0] > arrayForReturn[1])
  30.                 {
  31.                     a = arrayForReturn[0];
  32.                     arrayForReturn[0] = arrayForReturn[1];
  33.                     arrayForReturn[1] = a;
  34.                     return arrayForReturn;
  35.                 }
  36.                 return arrayForReturn;
  37.             }
  38.             else
  39.             {
  40.                 int[] firstArray = new int[size / 2];
  41.                 int[] secondArray = new int[size / 2];
  42.                 for (int i = 0; i < size / 2; i++)
  43.                 {
  44.                     firstArray[i] = arrayForReturn[i];
  45.                 }
  46.                 for (int i = 0; i < size / 2; i++)
  47.                 {
  48.                     secondArray[i] = arrayForReturn[i+size/2];
  49.                 }
  50.                 firstArray = someReturn(firstArray);
  51.                 secondArray = someReturn(secondArray);
  52.  
  53.                 int k = 0;
  54.                 int j = 0;
  55.                 size = firstArray.Length + secondArray.Length;
  56.                 int[] outputArray = new int[size];
  57.                 for (int i = 0; i < size; i++)
  58.                 {
  59.                     if (k == size / 2)
  60.                         {
  61.                             outputArray[i] = secondArray[j];
  62.                             continue;
  63.                         }
  64.                     if (j == size / 2)
  65.                     {
  66.                         outputArray[i] = firstArray[k];
  67.                         continue;
  68.                     }
  69.                     if (firstArray[k] < secondArray[j])
  70.                     {
  71.                         outputArray[i] = firstArray[k];
  72.                         k++;
  73.                     }
  74.                     else
  75.                     {
  76.                         outputArray[i] = secondArray[j];
  77.                         j++;
  78.                     }
  79.                 }
  80.                 return outputArray;
  81.             }
  82.         }
  83.     }
  84. }
Advertisement
Add Comment
Please, Sign In to add comment