-
Notifications
You must be signed in to change notification settings - Fork 294
/
Copy pathHeapSort.cs
46 lines (41 loc) · 1.11 KB
/
HeapSort.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
/* Unmerged change from project 'Advanced.Algorithms (netstandard1.0)'
Before:
using System;
After:
using Advanced.Algorithms.DataStructures;
using System;
*/
using System;
using System.Collections.Generic;
using Advanced.Algorithms.DataStructures;
/* Unmerged change from project 'Advanced.Algorithms (netstandard1.0)'
Before:
using System.Linq;
using Advanced.Algorithms.DataStructures;
After:
using System.Linq;
*/
namespace Advanced.Algorithms.Sorting;
/// <summary>
/// A heap sort implementation.
/// </summary>
public class HeapSort<T> where T : IComparable
{
/// <summary>
/// Time complexity: O(nlog(n)).
/// </summary>
public static T[] Sort(ICollection<T> collection, SortDirection sortDirection = SortDirection.Ascending)
{
//heapify
var heap = new BHeap<T>(sortDirection, collection);
//now extract min until empty and return them as sorted array
var sortedArray = new T[collection.Count];
var j = 0;
while (heap.Count > 0)
{
sortedArray[j] = heap.Extract();
j++;
}
return sortedArray;
}
}