stefan1919

MergeSort

Oct 23rd, 2015
187
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.78 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 n = int.Parse(Console.ReadLine());
  14.             int[] arrayStart = new int[] { 5, 4, 1, 8, 7, 2, 6, 3};
  15.             Console.WriteLine(someReturn(arrayStart));
  16.         }
  17.         static int[] Function (int[] someArray)
  18.         {
  19.             int size = someArray.Length;
  20.             int[] outputArray = new int[size];
  21.             int[] firstArray = new int[size / 2];
  22.             for (int i = 0; i < size / 2; i++)
  23.             {
  24.                 firstArray[i] = someArray[i];
  25.             }
  26.             int[] secondArray = new int[size / 2];
  27.             for (int i = size / 2; i < size; i++)
  28.             {
  29.                 secondArray[i] = someArray[i];
  30.             }
  31.             int k = 1;
  32.             int j = 1;
  33.             for (int i = 0; i < size; i++)
  34.             {
  35.                 if (firstArray[k] < secondArray[j])
  36.                 {
  37.                     outputArray[i] = firstArray[k];
  38.                     k++;
  39.                 }
  40.                 else
  41.                 {
  42.                     outputArray[i] = secondArray[j];
  43.                     j++;
  44.                 }
  45.             }
  46.             return outputArray;
  47.         }
  48.  
  49.         static int[] someReturn(int[] arrayForReturn)
  50.         {
  51.             int size = arrayForReturn.Length;
  52.             int[] firstArray = new int[size / 2];
  53.             int[] secondArray = new int[size / 2];
  54.             if (arrayForReturn.Length == 2)
  55.             {
  56.                 firstArray[0] = arrayForReturn[0];
  57.                 secondArray[0] = arrayForReturn[1];
  58.             }
  59.             else
  60.             {
  61.                 for (int i = 0; i < size / 2; i++)
  62.                 {
  63.                     firstArray[i] = arrayForReturn[i];
  64.                 }
  65.                 for (int i = size / 2; i < size; i++)
  66.                 {
  67.                     secondArray[i] = arrayForReturn[i];
  68.                 }
  69.                 firstArray = someReturn(firstArray);
  70.                 secondArray = someReturn(secondArray);
  71.             }
  72.  
  73.             int k = 1;
  74.             int j = 1;
  75.             size = firstArray.Length + secondArray.Length;
  76.             int[] outputArray = new int[size];
  77.             for (int i = 0; i < size; i++)
  78.             {
  79.                 if (firstArray[k] < secondArray[j])
  80.                 {
  81.                     outputArray[i] = firstArray[k];
  82.                     k++;
  83.                 }
  84.                 else
  85.                 {
  86.                     outputArray[i] = secondArray[j];
  87.                     j++;
  88.                 }
  89.             }
  90.             return outputArray;
  91.         }
  92.     }
  93. }
Advertisement
Add Comment
Please, Sign In to add comment