tis merge niet heap
This commit is contained in:
parent
a0a8184dec
commit
c1b42cc04d
|
@ -7,33 +7,33 @@ namespace Sorting.Tests
|
||||||
public class SortingTests
|
public class SortingTests
|
||||||
{
|
{
|
||||||
[TestMethod]
|
[TestMethod]
|
||||||
public void HeapSorting_MergeSortedLists_EachListHasDifferentAmountOfNumbers()
|
public void MergeSorting_MergeSortedLists_EachListHasDifferentAmountOfNumbers()
|
||||||
{
|
{
|
||||||
var sort = new HeapSort();
|
var sort = new MergeSort();
|
||||||
var result = sort.MergeSortedLists(new List<int> { 3 }, new List<int> { 2, 4 });
|
var result = sort.MergeSortedLists(new List<int> { 3 }, new List<int> { 2, 4 });
|
||||||
|
|
||||||
CollectionAssert.AreEqual(new List<int> { 2, 3, 4 }, result);
|
CollectionAssert.AreEqual(new List<int> { 2, 3, 4 }, result);
|
||||||
}
|
}
|
||||||
|
|
||||||
[TestMethod]
|
[TestMethod]
|
||||||
public void HeapSorting_MergeSortedLists_EachListHasSameAmountOfNumbers()
|
public void MergeSorting_MergeSortedLists_EachListHasSameAmountOfNumbers()
|
||||||
{
|
{
|
||||||
var sort = new HeapSort();
|
var sort = new MergeSort();
|
||||||
var result = sort.MergeSortedLists(new List<int> { 1, 3 }, new List<int> { 2, 4 });
|
var result = sort.MergeSortedLists(new List<int> { 1, 3 }, new List<int> { 2, 4 });
|
||||||
|
|
||||||
CollectionAssert.AreEqual(new List<int> { 1, 2, 3, 4 }, result);
|
CollectionAssert.AreEqual(new List<int> { 1, 2, 3, 4 }, result);
|
||||||
}
|
}
|
||||||
|
|
||||||
[TestMethod]
|
[TestMethod]
|
||||||
public void HeapSorting_SimpleListDividesNeatlyIntoTwo()
|
public void MergeSorting_SimpleListDividesNeatlyIntoTwo()
|
||||||
{
|
{
|
||||||
ExecuteTestWithSortMethod(new HeapSort(), new List<int> { 4, 3, 2, 1 }, new List<int> { 1, 2, 3, 4 });
|
ExecuteTestWithSortMethod(new MergeSort(), new List<int> { 4, 3, 2, 1 }, new List<int> { 1, 2, 3, 4 });
|
||||||
}
|
}
|
||||||
|
|
||||||
[TestMethod]
|
[TestMethod]
|
||||||
public void HeapSorting_SimpleListUnevenNumbers()
|
public void MergeSorting_SimpleListUnevenNumbers()
|
||||||
{
|
{
|
||||||
ExecuteTestWithSortMethod(new HeapSort(), new List<int> { 5, 7, 6, 4, 3, 2, 1 }, new List<int> { 1, 2, 3, 4, 5, 6, 7 });
|
ExecuteTestWithSortMethod(new MergeSort(), new List<int> { 5, 7, 6, 4, 3, 2, 1 }, new List<int> { 1, 2, 3, 4, 5, 6, 7 });
|
||||||
}
|
}
|
||||||
|
|
||||||
private void ExecuteTestWithSortMethod(ISortable sortable, List<int> toSort, List<int> expected)
|
private void ExecuteTestWithSortMethod(ISortable sortable, List<int> toSort, List<int> expected)
|
||||||
|
|
|
@ -0,0 +1,88 @@
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Diagnostics;
|
||||||
|
using System.Linq;
|
||||||
|
|
||||||
|
namespace Sorting
|
||||||
|
{
|
||||||
|
public class MergeSort : ISortable
|
||||||
|
{
|
||||||
|
public List<int> Sort(List<int> list)
|
||||||
|
{
|
||||||
|
return SortRecur(list);
|
||||||
|
}
|
||||||
|
|
||||||
|
internal List<int> MergeSortedLists(List<int> a, List<int> b)
|
||||||
|
{
|
||||||
|
var result = new List<int>();
|
||||||
|
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<int> AddRemainder(List<int> list, int index)
|
||||||
|
{
|
||||||
|
List<int> result = new List<int>();
|
||||||
|
for (var i = index; i < list.Count; i++)
|
||||||
|
{
|
||||||
|
result.Add(list[i]);
|
||||||
|
}
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
private List<int> SortRecur(List<int> list)
|
||||||
|
{
|
||||||
|
Debug.WriteLine("sorting " + list.AsString());
|
||||||
|
|
||||||
|
if (list.Count == 2)
|
||||||
|
{
|
||||||
|
return new List<int>
|
||||||
|
{
|
||||||
|
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));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,15 +0,0 @@
|
||||||
using System;
|
|
||||||
using System.Collections.Generic;
|
|
||||||
using System.Linq;
|
|
||||||
using System.Text;
|
|
||||||
using System.Threading.Tasks;
|
|
||||||
|
|
||||||
namespace Sorting
|
|
||||||
{
|
|
||||||
class Program
|
|
||||||
{
|
|
||||||
static void Main(string[] args)
|
|
||||||
{
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
Loading…
Reference in New Issue