Advertisement
Guest User

Untitled

a guest
Oct 4th, 2015
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.61 KB | None | 0 0
  1. namespace Sortable_Collection.Sorters
  2. {
  3.     using Contracts;
  4.     using System;
  5.     using System.Collections.Generic;
  6.     using System.Threading.Tasks;
  7.  
  8.     public class AsyncMergeSorter<T> : ISorter<T> where T : IComparable<T>
  9.     {
  10.         public void Sort(IList<T> collection)
  11.         {
  12.             if (collection.Count <= 1)
  13.             {
  14.                 return;
  15.             }
  16.  
  17.             T[] workArray = new T[collection.Count];
  18.             SplitMerge(collection, workArray, 0, collection.Count);
  19.         }
  20.  
  21.         private void SplitMerge(IList<T> collection, T[] tempArray, int start, int end)
  22.         {
  23.             if (end - start < 2)
  24.             {
  25.                 return;
  26.             }
  27.  
  28.             int midd = (start + end) / 2;
  29.  
  30.             if (end - start < 15000)
  31.             {
  32.                 SplitMerge(collection, tempArray, start, midd);
  33.                 SplitMerge(collection, tempArray, midd, end);
  34.             }
  35.             else
  36.             {
  37.                 Task t1 = Task.Factory.StartNew(() => SplitMerge(collection, tempArray, start, midd));
  38.                 Task t2 = Task.Factory.StartNew(() => SplitMerge(collection, tempArray, midd, end));
  39.                 Task.WaitAll(t1, t2);
  40.             }
  41.  
  42.  
  43.             //SplitMerge(collection, tempArray, start, midd);
  44.             //SplitMerge(collection, tempArray, midd, end);
  45.             Merge(collection, tempArray, start, midd, end);
  46.  
  47.             CoppyArray(collection, tempArray, start, end);
  48.         }
  49.  
  50.         private void Merge(IList<T> collection, T[] tempArray, int start, int midd, int end)
  51.         {
  52.             int first = start;
  53.             int second = midd;
  54.  
  55.             for (int i = start; i < end; i++)
  56.             {
  57.                 if (first >= midd)
  58.                 {
  59.                     tempArray[i] = collection[second++];
  60.                 }
  61.                 else if (second >= end)
  62.                 {
  63.                     tempArray[i] = collection[first++];
  64.                 }
  65.                 else
  66.                 {
  67.                     if (collection[first].CompareTo(collection[second]) <= 0)
  68.                     {
  69.                         tempArray[i] = collection[first++];
  70.                     }
  71.                     else
  72.                     {
  73.                         tempArray[i] = collection[second++];
  74.                     }
  75.                 }
  76.             }
  77.         }
  78.  
  79.         private void CoppyArray(IList<T> collection, T[] tempArray, int start, int end)
  80.         {
  81.             for (int i = start; i < end; i++)
  82.             {
  83.                 collection[i] = tempArray[i];
  84.             }
  85.         }
  86.     }
  87. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement