forked from aalhour/C-Sharp-Algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
/
HeapSorter.cs
143 lines (121 loc) · 5.01 KB
/
HeapSorter.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
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
using System.Collections.Generic;
using Algorithms.Common;
namespace Algorithms.Sorting
{
public static class HeapSorter
{
/// <summary>
/// Public API: Default functionality.
/// Sorts in ascending order. Uses Max-Heaps.
/// </summary>
public static void HeapSort<T>(this IList<T> collection, Comparer<T> comparer = null)
{
collection.HeapSortAscending(comparer);
}
/// <summary>
/// Public API: Sorts ascending
/// Uses Max-Heaps
/// </summary>
public static void HeapSortAscending<T>(this IList<T> collection, Comparer<T> comparer = null)
{
// Handle the comparer's default null value
comparer = comparer ?? Comparer<T>.Default;
int lastIndex = collection.Count - 1;
collection.BuildMaxHeap(0, lastIndex, comparer);
while (lastIndex >= 0)
{
collection.Swap(0, lastIndex);
lastIndex--;
collection.MaxHeapify(0, lastIndex, comparer);
}
}
/// <summary>
/// Public API: Sorts ascending
/// Uses Min-Heaps
/// </summary>
public static void HeapSortDescending<T>(this IList<T> collection, Comparer<T> comparer = null)
{
// Handle the comparer's default null value
comparer = comparer ?? Comparer<T>.Default;
int lastIndex = collection.Count - 1;
collection.BuildMinHeap(0, lastIndex, comparer);
while (lastIndex >= 0)
{
collection.Swap(0, lastIndex);
lastIndex--;
collection.MinHeapify(0, lastIndex, comparer);
}
}
/****************************************************************************/
/// <summary>
/// Private Max-Heap Builder.
/// Builds a max heap from an IList<T> collection.
/// </summary>
private static void BuildMaxHeap<T>(this IList<T> collection, int firstIndex, int lastIndex, Comparer<T> comparer)
{
int lastNodeWithChildren = lastIndex / 2;
for (int node = lastNodeWithChildren; node >= 0; --node)
{
collection.MaxHeapify(node, lastIndex, comparer);
}
}
/// <summary>
/// Private Max-Heapifier. Used in BuildMaxHeap.
/// Heapfies the elements between two indexes (inclusive), maintaining the maximum at the top.
/// </summary>
private static void MaxHeapify<T>(this IList<T> collection, int nodeIndex, int lastIndex, Comparer<T> comparer)
{
// assume left(i) and right(i) are max-heaps
int left = (nodeIndex * 2) + 1;
int right = left + 1;
int largest = nodeIndex;
// If collection[left] > collection[nodeIndex]
if (left <= lastIndex && comparer.Compare(collection[left], collection[nodeIndex]) > 0)
largest = left;
// If collection[right] > collection[largest]
if (right <= lastIndex && comparer.Compare(collection[right], collection[largest]) > 0)
largest = right;
// Swap and heapify
if (largest != nodeIndex)
{
collection.Swap(nodeIndex, largest);
collection.MaxHeapify(largest, lastIndex, comparer);
}
}
/// <summary>
/// Private Min-Heap Builder.
/// Builds a min heap from an IList<T> collection.
/// </summary>
private static void BuildMinHeap<T>(this IList<T> collection, int firstIndex, int lastIndex, Comparer<T> comparer)
{
int lastNodeWithChildren = lastIndex / 2;
for (int node = lastNodeWithChildren; node >= 0; --node)
{
collection.MinHeapify(node, lastIndex, comparer);
}
}
/// <summary>
/// Private Min-Heapifier. Used in BuildMinHeap.
/// Heapfies the elements between two indexes (inclusive), maintaining the minimum at the top.
/// </summary>
private static void MinHeapify<T>(this IList<T> collection, int nodeIndex, int lastIndex, Comparer<T> comparer)
{
// assume left(i) and right(i) are max-heaps
int left = (nodeIndex * 2) + 1;
int right = left + 1;
int smallest = nodeIndex;
// If collection[left] > collection[nodeIndex]
if (left <= lastIndex && comparer.Compare(collection[left], collection[nodeIndex]) < 0)
smallest = left;
// If collection[right] > collection[largest]
if (right <= lastIndex && comparer.Compare(collection[right], collection[smallest]) < 0)
smallest = right;
// Swap and heapify
if (smallest != nodeIndex)
{
collection.Swap(nodeIndex, smallest);
collection.MinHeapify(smallest, lastIndex, comparer);
}
}
}
}