public void BitonicSort() { Random rnd = new Random(); int N = 8; { int[] a = Enumerable.Range(0, N).ToArray().OrderBy(x => rnd.Next()).ToArray(); BitonicSorter.SeqBitonicSort1(a); for (int i = 0; i < N; ++i) { if (a[i] != i) { throw new Exception(); } } } { int[] a = Enumerable.Range(0, N).ToArray().OrderBy(x => rnd.Next()).ToArray(); BitonicSorter.SeqBitonicSort2(a); for (int i = 0; i < N; ++i) { if (a[i] != i) { throw new Exception(); } } } { int[] a = Enumerable.Range(0, N).ToArray().OrderBy(x => rnd.Next()).ToArray(); BitonicSorter.BitonicSort1(a); for (int i = 0; i < N; ++i) { if (a[i] != i) { throw new Exception(); } } } { int[] a = Enumerable.Range(0, N).ToArray().OrderBy(x => rnd.Next()).ToArray(); BitonicSorter.BitonicSort2(a); for (int i = 0; i < N; ++i) { if (a[i] != i) { throw new Exception(); } } } }
static void Main(string[] args) { //Random rng = new Random(); int[] v = new int[nrElements]; for (int i = 0; i < v.Length; ++i) { //v[i] = rng.Next(1 << 10); v[i] = nrElements - i; } BitonicSorter bitonicSorter = new BitonicSorter(); bitonicSorter.sort(v); Console.WriteLine("[{0}]", string.Join(", ", v)); }
/// <summary> /// Tests the speed. /// </summary> /// <param name="testName">Test name.</param> /// <param name="sorter">Sorter.</param> /// <param name="originalArray">Original array.</param> /// <param name="iterations">Iterations.</param> static void TestSpeed(string testName, BitonicSorter sorter, int[] originalArray, int iterations) { long totalTicks = 0; for (int i = 0; i < iterations; i++) { int[] data = (int[])originalArray.Clone(); Stopwatch timer = Stopwatch.StartNew(); sorter.Sort(ref data); timer.Stop(); totalTicks += timer.ElapsedTicks; } TimeSpan span = new TimeSpan(totalTicks); Console.WriteLine("Result: {0} - {1:0.00} ms", testName, span.TotalMilliseconds / iterations); }