forked from TheAlgorithms/C-Sharp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
QuickSorter.cs
66 lines (58 loc) · 1.93 KB
/
QuickSorter.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
using System.Collections.Generic;
namespace Algorithms.Sorters.Comparison
{
/// <summary>
/// Sorts arrays using quicksort.
/// </summary>
/// <typeparam name="T">Type of array element.</typeparam>
public class QuickSorter<T> : IComparisonSorter<T>
{
/// <summary>
/// Sorts array using Hoare partition scheme,
/// internal, in-place,
/// time complexity average: O(n log(n)),
/// time complexity worst: O(n^2),
/// space complexity: O(log(n)),
/// where n - array length.
/// </summary>
/// <param name="array">Array to sort.</param>
/// <param name="comparer">Compares elements.</param>
public void Sort(T[] array, IComparer<T> comparer) => Sort(array, comparer, 0, array.Length - 1);
private void Sort(T[] array, IComparer<T> comparer, int left, int right)
{
if (left >= right)
{
return;
}
var p = Partition(array, comparer, left, right);
Sort(array, comparer, left, p);
Sort(array, comparer, p + 1, right);
}
private int Partition(T[] array, IComparer<T> comparer, int left, int right)
{
var pivot = array[left + (right - left) / 2];
var nleft = left;
var nright = right;
while (true)
{
while (comparer.Compare(array[nleft], pivot) < 0)
{
nleft++;
}
while (comparer.Compare(array[nright], pivot) > 0)
{
nright--;
}
if (nleft >= nright)
{
return nright;
}
var t = array[nleft];
array[nleft] = array[nright];
array[nright] = t;
nleft++;
nright--;
}
}
}
}