-
Notifications
You must be signed in to change notification settings - Fork 0
/
mergesort.cs
94 lines (74 loc) · 1.74 KB
/
mergesort.cs
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
using System;
// To execute C#, please define "static void Main" on a class
// named Solution.
class Solution
{
static void Main(string[] args)
{
int[] arr = new int[10] {10,9,8,7,6,5,4,3,2,1};
MergeSort m = new MergeSort();
m.Sort(arr, 0, arr.Length-1);
for(int i=0; i<arr.Length;i++)
{
Console.Write(arr[i] + " ");
}
}
}
public class MergeSort
{
public void Sort(int[] arr, int left, int right)
{
if(left < right)
{
int middle = (left + right) /2;
Sort(arr, left, middle);
Sort(arr, middle +1, right);
Merge(arr, left, middle, right);
}
}
public void Merge(int[] arr, int left, int middle, int right)
{
int n1 = middle - left + 1;
int n2 = right - middle;
int[] L = new int[n1];
int[] R = new int[n2];
for(int m=0; m<n1; ++m)
{
L[m] = arr[left + m];
}
for(int n=0; n<n2; ++n)
{
R[n] = arr[middle + 1 + n];
}
int i=0;
int j =0;
int k = left;
while(i < n1 && j < n2)
{
if(L[i] <= R[j])
{
arr[k] = L[i];
i++;
k++;
}
else
{
arr[k] = R[j];
j++;
k++;
}
}
while(i < n1)
{
arr[k] = L[i];
i++;
k++;
}
while(j < n2)
{
arr[k] = R[j];
j++;
k++;
}
}
}