public void TestAccuracy()
        {
            int arraySize = 100;

            int[] testArray = RandomArray.GetRandomArray(arraySize, 0, 20000);

            int[] sortedArray = EvenOddSort.Sort(testArray);
            Assert.IsTrue(ArrayUtils.IsSorted(sortedArray));
        }
        public void OriginalArrayUnmodified()
        {
            int arraySize = 100;

            int[] testArray = RandomArray.GetRandomArray(arraySize, 0, 20000);
            int[] copyArray = new int[arraySize];
            testArray.CopyTo(copyArray, 0);

            int[] sortedArray = EvenOddSort.Sort(testArray);
            for (int i = 0; i < arraySize; i++)
            {
                Assert.IsTrue(testArray[i] == copyArray[i]);
            }
        }
Esempio n. 3
0
        static void Main(string[] args)
        {
            int[] arr = new int[10000];
            Random rnd = new Random();
            for (int i = 0; i < arr.Length; i++)
            {
                arr[i] = rnd.Next(10000);
            }
            ISort<int> s = new BubbleSort<int>();
            DateTime dt = DateTime.Now;
            arr = s.Sorting(arr);
            Console.WriteLine("Time for BubbleSort is {0}.", DateTime.Now - dt);

            for (int i = 0; i < arr.Length; i++)
            {
                arr[i] = rnd.Next(10000);
            }
            dt = DateTime.Now;
            s = new CocktailSort<int>();
            arr = s.Sorting(arr);
            Console.WriteLine("Time for CocktailSort is {0}.", DateTime.Now - dt);

            for (int i = 0; i < arr.Length; i++)
            {
                arr[i] = rnd.Next(10000);
            }
            dt = DateTime.Now;
            s = new EvenOddSort<int>();
            arr = s.Sorting(arr);
            Console.WriteLine("Time for EvenOddSort is {0}.", DateTime.Now - dt);

            for (int i = 0; i < arr.Length; i++)
            {
                arr[i] = rnd.Next(10000);
            }
            dt = DateTime.Now;
            s = new CombSort<int>();
            arr = s.Sorting(arr);
            Console.WriteLine("Time for CombSort is {0}.", DateTime.Now - dt);

            for (int i = 0; i < arr.Length; i++)
            {
                arr[i] = rnd.Next(10000);
            }
            dt = DateTime.Now;
            s = new GnomeSort<int>();
            arr = s.Sorting(arr);
            Console.WriteLine("Time for GnomeSort is {0}.", DateTime.Now - dt);

            arr = new int[10000];
            for (int i = 0; i < arr.Length; i++)
            {
                arr[i] = rnd.Next(10000);
            }
            dt = DateTime.Now;
            s = new InsertionSort<int>();
            arr = s.Sorting(arr);
            Console.WriteLine("Time for InsertionSort is {0}.", DateTime.Now - dt);

            for (int i = 0; i < arr.Length; i++)
            {
                arr[i] = rnd.Next(10000);
            }
            dt = DateTime.Now;
            s = new BinaryInsertionSort<int>();
            arr = s.Sorting(arr);
            Console.WriteLine("Time for BinaryInsertionSort is {0}.", DateTime.Now - dt);

            for (int i = 0; i < arr.Length; i++)
            {
                arr[i] = rnd.Next(10000);
            }
            dt = DateTime.Now;
            s = new ShellSort<int>();
            arr = s.Sorting(arr);
            Console.WriteLine("Time for ShellSort is {0}.", DateTime.Now - dt);

            arr = new int[1000000];
            for (int i = 0; i < arr.Length; i++)
            {
                arr[i] = rnd.Next(1000000);
            }
            for (int i = 0; i < arr.Length; i++)
            {
                arr[i] = rnd.Next(10000);
            }
            dt = DateTime.Now;
            s = new HeapSort<int>();
            arr = s.Sorting(arr);
            Console.WriteLine("Time for HeapSort is {0}.", DateTime.Now - dt);
            int ddd = 0;
            for (int i = 0; i < arr.Length - 2; i++)
            {
                //Console.Write(arr[i] + " ");
                if (arr[i] > arr[i + 1]) //Console.WriteLine("Fatal ERROR!!!");
                    ddd++;
            }
            Console.WriteLine("Error count: {0}", ddd);

            dt = DateTime.Now;
            s = new MergeSort<int>();
            arr = s.Sorting(arr);
            Console.WriteLine("Time for MergeSort is {0}.", DateTime.Now - dt);

            //StreamWriter sw = new StreamWriter("C:/Users/suvorovi/1.txt");
            for (int i = 0; i < arr.Length; i++)
            {
                arr[i] = rnd.Next(1000000);
                //sw.Write(arr[i] + " ");
            }
            //sw.WriteLine("");
            dt = DateTime.Now;
            s = new QuickSort<int>();
            arr = s.Sorting(arr);
            Console.WriteLine("Time for QuickSort is {0}.", DateTime.Now - dt);
            for (int i = 0; i < arr.Length; i++)
            {
                arr[i] = rnd.Next(1000000);
                //sw.Write(arr[i] + " ");
            }
            //sw.WriteLine("");
            dt = DateTime.Now;
            s = new TimSort<int>();
            arr = s.Sorting(arr);
            Console.WriteLine("Time for TimSort is {0}.", DateTime.Now - dt);
            ddd = 0;
            for (int i = 0; i < arr.Length - 2; i++)
            {
                //Console.Write(arr[i] + " ");
                if (arr[i] > arr[i + 1]) //Console.WriteLine("Fatal ERROR!!!");
                    ddd++;
            }
            Console.WriteLine("Error count: {0}", ddd);
            Console.ReadLine();
            //sw.Close();
        }
        private async void Button_Click(object sender, RoutedEventArgs e)
        {
            Button    clickedButton = (Button)sender;
            Stopwatch sw            = new Stopwatch();

            switch (clickedButton.Name.ToLower())
            {
            case "quick":
                break;

            case "insertion":
                iStatus.Text         = RUNNING;
                iSortRuntime.Content = string.Empty;
                sw.Start();
                await InsertionSort.SortAsync(RandomArray.GetRandomArray(DatasetSize, 0, 100), _cts);

                sw.Stop();
                iStatus.Text         = COMPLETE;
                iSortRuntime.Content = sw.Elapsed;
                break;

            case "evenodd":
                eoStatus.Text         = RUNNING;
                eoSortRuntime.Content = string.Empty;
                sw.Start();
                await EvenOddSort.SortAsync(RandomArray.GetRandomArray(DatasetSize, 0, 100), _cts);

                sw.Stop();
                eoStatus.Text         = COMPLETE;
                eoSortRuntime.Content = sw.Elapsed;
                break;

            case "arrayadd":
                arrayAddStatus.Text     = RUNNING;
                arrayAddRuntime.Content = string.Empty;
                sw.Start();
                await ArrayAddition.AddArraysAsync(RandomArray.GetRandomArray(DatasetSize, 0, 100),
                                                   RandomArray.GetRandomArray(DatasetSize, 0, 100), _cts);

                sw.Stop();
                arrayAddStatus.Text     = COMPLETE;
                arrayAddRuntime.Content = sw.Elapsed;
                break;

            case "arraysum":
                arraySumStatus.Text     = RUNNING;
                arraySumRuntime.Content = string.Empty;
                sw.Start();
                await ArraySum.SumAsync(RandomArray.GetRandomArray(DatasetSize, 0, 100), _cts);

                sw.Stop();
                arraySumStatus.Text     = COMPLETE;
                arraySumRuntime.Content = sw.Elapsed;
                break;

            case "parrayadd":
                pArrayAddStatus.Text     = RUNNING;
                pArrayAddRuntime.Content = string.Empty;
                sw.Start();
                await ParallelArrayAddition.AddArraysAsync(RandomArray.GetRandomArray(DatasetSize, 0, 100),
                                                           RandomArray.GetRandomArray(DatasetSize, 0, 100), _cts);

                sw.Stop();
                pArrayAddStatus.Text     = COMPLETE;
                pArrayAddRuntime.Content = sw.Elapsed;
                break;

            case "psum":
                pSumStatus.Text     = RUNNING;
                pSumRuntime.Content = string.Empty;
                sw.Start();
                await ParallelSum.SumAsync(RandomArray.GetRandomArray(DatasetSize, 0, 100), _cts);

                sw.Stop();
                pSumStatus.Text     = COMPLETE;
                pSumRuntime.Content = sw.Elapsed;
                break;

            case "reduct":
                reductStatus.Text     = RUNNING;
                reductRuntime.Content = string.Empty;
                sw.Start();
                await ReductionSum.SumAsync(RandomArray.GetRandomArray(DatasetSize, 0, 100));

                sw.Stop();
                reductStatus.Text     = COMPLETE;
                reductRuntime.Content = sw.Elapsed;
                break;

            case "all":
                iSortRuntime.Content     = string.Empty;
                eoSortRuntime.Content    = string.Empty;
                arrayAddRuntime.Content  = string.Empty;
                arraySumRuntime.Content  = string.Empty;
                pArrayAddRuntime.Content = string.Empty;
                pSumRuntime.Content      = string.Empty;
                allRuntime.Content       = string.Empty;
                iStatus.Text             = RUNNING;
                eoStatus.Text            = RUNNING;
                arrayAddStatus.Text      = RUNNING;
                arraySumStatus.Text      = RUNNING;
                pArrayAddStatus.Text     = RUNNING;
                pSumStatus.Text          = RUNNING;
                allStatus.Text           = RUNNING;
                sw.Start();
                Task inSort = InsertionSort.SortAsync(RandomArray.GetRandomArray(DatasetSize, 0, 100), _cts);
                Task eoSort = EvenOddSort.SortAsync(RandomArray.GetRandomArray(DatasetSize, 0, 100), _cts);
                Task arrAdd = ArrayAddition.AddArraysAsync(RandomArray.GetRandomArray(DatasetSize, 0, 100),
                                                           RandomArray.GetRandomArray(DatasetSize, 0, 100), _cts);
                Task arrSum = ArraySum.SumAsync(RandomArray.GetRandomArray(DatasetSize, 0, 100), _cts);
                Task parAdd = ParallelArrayAddition.AddArraysAsync(RandomArray.GetRandomArray(DatasetSize, 0, 100),
                                                                   RandomArray.GetRandomArray(DatasetSize, 0, 100), _cts);
                Task parSum = ParallelSum.SumAsync(RandomArray.GetRandomArray(DatasetSize, 0, 100), _cts);
                await inSort.ContinueWith(t =>
                {
                    this.Dispatcher.Invoke(() =>
                    {
                        iStatus.Text = COMPLETE;
                    });
                });

                await eoSort.ContinueWith(t =>
                {
                    this.Dispatcher.Invoke(() =>
                    {
                        eoStatus.Text = COMPLETE;
                    });
                });

                await arrAdd.ContinueWith(t =>
                {
                    this.Dispatcher.Invoke(() =>
                    {
                        arrayAddStatus.Text = COMPLETE;
                    });
                });

                await arrSum.ContinueWith(t =>
                {
                    this.Dispatcher.Invoke(() =>
                    {
                        arraySumStatus.Text = COMPLETE;
                    });
                });

                await parAdd.ContinueWith(t =>
                {
                    this.Dispatcher.Invoke(() =>
                    {
                        pArrayAddStatus.Text = COMPLETE;
                    });
                });

                await parSum.ContinueWith(t =>
                {
                    this.Dispatcher.Invoke(() =>
                    {
                        pSumStatus.Text = COMPLETE;
                    });
                });

                await Task.WhenAll(inSort, eoSort);

                sw.Stop();
                allRuntime.Content = sw.Elapsed;
                allStatus.Text     = COMPLETE;
                break;

            case "cancel":
                _cts?.Cancel();
                _cts = new CancellationTokenSource();
                break;

            default:
                break;
            }
        }