using System using System Collections Generic using System Linq using

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication14
{
class Program
{
static void Main(string[] args)
{
var array = new int[5];
for (int i = 0; i < 5; i++)
array[i] = int.Parse(Console.ReadLine());
array = MergeSort(array);
foreach (int i in array)
Console.WriteLine(i);
Console.ReadKey();
}
static int[] MergeSort(int[] a)
{
if (a.Length > 1)
{
var L = new int[a.Length / 2];
var R = new int[a.Length - L.Length];
for (int i = 0; i < L.Length; i++)
L[i] = a[i];
for (int i = 0; i < R.Length; i++)
R[i] = a[i + L.Length];
if (L.Length > 1)
L = MergeSort(L);
if (R.Length > 1)
R = MergeSort(R);
a = Merge(L, R);
}
return a;
}
static int[] Merge(int[] L, int[] R)
{
var res = new int[R.Length + L.Length];
int iL = 0;
int iR = 0;
while (iR + iL < res.Length)
if (iR >= R.Length)
{
res[iL + iR] = L[iL];
iL++;
}
else
if (iL < L.Length && L[iL] < R[iR])
{
res[iL + iR] = L[iL];
iL++;
}
else
{
res[iL + iR] = R[iR];
iR++;
}
return res;
}
}
}