-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathHeapSort.cs
More file actions
150 lines (132 loc) · 5.25 KB
/
HeapSort.cs
File metadata and controls
150 lines (132 loc) · 5.25 KB
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
144
145
146
147
148
149
150
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System.Collections.Generic;
using System.Linq;
// Heap Sort performs an unstable in place sort.
// Time complexity: O(nlog n) as the heapify operation is O(log(n)) and it is performed n times.
// Space complexity: O(1) as it sorts in place.
// While slower than quick sort it guarantees a worst case performance of O(nlogn) vs. quick sort's O(n^2).
// While slower than merge sort it uses constant space vs. merge sort's O(n) space requirement.
// Compiled: Visual Studio 2013
namespace HeapSort
{
public static class HeapSort
{
public static void Sort<T>(this IList<T> toSort) where T : IComparable<T>
{
if (toSort == null) { throw new ArgumentNullException(); }
BuildHeap(toSort);
var heapSize = toSort.Count;
for (var i = toSort.Count - 1; i >= 0; i--)
{
// Since the max element is now in position one swap swap it with last element.
Swap(toSort, 0, i);
// Reduce the range of unsorted values by one.
heapSize--;
// The new root may violate the heap order property to so Adjust heap so run heapify again to restore order.
Heapify(toSort, 0, heapSize);
}
}
/// <summary>
/// Recursively work from middle to root check that for each level the parent node
/// is larger than its children. When this is complete the entire tree will be
/// ordered.
/// </summary>
private static void BuildHeap<T>(IList<T> collection) where T : IComparable<T>
{
var heapSize = collection.Count;
for (var i = heapSize / 2; i >= 0; i--)
{
Heapify(collection, i, heapSize);
}
}
/// <summary>
/// If the parent element at the current level is not greater than both of its children swap
/// and then work downwards through the levels ensuring the parent element is always larger
/// then the child elements at each level.
/// Complexity: O(log n)
/// </summary>
private static void Heapify<T>(IList<T> collection, int parentIdx, int heapSize) where T : IComparable<T>
{
int leftChildIdx = 2 * parentIdx + 1;
int rightChildIdx = 2 * parentIdx + 2;
int largest = parentIdx;
if (leftChildIdx < heapSize && collection[leftChildIdx].CompareTo(collection[parentIdx]) > 0)
{
largest = leftChildIdx;
}
if (rightChildIdx < heapSize && collection[rightChildIdx].CompareTo(collection[largest]) > 0)
{
largest = rightChildIdx;
}
if (largest != parentIdx)
{
// Move the larger child into the parent's position
Swap(collection, parentIdx, largest);
Heapify(collection, largest, heapSize);
}
}
/// <summary>
/// In place swap
/// </summary>
private static void Swap<T>(IList<T> collection, int x, int y)
{
var temp = collection[x];
collection[x] = collection[y];
collection[y] = temp;
}
}
[TestClass]
public class HeapSortTests
{
[TestMethod]
[ExpectedException(typeof(ArgumentNullException))]
public void WhenSortNull_ExpectException()
{
IList<int> empty = null;
HeapSort.Sort(empty);
}
[TestMethod]
public void WhenSortEmptyCollection_ExpectNoChange()
{
var values = new List<int>();
HeapSort.Sort(values);
Assert.IsTrue(values.SequenceEqual(new List<int>()));
}
[TestMethod]
public void WhenReversedSorted_ExpectNoLongerReversed()
{
var values = new List<int> { 3, 2, 1 };
HeapSort.Sort(values);
Assert.IsFalse(values.SequenceEqual(new[] { 3, 2, 1 }));
}
[TestMethod]
public void WhenSortIntValues_ExpectSortedCorrectly()
{
var testCases = new[]
{
Tuple.Create(new List<int> { 1, 2, 3 }, new List<int> { 1, 2, 3 }),
Tuple.Create(new List<int> { 2, 2, 3, 3, 3, 1, 4, 4, 4, 4 }, new List<int> { 1, 2, 2, 3, 3, 3, 4, 4, 4, 4 }),
};
foreach (var testCase in testCases)
{
HeapSort.Sort(testCase.Item1);
Assert.IsTrue(testCase.Item1.SequenceEqual(testCase.Item2));
}
}
[TestMethod]
public void WhenSortFloatValues_ExpectSortedCorrectly()
{
var testCases = new[]
{
Tuple.Create(new List<float> { 2.1F, 2.11F, 3.1F, 3.111F, 3.11F }, new List<float> { 2.1F, 2.11F, 3.1F, 3.11F, 3.111F }),
Tuple.Create(new List<float> { 2.1F, 2.2F, 3.3F, 3.2F, 3.1F, 1.1F }, new List<float> { 1.1F, 2.1F, 2.2F, 3.1F, 3.2F, 3.3F }),
};
foreach (var testCase in testCases)
{
HeapSort.Sort(testCase.Item1);
Assert.IsTrue(testCase.Item1.SequenceEqual(testCase.Item2));
}
}
}
}