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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
| using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
private DateTime EventTime1, EventTime2;
private Thread t = new Thread(delegate() { });
private Random random = new Random();
private delegate void SetTextCallback(string text, SortingType sortingType);
private delegate void PerformStepCallback();
private enum SortingType { BubbleSort, SelectingSort, QuickSort, MergeSort };
public Form1()
{
InitializeComponent();
t.Abort();
}
private void b_ogl_1_Click(object sender, EventArgs e)
{
OpenFileDialog fd1 = new OpenFileDialog();
if (fd1.ShowDialog() != DialogResult.Cancel)
{
this.tb_see_1.Text = fd1.FileName;
}
}
private void b_ogl_2_Click(object sender, EventArgs e)
{
OpenFileDialog fd2 = new OpenFileDialog();
if (fd2.ShowDialog() != DialogResult.Cancel)
{
this.tb_see_2.Text = fd2.FileName;
}
}
/// <summary>
/// Swap two values
/// </summary>
/// <typeparam name="T">type of values</typeparam>
/// <param name="a">fisrt value</param>
/// <param name="b">second value</param>
private void Swap<T>(ref T a, ref T b)
{
T tmp = a; a = b; b = tmp;
}
private void PerformStep()
{
if (progressBar1.InvokeRequired)
{
PerformStepCallback p = new PerformStepCallback(PerformStep);
Invoke(p);
}
else progressBar1.PerformStep();
}
/// <summary>
/// Function to sort array by bubble method
/// </summary>
/// <typeparam name="T">type of sorted data</typeparam>
/// <param name="array">sorted array</param>
private void BubleSort<T>(T[] array)
where T : IComparable<T>
{
EventTime1 = DateTime.Now;
for (int i = 1; i < array.Length; i++)
{
for (int j = 0; j + 1 < array.Length; j++)
{
if (array[j].CompareTo(array[j + 1]) > 0)
{
Swap<T>(ref array[j], ref array[j + 1]);
}
}
PerformStep();
}
EventTime2 = DateTime.Now;
WrtiteTime(SortingType.BubbleSort);
WriteFile<T>(array);
MessageBox.Show("Sorting finnished successfuly.");
}
private void SelectingSort<T>(T[] array)
where T : IComparable<T>
{
EventTime1 = DateTime.Now;
for (int i = 0, k = array.Length - 1; i < k; ++i, --k)
{
int min = i,
max = i;
for (int j = i + 1; j < array.Length; ++j)
{
min = array[min].CompareTo(array[j]) > 0 ? j : min;
max = array[max].CompareTo(array[j]) < 0 ? j : max;
}
Swap<T>(ref array[i], ref array[min]);
Swap<T>(ref array[k], ref array[max]);
PerformStep();
}
EventTime2 = DateTime.Now;
WrtiteTime(SortingType.SelectingSort);
WriteFile<T>(array);
MessageBox.Show("Sorting finnished successfuly.");
}
/// <summary>
/// Function to sort array by quick sort method
/// </summary>
/// <typeparam name="T">type of sorted data</typeparam>
/// <param name="array">sorted array</param>
private void QuickSort<T>(T[] array)
where T : IComparable<T>
{
EventTime1 = DateTime.Now;
QuickSort<T>(array, 0, array.Length - 1);
EventTime2 = DateTime.Now;
WrtiteTime(SortingType.QuickSort);
WriteFile<T>(array);
MessageBox.Show("Sorting finnished successfuly.");
}
/// <summary>
/// Function to sort array by quick sort method
/// </summary>
/// <typeparam name="T">type of sorted data</typeparam>
/// <param name="array">sorted array</param>
/// <param name="left">begin of unsorted part</param>
/// <param name="right">end of unsorted part</param>
private void QuickSort<T>(T[] array, int left, int right)
where T : IComparable<T>
{
if (left < right)
{
T x = array[random.Next(left, right)];
int i = left, j = right;
while (i <= j)
{
while (array[i].CompareTo(x) < 0) ++i;
while (array[j].CompareTo(x) > 0) --j;
if (i <= j)
{
Swap(ref array[i], ref array[j]);
++i;
--j;
}
}
QuickSort<T>(array, left, j);
QuickSort<T>(array, i, right);
}
else
{
PerformStep();
}
}
/// <summary>
/// Sorting array using MergeSort method
/// </summary>
/// <typeparam name="T">type of array's elements.</typeparam>
/// <param name="array">sorting array.</param>
private void MergeSort<T>(T[] array)
where T : IComparable<T>
{
EventTime1 = DateTime.Now;
MergeSort<T>(array, new T[array.Length], 0, array.Length - 1);
EventTime2 = DateTime.Now;
WrtiteTime(SortingType.MergeSort);
WriteFile<T>(array);
MessageBox.Show("Sorting finnished successfuly.");
}
/// <summary>
/// Sorting array using MergeSort method.
/// </summary>
/// <typeparam name="T">type of array's elements.</typeparam>
/// <param name="array">sorting array.</param>
/// <param name="tmp">temporary array that used for sorting.
/// Size of sorted and temporary arrays must be the same.</param>
/// <param name="left">left index, from where sorting will be started.</param>
/// <param name="right">right index, where sorting will be finnished.</param>
private void MergeSort<T>(T[] array, T[] tmp, long left, long right)
where T : IComparable<T>
{
if (left >= right)
{
PerformStep();
return;
};
long mid = (left + right) / 2;
MergeSort<T>(array, tmp, left, mid);
MergeSort<T>(array, tmp, mid + 1, right);
Merge<T>(array, tmp, left, mid, right);
}
/// <summary>
/// Sorting array using MergeSort method.
/// </summary>
/// <typeparam name="T">type of array's elements.</typeparam>
/// <param name="array">sorting array.</param>
/// <param name="tmp">temporary array that used for sorting.
/// Size of sorted and temporary arrays must be the same.</param>
/// <param name="left">left index, from where sorting will be started.</param>
/// <param name="mid">middle of sorted array.</param>
/// <param name="right">right index, where sorting will be finnished.</param>
private void Merge<T>(T[] array, T[] tmp, long left, long mid, long right)
where T : IComparable<T>
{
long i = left,
j = mid + 1,
k = left;
while (i <= mid && j <= right)
{
if (array[i].CompareTo(array[j]) < 0)
{
tmp[k++] = array[i++];
}
else
{
tmp[k++] = array[j++];
}
}
while (i <= mid) tmp[k++] = array[i++];
while (j <= right) tmp[k++] = array[j++];
for (i = left; i <= right; ++i) array[i] = tmp[i];
}
/// <summary>
/// Write time of execution.
/// </summary>
/// /// <param name="sortingType"></param>
private void WrtiteTime(SortingType sortingType)
{
TimeSpan Eplased = EventTime2 - EventTime1;
string text = String.Format("{0:00}:{1:00}:{2:00},{3:000}",
Eplased.Hours,
Eplased.Minutes,
Eplased.Seconds,
Eplased.Milliseconds);
SetText(text, sortingType);
}
/// <summary>
/// Write text from another thread.
/// </summary>
/// <param name="text">text to write</param>
/// <param name="sortingType">type sort method</param>
private void SetText(string text, SortingType sortingType)
{
if (tb_time_bul.InvokeRequired)
{
SetTextCallback setText = new SetTextCallback(SetText);
Invoke(setText, new object[] { text, sortingType });
}
else
{
if (sortingType == SortingType.BubbleSort) tb_time_bul.Text = text;
if (sortingType == SortingType.SelectingSort) tb_time_vyb.Text = text;
if (sortingType == SortingType.QuickSort) tb_time_Q.Text = text;
if (sortingType == SortingType.MergeSort) textBoxMergeSortTime.Text = text;
progressBar1.Value = progressBar1.Maximum;
}
}
/// <summary>
/// Write data to file.
/// </summary>
/// <typeparam name="T">type of data</typeparam>
/// <param name="nmatr">file for writing</param>
private void WriteFile<T>(T[] nmatr)
{
string file = tb_see_2.Text;
try
{
TextWriter tw = new StreamWriter(file);
for (int i = 0; i < nmatr.Length; ++i)
tw.WriteLine(nmatr[i].ToString());
tw.Close();
}
catch (Exception exaption)
{
MessageBox.Show(exaption.Message, "Помилка", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
}
/// <summary>
/// Read data from file.
/// </summary>
/// <param name="file">file for reading</param>
private int[] ReadFile(string file)
{
int[] nmatr = new int[] { };
try
{
TextReader tr = new StreamReader(file);
while ((file = tr.ReadLine()) != null)
{
file.Trim();
string[] matr = file.Split(' '.ToString().ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
nmatr = new int[matr.Length];
for (int i = 0; i < nmatr.Length; ++i)
nmatr[i] = Convert.ToInt32(matr[i]);
}
tr.Close();
}
catch (Exception exaption)
{
MessageBox.Show(exaption.Message, "Помилка", MessageBoxButtons.OK, MessageBoxIcon.Error);
return null;
}
return nmatr;
}
private void b_sort_bul_Click(object sender, EventArgs e)
{
string file = this.tb_see_1.Text;
int[] nmatr = ReadFile(file);
progressBar1.Value = 0;
t = new Thread(delegate() { BubleSort<int>(nmatr); });
progressBar1.Maximum = nmatr.Length;
t.Start();
}
private void b_sort_vyb_Click(object sender, EventArgs e)
{
string file = this.tb_see_1.Text;
int[] nmatr = ReadFile(file);
progressBar1.Value = 0;
t = new Thread(delegate() { SelectingSort<int>(nmatr); });
progressBar1.Maximum = nmatr.Length / 2;
t.Start();
}
private void b_sort_qsort_Click(object sender, EventArgs e)
{
string file = this.tb_see_1.Text;
int[] nmatr = ReadFile(file);
progressBar1.Value = 0;
t = new Thread(delegate() { QuickSort<int>(nmatr); });
progressBar1.Maximum = nmatr.Length;
t.Start();
}
private void buttonMergeSort_Click(object sender, EventArgs e)
{
string file = this.tb_see_1.Text;
int[] nmatr = ReadFile(file);
progressBar1.Value = 0;
t = new Thread(delegate() { MergeSort<int>(nmatr); });
progressBar1.Maximum = nmatr.Length;
t.Start();
}
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
buttonStop.PerformClick();
Application.Exit();
}
private void buttonStop_Click(object sender, EventArgs e)
{
t.Abort();
}
}
} |