-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMergeSort.cs
More file actions
55 lines (52 loc) · 1.29 KB
/
MergeSort.cs
File metadata and controls
55 lines (52 loc) · 1.29 KB
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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace LeetCode
{
static class MergeSort
{
public static void Merge(int[] array, int l, int m, int r)
{
int i = l;
int j = m + 1;
int k = 0;
int[] temp = new int[r - l + 1];
while(i <= m && j <= r)
{
if (array[i] < array[j])
temp[k++] = array[i++];
else
temp[k++] = array[j++];
}
while (i <= m)
{
temp[k++] = array[i++];
}
while (j <= r)
{
temp[k++] = array[j++];
}
int y = 0;
for(int x = l; x <= r; x++)
{
array[x] = temp[y++];
}
}
public static void _Sort(int[] array, int l, int r)
{
if (l < r)
{
int m = (l + r) / 2;
_Sort(array, l, m);
_Sort(array, m + 1, r);
Merge(array, l, m, r);
}
}
public static void Sort(int[] array)
{
_Sort(array, 0, array.Length - 1);
}
}
}