-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSort.cs
More file actions
175 lines (138 loc) · 5.69 KB
/
Sort.cs
File metadata and controls
175 lines (138 loc) · 5.69 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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace Simulator
{
class Sort
{
Robot r;
public Sort(Robot r){
this.r=r;
}
// Sorting algorithms-- Self Explanatory since the structure is the same as the actual algorithm
public void BubbleSort(Func<Robot.IPointable, Robot.IPointable, bool> sortOrder) //Sort order is passed as a func
{
int size = r.Elements.Count;
Robot._Pointer ppass = r.createPointer("Size-Pass", r.Elements[size-1]); //This creates pointers to be animated
for (int pass = 1; pass < size; pass++)
{
ppass.setIndex(r.Elements[size-pass]);
Robot._Pointer pi = r.createPointer("I", r.Elements[0]);
for (int i = 0; i < size - pass; i++)
{
pi.setIndex(r.Elements[i]);
if (sortOrder.Invoke(r.Elements[i], r.Elements[i + 1]))
{
r.Switch(r.Elements[i].value, r.Elements[i + 1].value);
}
}
pi.Remove(); //Remove pointer when sorting animation is done
}
ppass.Remove();
MessageBox.Show("Sorted!","BubbleSort",MessageBoxButtons.OK,MessageBoxIcon.Information);
}
public void InsertionSort(Func<Robot.IPointable, Robot.IPointable, bool> sortOrder)
{
int size = r.Elements.Count;
Robot._Pointer key = r.createTemp("Key", "");
Robot._Pointer pi = r.createPointer("I", r.Elements[0]);
for (int i = 1; i < size; i++)
{
pi.setIndex(r.Elements[i]);
r.Copy(r.Elements[i].value, key.getIndex().value);
int j = i - 1;
Robot._Pointer pj = r.createPointer("J", r.Elements[j],5,1);
while (j >= 0)
{
pj.setIndex(r.Elements[j]);
if (sortOrder.Invoke(r.Elements[j], key.getIndex()))
{
r.Copy(r.Elements[j].value, r.Elements[j + 1].value);
j -= 1;
}
else break;
}
r.Copy(key.getIndex().value, r.Elements[j + 1].value);
pj.Remove();
}
MessageBox.Show("Sorted!", "InsertionSort", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
public void SelectionSort(Func<Robot.IPointable, Robot.IPointable, bool> sortOrder)
{
int size = r.Elements.Count;
Robot._Pointer pj = r.createPointer("J", r.Elements[0]);
for (int j = 0; j < size; j++)
{
pj.setIndex(r.Elements[j]);
Robot._Pointer pmin = r.createPointer("Min", r.Elements[j],5,1);
Robot._Pointer pi = r.createPointer("I", r.Elements[(j + 1 < size) ? j + 1 : j]);
for (int i = j + 1; i < size; i++)
{
pi.setIndex(r.Elements[i]);
if (sortOrder.Invoke(pmin.getIndex(), r.Elements[i]))
{
pmin.setIndex(r.Elements[i]);
}
}
r.Switch(r.Elements[j].value, pmin.getIndex().value);
pi.Remove();
pmin.Remove();
}
pj.Remove();
MessageBox.Show("Sorted!", "SelectionSort", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
public void QuickSort(Func<Robot.IPointable, Robot.IPointable, bool> sortOrder, List<Robot.Element> A, int p, int _r)
{
if (p < _r)
{
int q = partition(sortOrder, A, p, _r);
QuickSort(sortOrder, A, p, q);
QuickSort(sortOrder, A, q + 1, _r);
}
else
{
return;
}
}
private int partition(Func<Robot.IPointable, Robot.IPointable, bool> sortOrder, List<Robot.Element> A, int p, int _r)
{
Robot._Pointer pp = r.createPointer("P", A[p],5,1);
Robot._Pointer pr = r.createPointer("R", A[_r],5,1);
Robot._Pointer pi = r.createPointer("I", A[p]);
Robot._Pointer px = r.createTemp("X","");
r.Copy(A[p].value, px.getIndex().value);
Robot._Pointer pj = r.createPointer("J", A[_r]);
int i=p-1;
int j=_r+1;
while (true)
{
do{
i++;
if (i< A.Count)
pi.setIndex(A[i]);
}while(i<_r && sortOrder(px.getIndex(), A[i]));
do{
j--;
if (j>0)
pj.setIndex(A[j]);
}while(j>p && sortOrder(A[j],px.getIndex()));
if (i < j)
{
r.Switch(A[i].value, A[j].value);
}
else
{
pi.Remove();
pj.Remove();
r.removeTemp((Robot.Temp)px.getIndex());
px.Remove();
pp.Remove();
pr.Remove();
return j;
}
}
}
}
}