-
Notifications
You must be signed in to change notification settings - Fork 0
/
IListExtension.cs
58 lines (46 loc) · 1.58 KB
/
IListExtension.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
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Net.WebSockets;
using System.Text;
using System.Threading.Tasks;
namespace QuickSort
{
internal static class IListExtension
{
public static void QuickSort<T>(this IList<T> list) where T : IComparable
{
if (list.Count < 2)
return;
var pivotIndex = list.Count / 2;
var lessList = new List<T>(pivotIndex);
var largerList = new List<T>(pivotIndex);
var pivotEqualsList = new List<T>(1);
foreach (var item in list)
{
var compareResult = item.CompareTo(list[pivotIndex]);
if (compareResult < 0)
{
lessList.Add(item);
continue;
}
if (compareResult > 0)
{
largerList.Add(item);
continue;
}
pivotEqualsList.Add(item);
}
lessList.QuickSort();
largerList.QuickSort();
var currentIndex = 0;
for (; currentIndex < lessList.Count; currentIndex++)
list[currentIndex] = lessList[currentIndex];
for (int i = 0; i < pivotEqualsList.Count; i++, currentIndex++)
list[currentIndex] = pivotEqualsList[i];
for (int i = 0; i < largerList.Count; i++, currentIndex++)
list[currentIndex] = largerList[i];
}
}
}