Viacheslav

Untitled

Jul 21st, 2019
431
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.65 KB | None | 0 0
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Diagnostics;
  5. using System.Linq;
  6. using System.Threading.Tasks;
  7.  
  8. namespace ConsoleApp1
  9. {
  10.     class Program
  11.     {
  12.         static void Main(string[] args)
  13.         {
  14.             var n = 3000000;
  15.             var timer = new Stopwatch();
  16.             int[] arrСlassic = new int[n];
  17.             int[] arrCopy = new int[n];
  18.             int[] arrTasks = new int[n];
  19.             timer.Start();
  20.             for (int i = 0; i < arrСlassic.Length - 1; i++)
  21.             {
  22.                 arrСlassic[i] = i;
  23.             }
  24.             timer.Stop();
  25.             Console.WriteLine($"Preparing array with {n} integers elapsed: {timer.ElapsedMilliseconds} miliseconds...");
  26.             timer.Reset();
  27.             var end = new Random();
  28.             timer.Start();
  29.             arrСlassic = arrСlassic.OrderBy(x => end.Next()).ToArray();
  30.             timer.Stop();
  31.             Console.WriteLine($"Sguffling the array elapsed: {timer.ElapsedMilliseconds} miliseconds");
  32.             timer.Reset();
  33.             Console.WriteLine("Copying shuffled array to others....");
  34.             timer.Start();
  35.             for (int i = 0; i < arrCopy.Length - 1; i++)
  36.             {
  37.                 arrCopy[i] = arrСlassic[i];
  38.                 arrTasks[i] = arrСlassic[i];
  39.             }
  40.             timer.Stop();
  41.             Console.WriteLine($"Done for {timer.ElapsedMilliseconds} milisecods");
  42.             timer.Reset();
  43.             timer.Start();
  44.             QuickSort(arrСlassic, 0, arrСlassic.Length - 1);
  45.             timer.Stop();
  46.             //Console.WriteLine(string.Join(", ", arr));
  47.             Console.WriteLine("----------------------------------------------------");
  48.             Console.WriteLine("QuickSortClassic sorted using synchronius operations");
  49.             Console.WriteLine($"Time elapsed: {timer.ElapsedMilliseconds} miliseconds");
  50.             timer.Reset();
  51.             timer.Start();
  52.             QuickSortTask(arrTasks, 0, arrTasks.Length - 1);
  53.             timer.Stop();
  54.             Console.WriteLine("----------------------------------------------------");
  55.             Console.WriteLine("QuickSortTask sorted using Task Factory");
  56.             Console.WriteLine($"Time elapsed: {timer.ElapsedMilliseconds} miliseconds");
  57.             timer.Reset();
  58.             timer.Start();
  59.             QuickSortParallel(arrCopy, 0, arrCopy.Length - 1);
  60.             timer.Stop();
  61.             Console.WriteLine("----------------------------------------------------");
  62.             Console.WriteLine("QuickSortParallel sorted using Parallel.Invoke");
  63.             Console.WriteLine($"Time elapsed: {timer.ElapsedMilliseconds} miliseconds");
  64.             Console.WriteLine("----------------------------------------------------");
  65.             Console.WriteLine("Becnhmark Done");
  66.         }
  67.         public static void QuickSort(int[] arr, int start, int end)
  68.         {
  69.             if (start >= end)
  70.             {
  71.                 return;
  72.             }
  73.             int pivotIndex = Partition(arr, start, end);
  74.             QuickSort(arr, start, pivotIndex - 1);
  75.             QuickSort(arr, pivotIndex + 1, end);
  76.         }
  77.         public static void QuickSortParallel(int[] arr, int start, int end)
  78.         {
  79.             if (start >= end)
  80.             {
  81.                 return;
  82.             }
  83.             int pivotIndex = Partition(arr, start, end);
  84.             Parallel.Invoke
  85.                 (
  86.                     () => QuickSortParallel(arr, start, pivotIndex - 1),
  87.                     () => QuickSortParallel(arr, pivotIndex + 1, end)
  88.                 );
  89.         }
  90.         public static void QuickSortTask(int[] arr, int start, int end)
  91.         {
  92.             if (start >= end)
  93.             {
  94.                 return;
  95.             }
  96.             int pivotIndex = Partition(arr, start, end);
  97.             Task.Factory.StartNew(() => QuickSortTask(arr, start, pivotIndex - 1));
  98.             Task.Factory.StartNew(() => QuickSortTask(arr, pivotIndex + 1, end));
  99.  
  100.         }
  101.         private static int Partition(int[] arr, int start, int end)
  102.         {
  103.             int pivotIndex = start;
  104.             int pivot = arr[end];
  105.  
  106.             for (int i = start; i < end; i++)
  107.             {
  108.                 if (arr[i] <= pivot)
  109.                 {
  110.                     int temp = arr[i];
  111.                     arr[i] = arr[pivotIndex];
  112.                     arr[pivotIndex] = temp;
  113.                     pivotIndex++;
  114.                 }
  115.             }
  116.             int temp1 = arr[pivotIndex];
  117.             arr[pivotIndex] = arr[end];
  118.             arr[end] = temp1;
  119.  
  120.             return pivotIndex;
  121.         }
  122.     }
  123. }
Advertisement
Add Comment
Please, Sign In to add comment