Exemple #1
0
        static void Main(string[] args)
        {
            bool isInNoneStandardMode = false;

            for (int i = 0; i < args.Length; i++)
            {
                switch (args[i])
                {
                case "-benchmark":
                    isInNoneStandardMode = true;
                    Benchmark();
                    break;

                case "-testSearching":
                    isInNoneStandardMode = true;
                    TestSearching();
                    break;

                case "-testSlowSort":
                    isInNoneStandardMode = true;
                    TestSlowSort();
                    break;

                case "-testSorting":
                    isInNoneStandardMode = true;
                    TestSorting();
                    break;

                case "-testSorting2":
                    isInNoneStandardMode = true;
                    TestSorting2();
                    break;
                }
            }

            if (isInNoneStandardMode == false)
            {
                while (true)
                {
                    string[] files = GetFiles();
                    for (int i = 0; i < files.Length; i++)
                    {
                        Console.Clear();
                        Console.WriteLine("Reading File: {0}", Path.GetFileNameWithoutExtension(files[i]));
                        float[] values = Utils.ParseFile(files[i]);
                        if (Utils.InputLoop(@"Would you like to merge multiple files for this search:
    0 - Yes
    1 - No"))
                        {
                            while (true)
                            {
                                List <int> indexs  = new List <int>();
                                string     message = string.Format("Which files would you like to merge with {0}:", Path.GetFileNameWithoutExtension(files[i]));
                                for (int x = i + 1; x < files.Length; x++)
                                {
                                    indexs.Add(x);
                                    message += string.Format("\n\t{0} - {1}", x, Path.GetFileNameWithoutExtension(files[x]));
                                }
                                int choice = Utils.InputLoop(message, indexs.ToArray());
                                values = Utils.ParseMergeFiles(values, files[choice]);

                                if (!Utils.InputLoop(@"Would you like to merge another file:
    0 - Yes
    1 - No"))
                                {
                                    break;
                                }
                            }
                        }

                        Utils.ConsoleHang();
                        Console.Clear();

                        arrayPrintingChoice = Utils.InputLoop(ARRAY_PRINTING_SELECTION, ARRAY_PRINTING_I);

                        #region Sort
                        float[] sortingValues            = new float[values.Length];
                        int     sortingComparisonsChoice = Utils.InputLoop(SORTING_COMPARE_SELECTION, Utils.YES_NO_I);
                        switch (sortingComparisonsChoice)
                        {
                        case Utils.YES:
                            List <SortingStatistics> sortingStatistics = new List <SortingStatistics>();
                            while (true)
                            {
                                Utils.CopyArray(values, sortingValues, values.Length);
                                Sorting.SortArray(sortingValues);
                                sortingStatistics.Add(Sorting.Instance.GetStatistics);

                                ArrayPrinting(sortingValues, arrayPrintingChoice, Path.GetFileNameWithoutExtension(files[i]));
                                Console.WriteLine();
                                Utils.WriteLastStatisticsSort();

                                Utils.ConsoleHang();
                                Console.Clear();

                                if (!Utils.InputLoop(SORTING_AGAIN_SELECTION))
                                {
                                    break;
                                }
                            }

                            // Compare Sorts
                            foreach (SortingStatistics stats in sortingStatistics)
                            {
                                Utils.WriteLastStatisticsSort();
                                Console.WriteLine();
                            }

                            Utils.ConsoleHang();
                            Console.Clear();
                            break;

                        case Utils.NO:
                            Utils.CopyArray(values, sortingValues, values.Length);
                            Sorting.SortArray(sortingValues);

                            ArrayPrinting(sortingValues, arrayPrintingChoice, Path.GetFileNameWithoutExtension(files[i]));
                            Console.WriteLine();
                            Utils.WriteLastStatisticsSort();

                            Utils.ConsoleHang();
                            Console.Clear();
                            break;

                        default:
                            break;
                        }

                        Utils.ConsoleHang();
                        Console.Clear();
                        #endregion

                        #region SearchArray
                        int searchComparisonsChoice = Utils.InputLoop(SEARCHING_COMPARE_SELECTION, Utils.YES_NO_I);
                        switch (searchComparisonsChoice)
                        {
                        case Utils.YES:
                            List <SearchingStatistics> searchingStatistics = new List <SearchingStatistics>();
                            while (true)
                            {
                                Searching.SearchArray(sortingValues);
                                Utils.WriteLastStatisticsSearch();

                                searchingStatistics.Add(Searching.Instance.GetStatistics);

                                Utils.ConsoleHang();
                                Console.Clear();

                                if (Utils.InputLoop(KEEP_SEARCHING_SELECTION))
                                {
                                    break;
                                }
                            }

                            Console.Clear();

                            foreach (SearchingStatistics stats in searchingStatistics)
                            {
                                Utils.WriteStatisticsSearch(stats);
                                Console.WriteLine();
                            }

                            Console.Clear();
                            break;

                        case Utils.NO:
                            while (true)
                            {
                                Searching.SearchArray(sortingValues);
                                Utils.WriteLastStatisticsSearch();

                                Utils.ConsoleHang();
                                Console.Clear();

                                if (Utils.InputLoop(KEEP_SEARCHING_SELECTION))
                                {
                                    break;
                                }
                            }
                            break;

                        default:
                            break;
                        }

                        Utils.ConsoleHang();
                        Console.Clear();
                        #endregion

                        if (Utils.InputLoop(LOAD_DIFFERENT_FILES_SELECTION))
                        {
                            break;
                        }
                    }
                }
            }

            // Make the console hang till user input.
            Console.WriteLine("Press any key to exit...");
            Console.ReadKey();
        }
Exemple #2
0
        public static void Benchmark()
        {
            string[]                   files               = Utils.ParseDirectory(Utils.InputLoopDirPath(DIRECTORY_MESSAGE));
            List <string>              fileNamesSort       = new List <string>();
            List <string>              fileNamesSearch     = new List <string>();
            List <int[]>               searchIndexs        = new List <int[]>();
            List <SortingStatistics>   sortingStatistics   = new List <SortingStatistics>();
            List <SearchingStatistics> searchingStatistics = new List <SearchingStatistics>();

            for (int i = 0; i < files.Length; i++)
            {
                Console.WriteLine(Path.GetFileName(files[i]));

                float[] values       = Utils.ParseFile(files[i]);
                float[] workerValues = new float[values.Length];

                #region Sort
                #region MergeSort
                Console.WriteLine("MergeSort");
                fileNamesSort.Add(Path.GetFileName(files[i]));
                Utils.CopyArray(values, workerValues, values.Length);
                Sorting.Instance.ResetCounters();
                Sorting.MergeSort(workerValues, Sorting.Ascending);
                sortingStatistics.Add(Sorting.Instance.GetStatistics);

                fileNamesSort.Add(Path.GetFileName(files[i]));
                Utils.CopyArray(values, workerValues, values.Length);
                Sorting.Instance.ResetCounters();
                Sorting.MergeSort(workerValues, Sorting.Descending);
                sortingStatistics.Add(Sorting.Instance.GetStatistics);
                #endregion

                #region QuickSort
                Console.WriteLine("QuickSort");
                fileNamesSort.Add(Path.GetFileName(files[i]));
                Utils.CopyArray(values, workerValues, values.Length);
                Sorting.Instance.ResetCounters();
                Sorting.QuickSort(workerValues, Sorting.Ascending);
                sortingStatistics.Add(Sorting.Instance.GetStatistics);

                fileNamesSort.Add(Path.GetFileName(files[i]));
                Utils.CopyArray(values, workerValues, values.Length);
                Sorting.Instance.ResetCounters();
                Sorting.QuickSort(workerValues, Sorting.Descending);
                sortingStatistics.Add(Sorting.Instance.GetStatistics);
                #endregion

                #region BubbleSort
                Console.WriteLine("BubbleSort");
                fileNamesSort.Add(Path.GetFileName(files[i]));
                Utils.CopyArray(values, workerValues, values.Length);
                Sorting.Instance.ResetCounters();
                Sorting.BubbleSort(workerValues, Sorting.Ascending);
                sortingStatistics.Add(Sorting.Instance.GetStatistics);

                fileNamesSort.Add(Path.GetFileName(files[i]));
                Utils.CopyArray(values, workerValues, values.Length);
                Sorting.Instance.ResetCounters();
                Sorting.BubbleSort(workerValues, Sorting.Descending);
                sortingStatistics.Add(Sorting.Instance.GetStatistics);
                #endregion

                #region HeapSort
                Console.WriteLine("HeapSort");
                fileNamesSort.Add(Path.GetFileName(files[i]));
                Utils.CopyArray(values, workerValues, values.Length);
                Sorting.Instance.ResetCounters();
                Sorting.HeapSort(workerValues, Sorting.Ascending);
                sortingStatistics.Add(Sorting.Instance.GetStatistics);

                fileNamesSort.Add(Path.GetFileName(files[i]));
                Utils.CopyArray(values, workerValues, values.Length);
                Sorting.Instance.ResetCounters();
                Sorting.HeapSort(workerValues, Sorting.Descending);
                sortingStatistics.Add(Sorting.Instance.GetStatistics);
                #endregion

                #region SlowSort
                if (workerValues.Length <= 256)
                {
                    Console.WriteLine("SlowSort");
                    fileNamesSort.Add(Path.GetFileName(files[i]));
                    Utils.CopyArray(values, workerValues, values.Length);
                    Sorting.Instance.ResetCounters();
                    Sorting.SlowSort(workerValues, Sorting.Ascending);
                    sortingStatistics.Add(Sorting.Instance.GetStatistics);

                    fileNamesSort.Add(Path.GetFileName(files[i]));
                    Utils.CopyArray(values, workerValues, values.Length);
                    Sorting.Instance.ResetCounters();
                    Sorting.SlowSort(workerValues, Sorting.Descending);
                    sortingStatistics.Add(Sorting.Instance.GetStatistics);
                }
                #endregion

                #region MathMaticaSort
                if (workerValues.Length <= 64)
                {
                    Console.WriteLine("MathMaticaSort");
                    fileNamesSort.Add(Path.GetFileName(files[i]));
                    Utils.CopyArray(values, workerValues, values.Length);
                    Sorting.Instance.ResetCounters();
                    Sorting.MathMaticaSort(workerValues, Sorting.Ascending);
                    sortingStatistics.Add(Sorting.Instance.GetStatistics);

                    fileNamesSort.Add(Path.GetFileName(files[i]));
                    Utils.CopyArray(values, workerValues, values.Length);
                    Sorting.Instance.ResetCounters();
                    Sorting.MathMaticaSort(workerValues, Sorting.Descending);
                    sortingStatistics.Add(Sorting.Instance.GetStatistics);
                }
                #endregion
                #endregion

                #region Search
                Utils.CopyArray(values, workerValues, values.Length);
                Sorting.Instance.ResetCounters();
                Sorting.MergeSort(workerValues, Sorting.Ascending);

                float firstSearchValue   = workerValues[Utils.Instance.Random.Next(0, workerValues.Length)];
                float boundSearchValue   = Utils.MostCommonValue(workerValues);
                float nearestSearchValue = ((workerValues[Utils.Instance.Random.Next(0, workerValues.Length)] + workerValues[Utils.Instance.Random.Next(0, workerValues.Length)]) / 2) + ((float)Utils.Instance.Random.NextDouble() * workerValues[Utils.Instance.Random.Next(0, workerValues.Length)]);

                #region LinearSearch
                Console.WriteLine("LinearSearch");
                fileNamesSearch.Add(Path.GetFileName(files[i]));
                Searching.Instance.ResetCounters();
                searchIndexs.Add(new int[] { Searching.LinearSearch(workerValues, firstSearchValue) });
                searchingStatistics.Add(Searching.Instance.GetStatistics);

                fileNamesSearch.Add(Path.GetFileName(files[i]));
                Searching.Instance.ResetCounters();
                searchIndexs.Add(Searching.LinearSearchBounds(workerValues, boundSearchValue));
                searchingStatistics.Add(Searching.Instance.GetStatistics);

                fileNamesSearch.Add(Path.GetFileName(files[i]));
                Searching.Instance.ResetCounters();
                searchIndexs.Add(new int[] { Searching.LinearSearchNearest(workerValues, nearestSearchValue) });
                searchingStatistics.Add(Searching.Instance.GetStatistics);
                #endregion

                #region BinarySearch
                Console.WriteLine("BinarySearch");
                fileNamesSearch.Add(Path.GetFileName(files[i]));
                Searching.Instance.ResetCounters();
                searchIndexs.Add(new int[] { Searching.BinarySearch(workerValues, firstSearchValue) });
                searchingStatistics.Add(Searching.Instance.GetStatistics);

                fileNamesSearch.Add(Path.GetFileName(files[i]));
                Searching.Instance.ResetCounters();
                searchIndexs.Add(Searching.BinarySearchBounds(workerValues, boundSearchValue));
                searchingStatistics.Add(Searching.Instance.GetStatistics);

                fileNamesSearch.Add(Path.GetFileName(files[i]));
                Searching.Instance.ResetCounters();
                searchIndexs.Add(new int[] { Searching.BinarySearchNearest(workerValues, nearestSearchValue) });
                searchingStatistics.Add(Searching.Instance.GetStatistics);
                #endregion
                #endregion

                Console.WriteLine();
            }

            int          count = 0;
            StreamWriter file  = new StreamWriter(@"benchmark.txt");
            foreach (SortingStatistics stats in sortingStatistics)
            {
                file.WriteLine(fileNamesSort[count++]);
                file.WriteLine(stats.Sort);
                file.WriteLine(stats.Order);
                file.WriteLine(stats.Comparisons);
                file.WriteLine(stats.Itterations);
                file.WriteLine(stats.Recursions);
                file.WriteLine(stats.Swaps);
                file.WriteLine("");
            }
            count = 0;
            foreach (SearchingStatistics stats in searchingStatistics)
            {
                file.WriteLine(fileNamesSearch[count]);
                file.WriteLine(stats.Search);
                file.WriteLine(stats.Value);
                file.WriteLine(searchIndexs[count].Length < 2 ? searchIndexs[count][0].ToString() : string.Format("{0}, {1}", searchIndexs[count][0], searchIndexs[count][1]));
                file.WriteLine(stats.Comparisons);
                file.WriteLine(stats.Itterations);
                file.WriteLine("");
                count++;
            }
            file.Close();
        }