Advertisement
stefanpu

MergeSort

Jan 9th, 2013
209
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.65 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3.  
  4.  
  5. namespace _13.MergeSort
  6. {
  7.     /*
  8.       Write a program that sorts an array of integers using the merge sort algorithm
  9.      (find it in Wikipedia).
  10.      Top-down implementation from wikipedia:
  11.      http://en.wikipedia.org/wiki/Merge_sort#Top-down_implementation
  12.      */
  13.  
  14.     class Program
  15.     {
  16.         static void Main(string[] args)
  17.         {
  18.             List<int> listToSort = new List<int>();
  19.             InitiliazeListRandomly(listToSort, 10);
  20.             Console.WriteLine("\nInitial list.\n");
  21.             PrintList(listToSort);
  22.  
  23.             listToSort = PerformMergeSort(listToSort);
  24.             Console.WriteLine("\nSorted list.\n");
  25.             PrintList(listToSort);
  26.         }
  27.  
  28.         private static List<int> PerformMergeSort(List<int> listToSort)
  29.         {
  30.             //List of one element is sorted.
  31.             if (listToSort.Count<=1)
  32.             {
  33.                 return listToSort;                
  34.             }
  35.  
  36.             List<int> leftList = new List<int>(), rightList = new List<int>();
  37.             int middle = listToSort.Count / 2;
  38.  
  39.             for (int elementIndex = 0; elementIndex < middle; elementIndex++)
  40.             {
  41.                 leftList.Add(listToSort[elementIndex]);                
  42.             }
  43.  
  44.             for (int elementIndex = middle; elementIndex < listToSort.Count; elementIndex++)
  45.             {
  46.                 rightList.Add(listToSort[elementIndex]);
  47.             }
  48.            
  49.             //Recursively create new lists of have size the list passed as parameter.
  50.             leftList = PerformMergeSort(leftList);
  51.             rightList = PerformMergeSort(rightList);
  52.  
  53.             return MergeLeftAndRightList(leftList, rightList);
  54.         }
  55.  
  56.         private static List<int> MergeLeftAndRightList(List<int> leftList, List<int> rightList)
  57.         {
  58.             List<int> mergedList = new List<int>();
  59.             while (leftList.Count > 0 || rightList.Count > 0)
  60.             {
  61.                 if (leftList.Count > 0 && rightList.Count > 0)
  62.                 {
  63.                     //after "leftList.RemoveAt(0);" the next item becomes with index 0.
  64.                     //So if list is: {1, 2}; after "RemoveAt(0)" list is {1} - the first
  65.                     // element is 1.
  66.                     if (leftList[0]<= rightList[0])
  67.                     {                        
  68.                         mergedList.Add(leftList[0]);
  69.                         leftList.RemoveAt(0);
  70.                     }
  71.                     else
  72.                     {
  73.                         mergedList.Add(rightList[0]);
  74.                         rightList.RemoveAt(0);
  75.                     }
  76.                 }
  77.  
  78.                 else if (leftList.Count >0)
  79.                 {
  80.                     mergedList.Add(leftList[0]);
  81.                     leftList.RemoveAt(0);                    
  82.                 }
  83.  
  84.                 else if (rightList.Count > 0)
  85.                 {
  86.                     mergedList.Add(rightList[0]);
  87.                     rightList.RemoveAt(0);
  88.                 }
  89.                
  90.             }
  91.  
  92.             return mergedList;
  93.         }
  94.  
  95.         /// <summary>
  96.         /// Initializes list of integers.
  97.         /// </summary>
  98.         /// <param name="elementsList">The list to be initialized.</param>
  99.         /// <param name="count">The number of the elements in the list.</param>
  100.         private static void InitiliazeList(List<int> elementsList, int count)
  101.         {
  102.             for (int i = 0; i < count; i++)
  103.             {
  104.                 Console.Write("Enter value {0}: ", i + 1);
  105.                 string currentNumber = Console.ReadLine();
  106.                 int currentElement = int.Parse(currentNumber);
  107.  
  108.                 elementsList.Add(currentElement);
  109.             }
  110.         }
  111.  
  112.         /// <summary>
  113.         /// Initializes list of integers by using random number generator.
  114.         /// </summary>
  115.         /// <param name="elementsList">The list to be initialized.</param>
  116.         /// <param name="count">The number of the elements in the list.</param>
  117.         private static void InitiliazeListRandomly(List<int> elementsList, int count)
  118.         {
  119.             Random numGenerator = new Random();
  120.             for (int i = 0; i < count; i++)
  121.             {
  122.                 int currentElement = numGenerator.Next(-561, 400);
  123.                 elementsList.Add(currentElement);
  124.             }
  125.         }
  126.  
  127.         public static void PrintList<T>(List<T> list)
  128.         {
  129.             foreach (T item in list)
  130.             {
  131.                 Console.Write(item + " ");
  132.             }
  133.             Console.WriteLine("\n---------------");
  134.         }
  135.     }
  136. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement