-
-
Notifications
You must be signed in to change notification settings - Fork 294
Expand file tree
/
Copy pathTreeSort.cs
More file actions
28 lines (24 loc) · 744 Bytes
/
Copy pathTreeSort.cs
File metadata and controls
28 lines (24 loc) · 744 Bytes
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
using System;
using System.Collections.Generic;
using System.Linq;
using Advanced.Algorithms.DataStructures;
namespace Advanced.Algorithms.Sorting;
/// <summary>
/// A tree sort implementation.
/// </summary>
public class TreeSort<T> where T : IComparable
{
private TreeSort()
{
}
/// <summary>
/// Time complexity: O(nlog(n)).
/// </summary>
public static IEnumerable<T> Sort(IEnumerable<T> enumerable, SortDirection sortDirection = SortDirection.Ascending)
{
//create BST
var tree = new RedBlackTree<T>();
foreach (var item in enumerable) tree.Insert(item);
return sortDirection == SortDirection.Ascending ? tree.AsEnumerable() : tree.AsEnumerableDesc();
}
}