两个相同类型已排序数据进行合并,虽然list数组中有AddRange方法,但它只是把第二个数组从第一个数组末尾插入,假如两个数组有重复数据,保存进去。
还有Union方法合并去重,首先会从第一个数组进行检查然后再把第二个数组数据从第一个数组依次从末尾插入,但相对于自定义类型排序还是不能有效解决问题。
归并排序是建立在归并操作上的一种有效的排序算法,该算法是采用分治法(Divide and Conquer)的一个非常典型的应用。将已有序的子序列合并,得到完全有序的序列;即先使每个子序列有序,再使子序列段间有序。若将两个有序表合并成一个有序表,称为二路归并。
归并过程为:比较a[i]和a[j]的大小,若a[i]≤a[j],则将第一个有序表中的元素a[i]复制到r[k]中,并令i和k分别加上1;否则将第二个有序表中的元素a[j]复制到r[k]中,并令j和k分别加上1,如此循环下去,直到其中一个有序表取完,然后再将另一个有序表中剩余的元素复制到r中从下标k到下标t的单元。归并排序的算法我们通常用递归实现,先把待排序区间[s,t]以中点二分,接着把左边子区间排序,再把右边子区间排序,最后把左区间和右区间用一次归并操作合并成有序的区间[s,t]。
Examples
using System;
using System.Collections.Generic;
using System.Linq; namespace Examples.Utils
{
public static class CollectionUtils
{
public static IList<T> MergeSortedLists<T>(Comparison<T> comparision, IList<T> list1, IList<T> list2)
{
var mergedList = new List<T>(list1.Count + list2.Count);
int i = , j = ;
while (i < list1.Count && j < list2.Count)
{
var result = comparision(list1[i], list2[j]);
if (result <= )
{
if (result == )
{
j++;
}
mergedList.Add(list1[i++]);
}
else
{
mergedList.Add(list2[j++]);
}
}
while (i < list1.Count)
{
mergedList.Add(list1[i++]);
}
while (j < list2.Count)
{
mergedList.Add(list2[j++]);
}
return mergedList;
}
}
}
TestExamples
using System;
using System.Collections.Generic;
using System.Linq;
using NUnit.Framework; namespace Examples.Utils.Tests
{
[TestFixture]
public class CollectionUtilsTests
{
[Test]
public void Merge2TestNoDuplicate()
{
var list1 = new List<int> {, , , , };
var list2 = new List<int> {, , , , };
var mergedList = CollectionUtils.MergeSortedLists(IntegerComparision, list1, list2);
var expectedList = GetExpectedMergedList(IntegerComparision, list1, list2);
Assert.AreEqual(expectedList.Count, mergedList.Count);
CollectionAssert.AreEqual(expectedList, mergedList);
} [Test]
public void Merge2TestWithDuplicates()
{
var list1 = new List<int> {, , , , };
var list2 = new List<int> {, , , , };
var mergedList = CollectionUtils.MergeSortedLists(IntegerComparision, list1, list2);
var expectedList = GetExpectedMergedList(IntegerComparision, list1, list2);
Assert.AreEqual(expectedList.Count, mergedList.Count);
CollectionAssert.AreEqual(expectedList, mergedList);
} [Test]
public void Merge2TestNoOverlap1()
{
var list1 = new List<int> {, , , , };
var list2 = new List<int> {, , , , };
var mergedList = CollectionUtils.MergeSortedLists(IntegerComparision, list1, list2);
var expectedList = GetExpectedMergedList(IntegerComparision, list1, list2);
Assert.AreEqual(expectedList.Count, mergedList.Count);
CollectionAssert.AreEqual(expectedList, mergedList);
} [Test]
public void Merge2TestNoOverlap2()
{
var list1 = new List<int> {, , , , };
var list2 = new List<int> {, , , , };
var mergedList = CollectionUtils.MergeSortedLists(IntegerComparision, list1, list2);
var expectedList = GetExpectedMergedList(IntegerComparision, list1, list2);
Assert.AreEqual(expectedList.Count, mergedList.Count);
CollectionAssert.AreEqual(expectedList, mergedList);
} private static IList<T> GetExpectedMergedList<T>(Comparison<T> comparision, params IList<T>[] lists)
{
var set = new SortedSet<T>(new ComparisionComparer<T>(comparision));
foreach (var list in lists)
{
foreach (var item in list)
{
if (!set.Contains(item))
{
set.Add(item);
}
}
}
return set.ToList();
} private static int IntegerComparision(int x, int y)
{
return y - x;
} private class ComparisionComparer<T> : IComparer<T>
{
private readonly Comparison<T> _comparision; public ComparisionComparer(Comparison<T> comparision)
{
_comparision = comparision;
} public int Compare(T x, T y)
{
return _comparision(x, y);
}
}
}
}