Advertisement
Guest User

Untitled

a guest
Jan 2nd, 2014
157
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.24 KB | None | 0 0
  1. using System;
  2. using System.Text;
  3.  
  4. namespace Algorithm {
  5.     class Program {
  6.         static void Main(string[] args) {
  7.             // Random 10,000,000 items
  8.             int[] list = new int[10000000];
  9.             Random rnd = new Random();
  10.  
  11.             for (int i = 0; i < 10000000; i++) {
  12.                 list[i] = rnd.Next(0, int.MaxValue);
  13.             }
  14.  
  15.             System.Diagnostics.Stopwatch sw = new System.Diagnostics.Stopwatch();
  16.  
  17.             int[] tmp = (int[])list.Clone();
  18.             sw.Start();
  19.             MergeSortAlgorithm.MergeInsertionSort(tmp);
  20.             sw.Stop();
  21.             Console.WriteLine("Merge-Insertion Sort: " + sw.ElapsedMilliseconds + " millioseonds");
  22.  
  23.             tmp = (int[])list.Clone();
  24.             sw.Reset();
  25.             sw.Start();
  26.             MergeSortAlgorithm.MergeSort(tmp);
  27.             sw.Stop();
  28.             Console.WriteLine("Merge Sort: " + sw.ElapsedMilliseconds + " millioseonds");      
  29.         }
  30.     }
  31.  
  32.     class MergeSortAlgorithm {
  33.         public static void MergeSort(int[] array) {
  34.             int[] aux = new int[array.Length];
  35.             MergeSort(array, aux, 0, array.Length - 1);
  36.         }
  37.  
  38.         public static void MergeSort(int[] array, int[] aux, int low, int high) {
  39.             if (low >= high) return;
  40.  
  41.             int mid = (low + high) / 2;
  42.  
  43.             MergeSort(array, aux, low, mid);
  44.             MergeSort(array, aux, mid + 1, high);
  45.  
  46.             Merge(array, aux, low, mid, high);
  47.         }
  48.  
  49.         protected static void Merge(int[] array, int[] aux, int low, int mid, int high) {
  50.             // copy into aux array
  51.             for (int i = low; i <= high; i++) aux[i] = array[i];
  52.  
  53.             // merge
  54.             int j = low, k = mid + 1;
  55.             for (int o = low; o <= high; o++) {
  56.                 if (j > mid)
  57.                     array[o] = aux[k++];
  58.                 else if (k > high)
  59.                     array[o] = aux[j++];
  60.                 else if (aux[k] < aux[j])
  61.                     array[o] = aux[k++];
  62.                 else
  63.                     array[o] = aux[j++];
  64.             }
  65.         }
  66.  
  67.         public static void MergeInsertionSort(int[] array) {
  68.             MergeInsertionSort(array, 0, array.Length - 1);
  69.         }
  70.  
  71.         public static void MergeInsertionSort(int[] array, int low, int high) {
  72.             if (low >= high) return;
  73.             if (low + 1 == high) {
  74.                 // sort two elements
  75.                 if (array[low] > array[high]) {
  76.                     int tmp = array[low];
  77.                     array[low] = array[high];
  78.                     array[high] = tmp;
  79.                 }
  80.             } else {
  81.                 int mid = (low + high) / 2;
  82.  
  83.                 MergeInsertionSort(array, low, mid);
  84.                 MergeInsertionSort(array, mid + 1, high);
  85.  
  86.                 // do insertion sort
  87.                 for (int i = mid + 1, j; i <= high; i++) {
  88.                     int ins = array[low];
  89.  
  90.                     // move the element into correct position
  91.                     for (j = i - 1; (j >= low) && (ins < array[j]); j--) {
  92.                         array[j + 1] = array[j];
  93.                     }
  94.  
  95.                     array[j + 1] = ins;
  96.                 }
  97.             }
  98.         }
  99.     }
  100. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement