forked from ProfessionalCSharp/ProfessionalCSharp7
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAlgorithms.cs
45 lines (41 loc) · 1.22 KB
/
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
using System;
namespace LocalFunctions
{
public static class Algorithms
{
public static void QuickSort<T>(T[] elements) where T : IComparable<T>
{
void Sort(int start, int end)
{
int i = start, j = end;
var pivot = elements[(start + end) / 2];
while (i <= j)
{
while (elements[i].CompareTo(pivot) < 0) i++;
while (elements[j].CompareTo(pivot) > 0) j--;
if (i <= j)
{
T tmp = elements[i];
elements[i] = elements[j];
elements[j] = tmp;
i++;
j--;
}
}
if (start < j) Sort(start, j);
if (i < end) Sort(i, end);
}
Sort(0, elements.Length - 1);
}
public static void WhenDoesItEnd()
{
Console.WriteLine(nameof(WhenDoesItEnd));
void InnerLoop(int ix)
{
Console.WriteLine(ix++);
InnerLoop(ix);
}
InnerLoop(1);
}
}
}