forked from TimyVillarmia/CSharp-Data-Structures-Algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
/
MergeSort.linq
91 lines (78 loc) · 2.34 KB
/
MergeSort.linq
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
<Query Kind="Program" />
void Main()
{
int[] array = new int[] { 3, 4, 2, 1, 6, 5, 7, 8, 1, 1 };
MergeSort.sort(array);
for (int i = 0; i < array.Length; i++)
Console.Write(array[i] + " ");
}
// You can define other methods, fields, classes and namespaces here
/*
Merge sort divide the input into two half recursively. In each step, the data
is divided into two half. The two parts are sorted separately and finally
combine the result into final sorted output.
*/
public class MergeSort
{
public static void merge(int[] arr, int[] tempArray, int lowerIndex, int middleIndex, int upperIndex)
{
int lowerStart = lowerIndex;
int lowerStop = middleIndex;
int upperStart = middleIndex + 1;
int upperStop = upperIndex;
int count = lowerIndex;
while (lowerStart <= lowerStop && upperStart <= upperStop)
{
if (arr[lowerStart] < arr[upperStart])
{
tempArray[count++] = arr[lowerStart++];
}
else
{
tempArray[count++] = arr[upperStart++];
}
}
while (lowerStart <= lowerStop)
{
tempArray[count++] = arr[lowerStart++];
}
while (upperStart <= upperStop)
{
tempArray[count++] = arr[upperStart++];
}
for (int i = lowerIndex; i <= upperIndex; i++)
{
arr[i] = tempArray[i];
}
}
public static void mergeSrt(int[] arr, int[] tempArray, int lowerIndex, int upperIndex)
{
if (lowerIndex >= upperIndex)
return;
int middleIndex = (lowerIndex + upperIndex) / 2;
mergeSrt(arr, tempArray, lowerIndex, middleIndex);
mergeSrt(arr, tempArray, middleIndex + 1, upperIndex);
merge(arr, tempArray, lowerIndex, middleIndex, upperIndex);
}
public static void sort(int[] arr)
{
int size = arr.Length;
int[] tempArray = new int[size];
mergeSrt(arr, tempArray, 0, size - 1);
}
}
/*
Analysis:
• Time Complexity of Merge-Sort is O(nlogn) in all 3 cases (best,
average and worst) as Merge-Sort always divides the array into two
halves and takes linear time to merge two halves.
• It requires the equal amount of additional space as the unsorted array.
Hence, it is not at all recommended for searching large unsorted arrays.
• It is the best Sorting technique for sorting Linked Lists.
Complexity Analysis:
Worst Case time complexity | O(nlogn)
Best Case time complexity | O(nlogn)
Average case time complexity | O(nlogn)
Space Complexity | O(n)
Stable Sorting | Yes
*/