コード例 #1
0
        public void Execute()
        {
            // Create sorting algorithm
            SortingType sortingType = GetSortingType();

            if (sortingType == SortingType.Exit)
            {
                return;
            }
            ISortingAlgorithm algorithm = GetSortingAlgorithm(sortingType);

            // Get parameters
            Console.WriteLine("How many runs?");
            int runs = ProgramUtils.ReadLineInt();

            Console.WriteLine("How many entries per run?");
            int amount = ProgramUtils.ReadLineInt();

            // Start runs
            Stopwatch        stopWatch = new Stopwatch();
            IntegerGenerator generator = new IntegerGenerator();

            Console.WriteLine("Sorting...");

            long[] results = new long[runs];
            for (int i = 0; i < runs; i++)
            {
                int[] items = generator.GenerateData(amount, -1_000_000, 1_000_000);
                stopWatch.Start();
                algorithm.Sort(items);
                stopWatch.Stop();
                results[i] = stopWatch.ElapsedMilliseconds;
                stopWatch.Reset();
                Console.Write($"\rProgress: {i+1}/{runs}");
            }

            ProgramUtils.WriteLineImpressive($"The average sorting time of {runs} runs is: {results.Average()} ms");
        }
コード例 #2
0
        public void Execute()
        {
            Console.WriteLine("How many entries do you want to generate?");
            int entries = ProgramUtils.ReadLineInt();

            Console.WriteLine("What's the minimum integer?");
            int min = ProgramUtils.ReadLineInt();

            Console.WriteLine("What's the maximum integer?");
            int max = ProgramUtils.ReadLineInt();

            Console.WriteLine("What's the output file name?");
            string filename = Console.ReadLine();

            IntegerGenerator generator = new IntegerGenerator();

            int[]    data  = generator.GenerateData(entries, min, max);
            string[] lines = Array.ConvertAll(data, i => i.ToString());
            File.WriteAllLines(filename, lines);


            ProgramUtils.WriteLineImpressive($"Succesfully wrote {entries} random integers in range [{min} - {max}] to {filename}!");
        }