forked from thuva4/Algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMerge_sort.cs
More file actions
42 lines (37 loc) · 1.08 KB
/
Merge_sort.cs
File metadata and controls
42 lines (37 loc) · 1.08 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
using System.IO;
using System;
class Program
{
//By calling the sort method will sort the given array
static void sort(int[] a) {
int[] helper = new int[a.Length];
sort(a, 0, a.Length - 1, helper);
}
static void sort(int[] a, int low, int high, int[] helper) {
if (low >= high) {
return;
}
int middle = low + (high - low) / 2;
sort(a, low, middle, helper);
sort(a, middle + 1, high, helper);
merge(a, low, middle, high, helper);
}
static void merge(int[] a, int low, int middle, int high, int[] helper) {
for (int i = low; i <= high; i++) {
helper[i] = a[i];
}
int x = low;
int j = middle + 1;
for (int k = low; k <= high; k++) {
if (x > middle) {
a[k] = helper[j++];
} else if (j > high) {
a[k] = helper[x++];
} else if (helper[x] <= helper[j]) {
a[k] = helper[x++];
} else {
a[k] = helper[j++];
}
}
}
}