stefan1919

MergeSortFinal

Oct 24th, 2015
221
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.84 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 MergeSort1
  8. {
  9.     class Program
  10.     {
  11.         static void Main(string[] args)
  12.         {
  13.             List<int> list = new List<int>();
  14.             int m = int.Parse(Console.ReadLine());
  15.             for (int i = 0; i < m; i++)
  16.             {
  17.                 int p = int.Parse(Console.ReadLine());
  18.                 list.Add(p);
  19.             }
  20.             Console.WriteLine(String.Join(",", MergeSort(list)));
  21.         }
  22.         static List<int> MergeSort(List<int> array)
  23.         {
  24.             List<int> output = new List<int>();
  25.             if (array.Count == 1)
  26.             {
  27.                 return array;
  28.             }
  29.             if (array.Count == 2)
  30.             {
  31.                 if (array[0] > array[1])
  32.                 {
  33.                     int a = array[1];
  34.                     array[1] = array[0];
  35.                     array[0] = a;
  36.                     return array;
  37.                 }
  38.                 else
  39.                 {
  40.                     return array;
  41.                 }
  42.             }
  43.             else
  44.             {
  45.                 List<int> firstList = new List<int>();
  46.                 List<int> secondList = new List<int>();
  47.                 for (int i = 0; i < array.Count / 2; i++)
  48.                 {  
  49.                     firstList.Add(array[i]);
  50.                     secondList.Add(array[i + array.Count / 2]);  
  51.                 }
  52.                 if (array.Count % 2 != 0)
  53.                 {
  54.                     secondList.Add(array[array.Count - 1]);
  55.                 }
  56.                 firstList = MergeSort(firstList);
  57.                 secondList = MergeSort(secondList);
  58.                 int k = 0;
  59.                 int j = 0;
  60.                 int size = firstList.Count + secondList.Count;
  61.                 int markerFirst = firstList.Count;
  62.                 int markerSecond = secondList.Count;
  63.                
  64.                 for (int i = 0; i < size; i++)
  65.                 {
  66.                     if (k == markerFirst)
  67.                     {
  68.                         output.Add(secondList[j]);
  69.                         j++;
  70.                         continue;
  71.                     }
  72.                     if (j == markerSecond)
  73.                     {
  74.                         output.Add(firstList[k]);
  75.                         k++;
  76.                         continue;
  77.                     }
  78.                     if (firstList[k] < secondList[j])
  79.                     {
  80.                         output.Add(firstList[k]);
  81.                         k++;
  82.                     }
  83.                     else
  84.                     {
  85.                         output.Add(secondList[j]);
  86.                         j++;
  87.                     }
  88.                 }
  89.             }
  90.             return output;
  91.         }
  92.     }
  93. }
Advertisement
Add Comment
Please, Sign In to add comment