-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMergeSort.cs
More file actions
124 lines (114 loc) · 4.73 KB
/
MergeSort.cs
File metadata and controls
124 lines (114 loc) · 4.73 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
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace SortingVisualiser
{
class MergeSort
{
private static int[] nums;
private static PictureBox graph;
private static List<Bars> bars;
public static void Sort(int[] numbers, PictureBox box, List<Bars> barsList)
{
graph = box;
nums = numbers; //passed by reference
bars = barsList; //accessible anywhere in class
//split is entire array length
Split(0, nums.Length - 1);
}
public static void Split(int start, int end)
{
if (start < end)
{ //condition to end recurtion
int mid = (start + end) / 2; //get middle value
Split(start, mid);
Split(mid + 1, end); //splits into half1 and half2
Merge(start, mid, end); //merges both sets
}
}
public static void Merge(int start, int mid, int end)
{
int half1len = mid - start + 1;
int half2len = end - mid;
int[] half1 = new int[half1len]; //initialses new arrays
int[] half2 = new int[half2len]; //will merge in main array
//assigns a copy of the valuse in the range
for (int i = 0; i < half1len; i++)
{
half1[i] = nums[start + i];
SortPage.Reads++;
SortPage.UpdateReadLbl();
}
for (int i = 0; i < half2len; i++)
{
half2[i] = nums[mid + i + 1];
SortPage.Reads++;
SortPage.UpdateReadLbl();
}
int Half1Index = 0; //start index of half1
int Half2Index = 0; //start index of half2
while (Half1Index < half1len && Half2Index < half2len)
{ //while either index is less than length of its array
SortPage.Comparisons++;
SortPage.UpdateComparisonLbl();
if (half1[Half1Index] < half2[Half2Index])
{
nums[start + Half1Index + Half2Index] = half1[Half1Index];
SortPage.Writes++;
SortPage.UpdateWriteLbl();
AddMergedBar(start + Half1Index + Half2Index);
UpdateGraph(SortPage.Delay);
Half1Index++;
} //adds the smaller value to nums, then increments
else
{
nums[start + Half1Index + Half2Index] = half2[Half2Index];
SortPage.Writes++;
SortPage.UpdateWriteLbl();
AddMergedBar(start + Half1Index + Half2Index);
UpdateGraph(SortPage.Delay);
Half2Index++;
}
}
while (Half1Index < half1len)
{ //some may be left, so it will add them
nums[start + Half1Index + Half2Index] = half1[Half1Index];
SortPage.Writes++;
SortPage.UpdateWriteLbl();
AddMergedBar(start + Half1Index + Half2Index);
UpdateGraph(SortPage.Delay);
Half1Index++;
}
while (Half2Index < half2len)
{ //some may be left, so it will add them
nums[start + Half1Index + Half2Index] = half2[Half2Index];
SortPage.Writes++;
SortPage.UpdateWriteLbl();
AddMergedBar(start + Half1Index + Half2Index);
UpdateGraph(SortPage.Delay);
Half2Index++;
}
UpdateGraph(0);
}
private static void AddMergedBar(int i)
{
RectangleF rectangle = new RectangleF(new PointF(i * SortPage.WidthConstant, 0), new SizeF(SortPage.WidthConstant, 9999));
Bars bar = new Bars(SortPage.CreateRectangle(i), new Region(rectangle));
bars.Add(bar);
}
private static void UpdateGraph(int delay)
{
for (int j = 0; j < bars.Count; j++)
{
graph.Invalidate(bars[j].Region);
}
graph.Update();
Thread.Sleep(delay);
}
}
}