List array Merge to remove weight

Source: Internet
Author: User

C # Two-way list arrays Merge and remove weight

The same type of sorted data is merged, although there is a AddRange method in the list array, it simply inserts the second array from the end of the first array, assuming that two arrays have duplicate data to be saved in.
There is also the Union method to merge the deduplication, first check from the first array and then the second array data from the first array from the end of the insertion, but the relative to the custom type is not effective to solve the problem.

Merge sort is an effective sorting algorithm based on merging operation, which is a very typical application of the partition method (Divide and Conquer). The ordered Subsequence is merged to obtain a fully ordered sequence, i.e., the order of each subsequence is ordered, and then the sequence of sub-sequences is ordered. If two ordered tables are combined into an ordered table, they are called two-way merging.

The merge process is: Compare the size of a[i] and a[j], if A[I]≤A[J], then the first ordered table elements a[i] copied to r[k], and I and K plus 1; otherwise, the second ordered TABLE element A[j] copied to R[k], and the J and K respectively add 1, This loop continues until one of the ordered tables is finished, and then the remaining elements in the other ordered table are copied to the cells in R from subscript K to subscript t. Merging sorting algorithm we usually use recursive implementation, first to sort the interval [s,t] to the midpoint of the two points, then the left sub-range, then the right sub-range is sorted, and finally the left and right intervals with a merge operation into an orderly interval [s,t].

Examples

 1 using System; 2 using System.Collections.Generic; 3 using System.Linq;  4 5 Namespace Examples.utils 6 {7 public static class Collectionutils 8 {9 public static ilist<t>             Mergesortedlists<t> (comparison<t> comparision, ilist<t> list1, ilist<t> list2) 10 {11 var mergedlist = new List<t> (List1. Count + List2. Count); int i = 0, j = 0;13 while (i < List1. Count && J < List2. Count) (+ var result = comparision (List1[i], list2[j]), if (Result <=                     0) {if (result = = 0) {j++;21                 }22 Mergedlist.add (list1[i++]);}24 Else25 {Mergedlist.add (list2[j++]),}28}29 while (i &lt ; List1.           Count) 30  {Mergedlist.add (list1[i++]);}33 while (J < List2.         Count) {Mergedlist.add (list2[j++]);}37 return mergedlist;38 }39}40}

Testexamples

 1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using Nunit.framework; 5 6 Namespace Examples.Utils.Tests 7 {8 [Testfixture] 9 public class CollectionUtilsTests10 {one-by-one [Tes t]12 public void Merge2testnoduplicate () {list1 = new list<int> {100, 50, 20, 1 0, 1};15 var list2 = new List<int> {$,,, 5};16 var mergedlist = collectionutils. Mergesortedlists (Integercomparision, List1, list2); var expectedlist = Getexpectedmergedlist (integercomparis Ion, List1, List2); Assert.AreEqual (Expectedlist.count, mergedlist.count); collectionassert.ar         Eequal (Expectedlist, mergedlist);}21 [test]23 public void merge2testwithduplicates () 24 {var list1 = new List<int> {50, ten,, 1};26 var list2 = new list<int> 0, 1};27 var mergedlist = Collectionutils.mergesortedlists (Integercomparision, List1, list2); var expectedlist = Getexpectedmergedl             IST (integercomparision, List1, List2); Assert.AreEqual (Expectedlist.count, mergedlist.count); 30 Collectionassert.areequal (Expectedlist, mergedlist);}32 [test]34 public void Merge2testnoove  Rlap1 () {var list1 = new List<int> {$, +, +, 120};37 var list2 = new  list<int> {+, A, ten, 1};38 var mergedlist = collectionutils.mergesortedlists (Integercomparision, List1, List2); var expectedlist = Getexpectedmergedlist (integercomparision, List1, List2); Sert.         AreEqual (Expectedlist.count, Mergedlist.count); Collectionassert.areequal (Expectedlist, mergedlist); 42 }43 [test]45 public void Merge2testnooverlap2 () ~ $ var list1 = new List<in t> {100, 50, 20, 1};48 var list2 = new List<int> {$, +, +, 120};49 var mergedlist = Collectio Nutils.mergesortedlists (Integercomparision, List1, list2); var expectedlist = Getexpectedmergedlist (Integerc Omparision, List1, List2); Assert.AreEqual (Expectedlist.count, mergedlist.count); Collectionas Sert. AreEqual (Expectedlist, mergedlist),}54, and Static ilist<t> getexpectedmergedlist<t> ( comparison<t> comparision, params ilist<t>[] lists).                 (New comparisioncomparer<t> (comparision)); (var list in lists) 59 {60 foreach (var item in list) (!set. Contains (item)). ADD (item),}66}67}68 return set.     ToList (); 69    }70-private static int integercomparision (int x, int y), {y-x;74} Comparisioncomparer<t> Private class: icomparer<t>77 {readonly C             Omparison<t> _comparision;79 comparisioncomparer (comparison<t> comparision) 81             {_comparision = comparision;83}84-public int Compare (t x, T y) 86 {_comparision return (x, y); 88}89}90}91}

List array Merge to remove weight

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.