/// <summary> /// Merges two lists of copies. Each time when the book from list "b" is inserted into the list "a", /// it is put into the right place by a comparer /// First we try to insert the book at the end of the list "a", then we try to insert it into other place. /// </summary> /// <param name="a"></param> /// <param name="b"></param> /// <returns></returns> public List <Copy> Merge(List <Copy> a, List <Copy> b) { BookSorter bookSorter = new BookSorter(); int i = 0; int j = a.Count - 1; foreach (Copy copy in b) { while (true) { if (bookSorter.Compare(a[j], b[i]) == -1 && j != 0) { j--; continue; } a.Insert(j, b[i]); j = a.Count - 1; i++; break; } } return(a); }
public List <Copy> MergeSort(List <Copy> filter, int count) { BookSorter bookSorter = new BookSorter(); if (count == 1) { filter.Sort(bookSorter); return(filter); } int temp = count / 2; count -= temp; List <Copy> first = filter.GetRange(0, filter.Count / 2); List <Copy> second = filter.GetRange(filter.Count / 2, filter.Count / 2); Thread thread = new Thread(() => second = MergeSort(second, count)); thread.Start(); first = MergeSort(first, count); thread.Join(); return(Merge(first, second)); }