Advertisement
Guest User

Challenge: Parallel Sorting - Easy C# Solution

a guest
Jun 13th, 2012
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.25 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. namespace ParallelSort
  6. {
  7.     class Program
  8.     {
  9.         static void Main(string[] args)
  10.         {
  11.             // Stopwatch for timing operations.
  12.             System.Diagnostics.Stopwatch stopwatch = new System.Diagnostics.Stopwatch();
  13.  
  14.             long count = long.Parse(args[0]);
  15.  
  16.             // Generate the numbers! This part is easy enough. It generates them
  17.             // in an array rather than yield return to prevent lazy evaluation.
  18.             System.Console.Write("Generating {0} integers...", count);
  19.             stopwatch.Restart();
  20.             IEnumerable<int> integers = GenerateRandoms(count);
  21.             stopwatch.Stop();
  22.             Console.WriteLine(" {0}ms.", stopwatch.ElapsedMilliseconds);
  23.  
  24.             // This timer is actually useless, since the sort is not evaluated until the next step...
  25.             System.Console.Write("Sorting integers...");
  26.             stopwatch.Restart();
  27.             IEnumerable<int> sortedIntegers = SortInParallel(integers);
  28.             stopwatch.Stop();
  29.             Console.WriteLine(" {0}ms.", stopwatch.ElapsedMilliseconds);
  30.  
  31.             // Confirm the sorting. This part is a single-core algorithm, because
  32.             // there's no real way to do it in LINQ without blowing away memory.
  33.             System.Console.Write("Confirming sort...");
  34.             stopwatch.Restart();
  35.             bool confirmed = ConfirmSort(sortedIntegers);
  36.             stopwatch.Stop();
  37.             if (confirmed)
  38.                 System.Console.WriteLine(" {0}ms.", stopwatch.ElapsedMilliseconds);
  39.             else
  40.                 System.Console.WriteLine(" FAIL - {0}ms.", stopwatch.ElapsedMilliseconds);
  41.         }
  42.  
  43.         /// <summary>Generates an array of random integers.</summary>
  44.         /// <param name="count">Number of integers to generate.</param>
  45.         /// <returns>Array of random integers.</returns>
  46.         static int[] GenerateRandoms(long count)
  47.         {
  48.             Random random = new Random();
  49.  
  50.             int[] array = new int[count];
  51.             for (int index = 0; index < count; ++index)
  52.                 array[index] = random.Next();
  53.  
  54.             return array;
  55.         }
  56.  
  57.         /// <summary>Sorts the given integers in parallel.</summary>
  58.         /// <param name="integers">Enumeration of integers to sort.</param>
  59.         /// <returns>Sorted enumeration of integers.</returns>
  60.         static IEnumerable<int> SortInParallel(IEnumerable<int> integers)
  61.         {
  62.             return integers.AsParallel().OrderBy((i) => i);
  63.         }
  64.  
  65.         /// <summary>Confirms that the given integers are in sorted order.</summary>
  66.         /// <param name="integers">Enumeration of integers to check.</param>
  67.         /// <returns>
  68.         ///     If <paramref name="integers"/> is in ascending sorted order, <c>true</c>.
  69.         ///     Otherwise, <c>false</c>.
  70.         /// </returns>
  71.         static bool ConfirmSort(IEnumerable<int> integers)
  72.         {
  73.             int last = integers.First();
  74.             foreach (int current in integers.Skip(1))
  75.             {
  76.                 if (last > current)
  77.                     return false;
  78.                 last = current;
  79.             }
  80.             return true;
  81.         }
  82.     }
  83. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement