Пример #1
0
        public void measure()
        {
            Console.WriteLine("\n\n\t\tBUBBLE SORT PERFORMANCE TESTING!\n\n");

            Random r = new Random();
            SearchingAndSorting sas = new SearchingAndSorting();

            double totalComps    = 0;
            int    compsThisTime = 0;

            double totalSwaps    = 0;
            int    swapsThisTime = 0;

            for (int i = 1; i < 5; i++)
            {
                int arraySize = i * i * 100;
                totalComps = 0;

                // repeat 1000 times: create an unsorted array, and sort it.
                int numTimes = 2;
                for (int j = 0; j < numTimes; j++)
                {
                    int[] nums = new int[arraySize];
                    for (int k = 0; k < arraySize; k++)
                    {                           // give each element a random starting value.
                        nums[k] = r.Next(arraySize);
                    }

                    sas.BubbleSortPerfMeasured(nums, out swapsThisTime, out compsThisTime);
                    totalComps += compsThisTime;
                    totalSwaps += swapsThisTime;
                }
                Console.WriteLine("For an array of " + arraySize + " elements, there were an average of " + totalComps / numTimes + " comparisons performed, and an average of " + totalSwaps / numTimes + "swaps performed");
            }
        }
Пример #2
0
        public void measure()
        {
            SearchingAndSorting sas = new SearchingAndSorting();

            Console.WriteLine("\n\n\t\tBINARY SEARCH PERFORMANCE TESTING!\n\n");

            Random r = new Random();

            for (int i = 1; i < 5; i++)
            {
                int arraySize = i * i * 100;

                int[] nums = new int[arraySize];
                for (int j = 0; j < arraySize; j++)
                {                       // give each element a random starting value.
                    nums[j] = r.Next(arraySize);
                }
                Array.Sort(nums);                 // binary search only works on sorted arrays, so sort it...

                // we want to first get a reading on how many comparisons are
                // needed when we're looking for something in the array.
                // So we'll randomly pick an element out of the array,
                // and search for it.  Lather, rinse, repeat a zillioni times.
                int totalComps = 0;
                int numComps;
                for (int j = 0; j < 10000; j++)
                {
                    int target = nums[r.Next(arraySize)];
                    if (!sas.FindIntegerBinaryPerfMeasured(target, nums, out numComps))
                    {
                        Console.WriteLine("ERROR: COULDN'T find " + target + " despite it's presence");
                    }
                    totalComps += numComps;
                }
                double aveFound = totalComps / 10000.0;                 // Average number of comparisons per search

                // next, we want to get a reading on how many comparisons are
                // needed when we're looking for something NOT in the array.
                // So we'll randomly pick a value we know is NOT in the array,
                // and search for it.  Lather, rinse, repeat a zillion more times.
                totalComps = 0;
                for (int j = 0; j < 10000; j++)
                {
                    if (sas.FindIntegerBinaryPerfMeasured(arraySize + 10, nums, out numComps))
                    {
                        Console.WriteLine("ERROR: COULD find " + (arraySize + 10) + " despite it's presence");
                    }
                    totalComps += numComps;
                }
                double aveNotFound = totalComps / 10000.0;                      // Average number of comparisons per search
                Console.WriteLine("For an array of " + arraySize + " elements, the average number of comparisons when:\n\tthe element was present: " + aveFound + "\n\tthe element was NOT present: " + aveNotFound);
            }
        }
Пример #3
0
        public void RunExercise()
        {
            SearchingAndSorting ase = new SearchingAndSorting();

            int[] i = { 10, 1, 12, 3, 9, 32, 36 };

            // int test = 32;
            int comparison = 0;
            int swaps      = 0;

            //if (ase.FindIntegerLinearPerfMeasured(test, i,out comparison) == false)
            //    Console.WriteLine("Not, after {0} comparisons",comparison);
            //if (ase.FindIntegerLinearPerfMeasured(test, i,out comparison) == true)
            //    Console.WriteLine("Is in, after {0} comparisons",comparison);

            // ase.FindIntegerBinaryPerfMeasured(test, i, out comparison);


            // ase.BubbleSort(i);
            ase.BubbleSortPerfMeasured(i, out swaps, out comparison);
        }