"merge sort c# example" Code Answer's

You're definitely familiar with the best coding language C# that developers use to develop their projects and they get all their queries like "merge sort c# example" answered properly. Developers are finding an appropriate answer about merge sort c# example related to the C# coding language. By visiting this online portal developers get answers concerning C# codes question like merge sort c# example. Enter your desired code related query in the search bar and get every piece of information about C# code related question on merge sort c# example. 

merge sort c#

By BatmanBatman on Jul 10, 2020
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Merge_sort
{    
    class Program
    {
        static void Main(string[] args)
        {
            List<int> unsorted = new List<int>();
            List<int> sorted;

            Random random = new Random();

            Console.WriteLine("Original array elements:" );
            for(int i = 0; i< 10;i++){
                unsorted.Add(random.Next(0,100));
                Console.Write(unsorted[i]+" ");
            }
            Console.WriteLine();

            sorted = MergeSort(unsorted);

            Console.WriteLine("Sorted array elements: ");
            foreach (int x in sorted)
            {
                Console.Write(x+" ");
            }
			Console.Write("\n");
        }
		

        private static List<int> MergeSort(List<int> unsorted)
        {
            if (unsorted.Count <= 1)
                return unsorted;

            List<int> left = new List<int>();
            List<int> right = new List<int>();

            int middle = unsorted.Count / 2;
            for (int i = 0; i < middle;i++)  //Dividing the unsorted list
            {
                left.Add(unsorted[i]);
            }
            for (int i = middle; i < unsorted.Count; i++)
            {
                right.Add(unsorted[i]);
            }

            left = MergeSort(left);
            right = MergeSort(right);
            return Merge(left, right);
        }

        private static List<int> Merge(List<int> left, List<int> right)
        {
            List<int> result = new List<int>();

            while(left.Count > 0 || right.Count>0)
            {
                if (left.Count > 0 && right.Count > 0)
                {
                    if (left.First() <= right.First())  //Comparing First two elements to see which is smaller
                    {
                        result.Add(left.First());
                        left.Remove(left.First());      //Rest of the list minus the first element
                    }
                    else
                    {
                        result.Add(right.First());
                        right.Remove(right.First());
                    }
                }
                else if(left.Count>0)
                {
                    result.Add(left.First());
                    left.Remove(left.First());
                }
                else if (right.Count > 0)
                {
                    result.Add(right.First());

                    right.Remove(right.First());    
                }    
            }
            return result;
        }
    }
}

Add Comment

6

merge sort c# example

By Blushing BarracudaBlushing Barracuda on Apr 24, 2021
MergeSorting

Add Comment

0

All those coders who are working on the C# based application and are stuck on merge sort c# example can get a collection of related answers to their query. Programmers need to enter their query on merge sort c# example related to C# code and they'll get their ambiguities clear immediately. On our webpage, there are tutorials about merge sort c# example for the programmers working on C# code while coding their module. Coders are also allowed to rectify already present answers of merge sort c# example while working on the C# language code. Developers can add up suggestions if they deem fit any other answer relating to "merge sort c# example". Visit this developer's friendly online web community, CodeProZone, and get your queries like merge sort c# example resolved professionally and stay updated to the latest C# updates. 

C# answers related to "merge sort c# example"

View All C# queries

C# queries related to "merge sort c# example"

Browse Other Code Languages

CodeProZone