-
Notifications
You must be signed in to change notification settings - Fork 0
/
sorting algorithms.cs
225 lines (198 loc) · 7.51 KB
/
sorting algorithms.cs
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
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ProjektSortowanie
{
class Program
{
static void SelectionSort(int[] tab)
{
for (int i = 0; i < tab.Length; i++)
for (int j = i + 1; j < tab.Length; j++)
if (tab[i] > tab[j])
(tab[i], tab[j]) = (tab[j], tab[i]);
}
static void InsertionSort(int[] tab)
{
for (int i = 1; i < tab.Length; i++)
for (int j = i; j > 0 && tab[j] < tab[j - 1]; j--)
(tab[j], tab[j - 1]) = (tab[j - 1], tab[j]);
}
static void QuickSortImpl(int[] tab, int left, int right)
{
if (left >= right)
return;
int pivot = tab[left];
int l = left - 1;
int r = right + 1;
while (l < r)
{
l++;
r--;
while (tab[l] < pivot)
l++;
while (tab[r] > pivot)
r--;
(tab[l], tab[r]) = (tab[r], tab[l]);
}
(tab[l], tab[r]) = (tab[r], tab[l]);
QuickSortImpl(tab, left, r);
QuickSortImpl(tab, r + 1, right);
}
static void QuickSort(int[] tab)
{
QuickSortImpl(tab, 0, tab.Length - 1);
}
static void MergeSortImpl(int[] tab, int l, int r)
{
if (l >= r)
return;
int mid = (l + r) / 2;
MergeSortImpl(tab, l, mid);
MergeSortImpl(tab, mid + 1, r);
var outTab = new int[r - l + 1];
int first = l, second = mid + 1, outIndex = 0;
while (first <= mid && second <= r)
{
if (tab[first] < tab[second])
{
outTab[outIndex++] = tab[first++];
}
else
{
outTab[outIndex++] = tab[second++];
}
}
while (first <= mid)
outTab[outIndex++] = tab[first++];
while (second <= r)
outTab[outIndex++] = tab[second++];
for (int i = 0; i < outTab.Length; i++)
tab[l + i] = outTab[i];
}
static void MergeSort(int[] tab)
{
MergeSortImpl(tab, 0, tab.Length - 1);
}
static void ShellMethodSort(int[] tab)
{
int i, j, gap, x;
gap = 1;
while (gap >= tab.Length)
{
gap = 3 * gap + 1;
}
if (gap == 0)
gap = 1;
while (gap > 0)
{
for (j = tab.Length - gap; j >= 0; j--)
{
x = tab[j];
i = j + gap;
while ((i < tab.Length) && (x > tab[i]))
{
tab[i - gap] = tab[i];
i = i + gap;
}
tab[i - gap] = x;
}
gap = gap / 3;
}
}
static void Main(string[] args)
{
Stopwatch stopwatch = new Stopwatch();
//klucze rosnące, malejące, V-kształtne, A-kształtne, stałe, losowe
Console.WriteLine("n;rosnące;malejące;V-kształtne;A-kształtne;stałe;losowe");
for (int num_of_elems = 1000; num_of_elems <= 10000; num_of_elems += 1000)
{
//dane rosnące
int[] data_asc = new int[num_of_elems];
for (int i = 0; i < num_of_elems; i++)
data_asc[i] = i;
stopwatch.Reset();
stopwatch.Start();
SelectionSort(data_asc);
stopwatch.Stop();
long time_data_asc = stopwatch.ElapsedMilliseconds;
//dane malejące
int[] data_desc = new int[num_of_elems];
List<int> data_desc_list = new List<int>();
for (int i = num_of_elems - 1; i >= 0; i--)
data_desc_list.Add(i);
data_desc = data_desc_list.ToArray();
stopwatch.Reset();
stopwatch.Start();
SelectionSort(data_desc);
stopwatch.Stop();
long time_data_desc = stopwatch.ElapsedMilliseconds;
//dane V-kształtne
int[] data_V = new int[num_of_elems];
List<int> data_V_list = new List<int>();
int tmp_num_of_elems = num_of_elems / 2;
for (int i = tmp_num_of_elems; i >= 0; i--)
data_V_list.Add(i);
for (int i = tmp_num_of_elems + 1; i < num_of_elems; i++)
data_V_list.Add(i);
data_V = data_V_list.ToArray();
stopwatch.Reset();
stopwatch.Start();
SelectionSort(data_V);
stopwatch.Stop();
long time_data_V = stopwatch.ElapsedMilliseconds;
//dane A-kształtne
int[] data_A = new int[num_of_elems];
List<int> data_A_list = new List<int>();
tmp_num_of_elems = num_of_elems / 2;
for (int i = tmp_num_of_elems + 1; i < num_of_elems; i++)
data_A_list.Add(i);
for (int i = tmp_num_of_elems; i >= 0; i--)
data_A_list.Add(i);
data_A = data_A_list.ToArray();
stopwatch.Reset();
stopwatch.Start();
SelectionSort(data_A);
stopwatch.Stop();
long time_data_A = stopwatch.ElapsedMilliseconds;
//dane stałe
int[] data_static = new int[num_of_elems];
for (int i = 0; i < num_of_elems; i++)
data_static[i] = 1;
stopwatch.Reset();
stopwatch.Start();
SelectionSort(data_static);
stopwatch.Stop();
long time_data_static = stopwatch.ElapsedMilliseconds;
//dane losowe
int[] data_random = new int[num_of_elems];
Random gen = new Random();
int n;
bool flag;
for (int i = 0; i < num_of_elems; i++)
{
flag = true;
while (flag)
{
n = gen.Next(0, num_of_elems + 1);
if (false == Array.Exists(data_random, element => element == n))
{
data_random[i] = n;
flag = false;
}
}
}
stopwatch.Reset();
stopwatch.Start();
SelectionSort(data_random);
stopwatch.Stop();
long time_data_random = stopwatch.ElapsedMilliseconds;
Console.WriteLine($"{num_of_elems};{time_data_asc}ms;{time_data_desc}ms;{time_data_V}ms;{time_data_A}ms;{time_data_static}ms;{time_data_random}ms");
}
Console.ReadLine();
}
}
}