-
Notifications
You must be signed in to change notification settings - Fork 294
/
Copy pathQuickSort.cs
57 lines (46 loc) · 1.67 KB
/
QuickSort.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
using System;
using System.Collections.Generic;
namespace Advanced.Algorithms.Sorting;
/// <summary>
/// A quick sort implementation.
/// </summary>
public class QuickSort<T> where T : IComparable
{
/// <summary>
/// Time complexity: O(n^2)
/// </summary>
public static T[] Sort(T[] array, SortDirection sortDirection = SortDirection.Ascending)
{
if (array.Length <= 1) return array;
var comparer = new CustomComparer<T>(sortDirection, Comparer<T>.Default);
Sort(array, 0, array.Length - 1, comparer);
return array;
}
private static void Sort(T[] array, int startIndex, int endIndex, CustomComparer<T> comparer)
{
while (true)
{
//if only one element the do nothing
if (startIndex < 0 || endIndex < 0 || endIndex - startIndex < 1) return;
//set the wall to the left most index
var wall = startIndex;
//pick last index element on array as comparison pivot
var pivot = array[endIndex];
//swap elements greater than pivot to the right side of wall
//others will be on left
for (var j = wall; j <= endIndex; j++)
{
if (comparer.Compare(pivot, array[j]) <= 0 && j != endIndex) continue;
var temp = array[wall];
array[wall] = array[j];
array[j] = temp;
//increment to exclude the minimum element in subsequent comparisons
wall++;
}
//sort left
Sort(array, startIndex, wall - 2, comparer);
//sort right
startIndex = wall;
}
}
}