diff --git a/sorting/dotnet/Sorting/HeapSort.cs b/sorting/dotnet/Sorting/HeapSort.cs deleted file mode 100644 index 288732d..0000000 --- a/sorting/dotnet/Sorting/HeapSort.cs +++ /dev/null @@ -1,96 +0,0 @@ -using System.Collections.Generic; -using System.Diagnostics; -using System.Linq; - -namespace Sorting -{ - public static class ListExt - { - public static string AsString(this List list) - { - return "(" + string.Join(", ", list.Select(x => x.ToString())) + ")"; - } - } - - public class HeapSort : ISortable - { - public List Sort(List list) - { - return SortRecur(list); - } - - internal List MergeSortedLists(List a, List b) - { - var result = new List(); - var aCounter = 0; - var bCounter = 0; - - Debug.WriteLine("merging" + a.AsString() + " with " + b.AsString()); - - while (aCounter < a.Count && bCounter < b.Count) - { - var currA = a[aCounter]; - var currB = b[bCounter]; - - if (currA < currB) - { - result.Add(currA); - aCounter++; - } - else - { - result.Add(currB); - bCounter++; - } - } - - if (aCounter < a.Count) - { - result.AddRange(AddRemainder(a, aCounter)); - } - if (bCounter < b.Count) - { - result.AddRange(AddRemainder(b, bCounter)); - } - - return result; - } - - private List AddRemainder(List list, int index) - { - List result = new List(); - for (var i = index; i < list.Count; i++) - { - result.Add(list[i]); - } - - return result; - } - - private List SortRecur(List list) - { - Debug.WriteLine("sorting " + list.AsString()); - - if (list.Count == 2) - { - return new List - { - list[0] < list[1] ? list[0] : list[1], - list[0] < list[1] ? list[1] : list[0] - }; - } - if (list.Count <= 1) - { - return list; - } - - var halfIndex = list.Count / 2; - - var left = list.TakeWhile((number, i) => i < halfIndex).ToList(); - var right = list.Except(left).ToList(); - return MergeSortedLists( - SortRecur(left), - SortRecur(right)); - } - } -} \ No newline at end of file