Skip to content

Instantly share code, notes, and snippets.

@catlion
Created June 14, 2012 18:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save catlion/2932050 to your computer and use it in GitHub Desktop.
Save catlion/2932050 to your computer and use it in GitHub Desktop.
psort
using System;
using System.Diagnostics;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace PsortTest
{
class Program
{
static void Main(string[] args)
{
Stopwatch timer;
var unord = RandomSeq((int)Math.Pow(10, 7));
timer = Stopwatch.StartNew();
var ord = unord.AsParallel().OrderBy(x => x).ToArray();
Console.WriteLine("Dumb order: " + timer.Elapsed);
Console.WriteLine(
string.Format(
"Array of length {1} is {0}ordered",
IsOrdered(ord) ? String.Empty : "not ",
ord.Length));
//Console.ReadLine();
}
private static bool IsOrdered(int[] arr)
{
for (int i = 1; i < arr.Length; i++) {
if (arr[i - 1] > arr[i]) return false;
}
return true;
}
private static int[] RandomSeq(int length)
{
var rnd = new Random();
var arr = new int[length];
while (--length >= 0) {
arr[length] = rnd.Next();
}
return arr;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment