-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathClass1.cs
More file actions
68 lines (67 loc) · 2.01 KB
/
Class1.cs
File metadata and controls
68 lines (67 loc) · 2.01 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
56
57
58
59
60
61
62
63
64
65
66
67
68
namespace MergeSort
{
public static class Merge<T> where T : IComparable
{
static T[] MergeMethod(T[] a, T[] b)
{
T[] result = new T[a.Length + b.Length];
int aindex = 0;
int bindex = 0;
int resindex = 0;
while (resindex != result.Length)
{
if (aindex == a.Length)
{
result[resindex] = b[bindex];
bindex++;
resindex++;
continue;
}
if (bindex == b.Length)
{
result[resindex] = a[aindex];
aindex++;
resindex++;
continue;
}
if (a[aindex].CompareTo(b[bindex]) == -1 || a[aindex].CompareTo(b[bindex]) == 0)
{
result[resindex] = a[aindex];
aindex++;
resindex++;
continue;
}
if (bindex < b.Length && a[aindex].CompareTo(b[bindex]) == 1)
{
result[resindex] = b[bindex];
bindex++;
resindex++;
continue;
}
}
return result;
}
public static T[] Sort(T[] elements)
{
if (elements.Length == 1)
{
T[] myArray = { elements[0] };
return myArray;
}
int mid = elements.Length / 2;
T[] left = new T[mid];
T[] right = new T[elements.Length - mid];
for (int i = 0; i < mid; i++)
{
left[i] = elements[i];
}
for (int j = mid; j < elements.Length; j++)
{
right[j - mid] = elements[j];
}
left = Sort(left);
right = Sort(right);
return MergeMethod(left, right);
}
}
}