Advertisement
Guest User

Untitled

a guest
Mar 31st, 2015
203
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.09 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using System.Diagnostics;
  7.  
  8. namespace CV07
  9. {
  10.     class Program
  11.     {
  12.  
  13.         static int len = 10000000;
  14.  
  15.         static void QuickSort_lr(int[] p, int l, int r)
  16.         {
  17.             if (l >= r) return;
  18.             var pivot = (l + r) / 2;
  19.  
  20.             //menší přesunu před pivot, větší za pivot
  21.             var i = l;
  22.             var j = r;
  23.             var v = p[pivot];
  24.  
  25.             while (i <= j)
  26.             {
  27.                 while (p[i] < v) i++; //hledám chybu zleva
  28.                 while (p[j] > v) j--; //hledám chybu zprava
  29.                 if (i > j) break; //test na ukončení opakování
  30.                 //spravím chyby tj. prohodím p[i] s p[j]
  31.                 var x = p[i];
  32.                 p[i] = p[j];
  33.                 p[j] = x;
  34.                 i++;
  35.                 j--;
  36.             }
  37.  
  38.             QuickSortP_lr(p, l, j);
  39.             QuickSortP_lr(p, i, r);
  40.         }
  41.  
  42.         static void QuickSort(int[] p)
  43.         {
  44.             QuickSort_lr(p, 0, p.Length - 1);
  45.         }
  46.  
  47.         static void QuickSortP_lr(int[] p, int l, int r)
  48.         {
  49.             if (l >= r) return;
  50.             var pivot = (l + r) / 2;
  51.  
  52.             //menší přesunu před pivot, větší za pivot
  53.             var i = l;
  54.             var j = r;
  55.             var v = p[pivot];
  56.  
  57.             while (i <= j)
  58.             {
  59.                 while (p[i] < v) i++; //hledám chybu zleva
  60.                 while (p[j] > v) j--; //hledám chybu zprava
  61.                 if (i > j) break; //test na ukončení opakování
  62.                 //spravím chyby tj. prohodím p[i] s p[j]
  63.                 var x = p[i];
  64.                 p[i] = p[j];
  65.                 p[j] = x;
  66.                 i++;
  67.                 j--;
  68.             }
  69.  
  70.             if (r - l > len - 1000000)
  71.             {
  72.                 Parallel.Invoke(
  73.                 () => QuickSortP_lr(p, l, j),
  74.                 () => QuickSortP_lr(p, i, r)
  75.                 );
  76.             }
  77.             else
  78.             {
  79.                 QuickSortP_lr(p, l, j);
  80.                 QuickSortP_lr(p, i, r);
  81.             }
  82.         }
  83.  
  84.         static void QuickSortP(int[] p)
  85.         {
  86.             QuickSortP_lr(p, 0, p.Length - 1);
  87.         }
  88.  
  89.         static void Main(string[] args)
  90.         {
  91.  
  92.             Random r = new Random();
  93.  
  94.             int[] arr = new int[len];
  95.             int[] arrParallel = new int[len];
  96.  
  97.             for (int i = 0; i < len; i++)
  98.             {
  99.                 arr[i] = arrParallel[i] = r.Next() % len + 1;
  100.             }
  101.  
  102.             Stopwatch stopwatch = Stopwatch.StartNew();
  103.             QuickSort(arr);
  104.             stopwatch.Stop();
  105.             Console.WriteLine("Normal:\t\t" + stopwatch.ElapsedMilliseconds);
  106.  
  107.             Stopwatch stopwatchParallel = Stopwatch.StartNew();
  108.             QuickSortP(arrParallel);
  109.             stopwatchParallel.Stop();
  110.             Console.WriteLine("Parallel:\t" + stopwatchParallel.ElapsedMilliseconds);
  111.  
  112.             Console.Read();
  113.  
  114.         }
  115.     }
  116. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement