Created
June 14, 2012 18:36
-
-
Save catlion/2932050 to your computer and use it in GitHub Desktop.
psort
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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