-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAlgorithms.cs
More file actions
322 lines (264 loc) · 7.39 KB
/
Algorithms.cs
File metadata and controls
322 lines (264 loc) · 7.39 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
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
using System;
namespace algorithms
{
public class Algorithms
{
public static double Degree(double num, int x)
{
double c = num;
int d = x;
double r = 1;
while (d > 0)
{
if (d % 2 == 1)
r *= c;
d = d / 2;
c *= c;
}
return r;
}
public static double DegreeMod(double num, int x, int mod)
{
double c = num % mod;
int d = x;
double r = 1;
while (d > 0)
{
if (d % 2 == 1)
r = (r * c) % mod;
d = d / 2;
c = (c * c) % mod;
}
return r;
}
public static int BitwiseDegree(int num, int x)
{
int c = num;
int d = x;
int r = 1;
while (d > 0)
{
if ((d & 00000001) == 1)
r *= c;
d = d >> 1;
c *= c;
}
return r;
}
public static int BitwiseDegreeMod(int num, int x, int mod)
{
int c = num % mod;
int d = x;
int r = 1;
while (d > 0)
{
if ((d & 00000001) == 1)
r = (r * c) % mod;
d = d >> 1;
c = (c * c) % mod;
}
return r;
}
public static int BinarySearch<T>(T[] arr, T value) where T : IComparable
{
int low = 0;
int high = arr.Length - 1;
while (low <= high)
{
int mid = low + (int)(high - low) / 2;
int compare = value.CompareTo(arr[mid]);
if (compare == 0)
{
return mid;
}
else if (compare > 0)
{
low = mid + 1;
}
else
{
high = mid - 1;
}
}
return -1;
}
public static long Factorial(int n)
{
if (n == 0)
return 1;
else
return n * Factorial(n - 1);
}
public static long Fibonacci(int n)
{
long a = 1, b = 0;
while (n > 0)
{
long t = a;
a += b;
b = t;
n--;
}
return b;
}
private static void Merge<T>(T[] arr, int start, int mid, int end) where T : IComparable
{
T[] temp = new T[end - start + 1];
int i = start, j = mid + 1, k = 0;
while (i <= mid && j <= end)
{
if (arr[i].CompareTo(arr[j]) <= 0)
{
temp[k++] = arr[i++];
}
else
{
temp[k++] = arr[j++];
}
}
while (i <= mid)
{
temp[k++] = arr[i++];
}
while (j <= end)
{
temp[k++] = arr[j++];
}
for (i = start; i <= end; i++)
{
arr[i] = temp[i - start];
}
}
private static void MergeSort<T>(T[] arr, int start, int end) where T : IComparable
{
if (start < end)
{
int mid = start + (int)(end - start)/2;
MergeSort(arr, start, mid);
MergeSort(arr, mid + 1, end);
Merge(arr, start, mid, end);
}
}
public static void MergeSort<T>(T[] arr) where T : IComparable
{
MergeSort(arr, 0, arr.Length - 1);
}
private static void Swap<T>(ref T x, ref T y)
{
T t = x;
x = y;
y = t;
}
private static int Partition<T>(T[] arr, int start, int end) where T : IComparable
{
int pivot = start - 1;
for (var i = start; i < end; i++)
{
if (arr[i].CompareTo(arr[end]) < 0)
{
pivot++;
Swap(ref arr[pivot], ref arr[i]);
}
}
pivot++;
Swap(ref arr[pivot], ref arr[end]);
return pivot;
}
private static void QuickSort<T>(T[] arr, int start, int end) where T : IComparable
{
if (start < end)
{
int pivotIndex = Partition(arr, start, end);
QuickSort(arr, start, pivotIndex - 1);
QuickSort(arr, pivotIndex + 1, end);
}
}
public static void QuickSort<T>(T[] arr) where T : IComparable
{
QuickSort(arr, 0, arr.Length - 1);
}
public static void SelectionSort<T>(T[] arr) where T: IComparable
{
for (int i = 0; i < arr.Length - 1; ++i)
{
int t = i;
for (int j = i + 1; j < arr.Length; ++j)
{
if (arr[j].CompareTo(arr[t]) < 0)
t = j;
}
if (t != i)
Swap(ref arr[i], ref arr[t]);
}
}
public static void InsertionSort<T>(T[] arr) where T : IComparable
{
for (int i = 1; i < arr.Length; ++i)
{
int j = i;
while (j > 0 && arr[j].CompareTo(arr[j - 1]) < 0)
{
Swap(ref arr[j], ref arr[j - 1]);
j--;
}
}
}
private static void Sink<T>(T[] arr, int i, int n) where T : IComparable
{
int k = i;
bool placed = false;
int j = 2 * k + 1;
while (!placed && j < n)
{
if (j < n - 1 && arr[j].CompareTo(arr[j + 1]) < 0)
{
j++;
}
if (arr[k].CompareTo(arr[j]) >= 0)
{
placed = true;
}
else
{
Swap(ref arr[k], ref arr[j]);
}
k = j;
j = 2 * k + 1;
}
}
public static void HeapSort<T>(T[] arr) where T : IComparable
{
int n = arr.Length;
for (int i = (n - 1) / 2; i > -1; i--)
{
Sink(arr, i, n);
}
while (n > 0)
{
Swap(ref arr[0], ref arr[n - 1]);
n--;
Sink(arr, 0, n);
}
}
public static int SecretarySearch<T>(T[] arr) where T : IComparable
{
int m = (int)(arr.Length / Math.E);
int c = 0;
for (int i = 1; i < m; i++)
{
if (arr[i].CompareTo(arr[c]) > 0)
{
c = i;
}
}
for (int i = m; i < arr.Length; i++)
{
if (arr[i].CompareTo(arr[c]) > 0)
{
return i;
}
}
return -1;
}
}
}