merge sort source code in c code example

Example 1: merge sort c#

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;
        }
    }
}

Example 2: merge sort c++

#include "tools.hpp"
/*   >>>>>>>> (Recursive function that sorts a sequence of) <<<<<<<<<<<< 
     >>>>>>>> (numbers in ascending order using the merge function) <<<<                                 */
std::vector<int> sort(size_t start, size_t length, const std::vector<int>& vec)
{
	if(vec.size()==0 ||vec.size() == 1)
	return vec;

	vector<int> left,right; //===>  creating left and right vectors 

	size_t mid_point = vec.size()/2; //===>   midle point between the left vector and the right vector 

	for(int i = 0 ; i < mid_point; ++i){left.emplace_back(vec[i]);} //===>  left vector 
	for(int j = mid_point; j < length; ++j){ right.emplace_back(vec[j]);} //===>  right vector 

	left = sort(start,mid_point,left); //===>  sorting the left vector 
	right = sort(mid_point,length-mid_point,right);//===>  sorting the right vector 
	

	return merge(left,right); //===>   all the function merge to merge between the left and the right
}
/*

>>>>> (function that merges two sorted vectors of numberss) <<<<<<<<<                                    */ 
vector<int> merge(const vector<int>& a, const vector<int>& b)
{
	vector<int> merged_a_b(a.size()+b.size(),0); // temp vector that includes both left and right vectors
	int i = 0;
	int j = 0;
	int k = 0;
	int left_size = a.size();
	int right_size = b.size();
	while(i<left_size && j<right_size) 
	{
		if(a[i]<b[j])
		{
			merged_a_b[k]=a[i];
			i++;
		}
		else
		{
			merged_a_b[k]=b[j];
			j++;
		}
		k++;
	}
	while(i<left_size)
	{
		merged_a_b[k]=a[i];
		i++;
		k++;
	}
	while(j<right_size)
	{
		merged_a_b[k]=b[j];
		j++;
		k++;
	}
	
	return merged_a_b;

}

Tags:

Java Example