mirror of
https://github.com/theoludwig/programming-challenges.git
synced 2024-12-08 00:45:29 +01:00
feat(solutions): add sorting-algorithms/cs/merge-sort
This commit is contained in:
parent
2d13544b26
commit
0d1dc719e1
@ -0,0 +1,3 @@
|
||||
# sorting-algorithms/cs/merge-sort
|
||||
|
||||
Created by [@Divlo](https://github.com/Divlo) on 11 September 2021.
|
@ -0,0 +1,76 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace Solution
|
||||
{
|
||||
class Program
|
||||
{
|
||||
static void Main()
|
||||
{
|
||||
string line = Console.ReadLine();
|
||||
List<int> numbers = new List<int>();
|
||||
while ((line = Console.ReadLine()) != null)
|
||||
{
|
||||
numbers.Add(int.Parse(line));
|
||||
}
|
||||
int[] result = MergeSort(numbers.ToArray());
|
||||
foreach (int number in result)
|
||||
{
|
||||
Console.WriteLine(number);
|
||||
}
|
||||
}
|
||||
|
||||
public static int[] MergeSort(int[] array)
|
||||
{
|
||||
if (array.Length <= 1)
|
||||
{
|
||||
return array;
|
||||
}
|
||||
int middle = array.Length / 2;
|
||||
int[] left = new int[middle];
|
||||
int[] right = new int[array.Length - middle];
|
||||
for (int index = 0; index < middle; index++)
|
||||
{
|
||||
left[index] = array[index];
|
||||
}
|
||||
for (int index = middle; index < array.Length; index++)
|
||||
{
|
||||
right[index - middle] = array[index];
|
||||
}
|
||||
left = MergeSort(left);
|
||||
right = MergeSort(right);
|
||||
return Merge(left, right);
|
||||
}
|
||||
|
||||
public static int[] Merge(int[] left, int[] right)
|
||||
{
|
||||
int[] result = new int[left.Length + right.Length];
|
||||
int leftIndex = 0;
|
||||
int rightIndex = 0;
|
||||
for (int index = 0; index < result.Length; index++)
|
||||
{
|
||||
if (leftIndex >= left.Length)
|
||||
{
|
||||
result[index] = right[rightIndex];
|
||||
rightIndex++;
|
||||
}
|
||||
else if (rightIndex >= right.Length)
|
||||
{
|
||||
result[index] = left[leftIndex];
|
||||
leftIndex++;
|
||||
}
|
||||
else if (left[leftIndex] < right[rightIndex])
|
||||
{
|
||||
result[index] = left[leftIndex];
|
||||
leftIndex++;
|
||||
}
|
||||
else
|
||||
{
|
||||
result[index] = right[rightIndex];
|
||||
rightIndex++;
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user