Example #1
0
        private static void Sort(ISort SortAlgorithm, ArrayTestBed origArrayTestBed, ArrayTestBed clonedArrayTestBed, string algorithmTitle)
        {
            //origArrayTestBed.DispayElements();

            GC.Collect();
            Metrics metric = new Metrics($"{algorithmTitle} Sort");

            var result = SortAlgorithm.Sort(origArrayTestBed);


            metric.Dispose();
            GC.Collect();

            metric = new Metrics($"Reverse {algorithmTitle} Sort");

            var result2 = SortAlgorithm.ReverseSort(clonedArrayTestBed);

            metric.Dispose();


            origArrayTestBed.arr   = result;
            clonedArrayTestBed.arr = result2;


            //origArrayTestBed.DispayElements();

            //clonedArrayTestBed.DispayElements();
            Console.WriteLine();

            Console.WriteLine("=======================================================================================================================================");
            Console.WriteLine();
        }
        public int[] Sort(ArrayTestBed arrayObject)
        {
            int minimumValueIndex, tempValue;
            var numberArray = arrayObject.arr;

            for (int i = 0; i <= arrayObject.upper; ++i)
            {
                minimumValueIndex = i;
                for (int j = i + 1; j <= arrayObject.upper; ++j)
                {
                    if (numberArray[j] > numberArray[minimumValueIndex])
                    {
                        minimumValueIndex = j;
                    }
                }

                tempValue      = numberArray[i];
                numberArray[i] = numberArray[minimumValueIndex];
                numberArray[minimumValueIndex] = tempValue;

                if (ShowLog)
                {
                    arrayObject.DispayElements();
                }
            }
            return(numberArray);
        }
Example #3
0
        public void ComposeSearch()
        {
            BinarySearch binSearch      = new BinarySearch();
            ArrayTestBed numbersTestBed = new ArrayTestBed(10000);
            Random       rnd            = new Random(100);

            for (int i = 0; i <= 9999; i++)
            {
                numbersTestBed.Insert((int)(rnd.NextDouble() * 10));
            }



            var result = insertionSortAlgorithm.Sort(numbersTestBed);



            //numbersTestBed.DispayElements();

            GC.Collect();
            Metrics metric   = new Metrics($"Binary Search ");
            var     position = binSearch.Search(numbersTestBed.arr, 2);

            metric.Dispose();

            GC.Collect();
            metric = new Metrics($"Recursive Binary Search ");
            var position2 = binSearch.RecursiveSearch(numbersTestBed.arr, numbersTestBed.arr.Length - 1, 0, 2);

            metric.Dispose();

            GC.Collect();
            metric = new Metrics($"Inbuilt C# Binary Search ");
            var position3 = Array.BinarySearch(numbersTestBed.arr, 2);

            metric.Dispose();

            if (position > -1 && position2 > -1 && position3 > -1)
            {
                Console.WriteLine("Found Item");
            }
            else
            {
                Console.WriteLine("Not in the array");
            }
        }
Example #4
0
        public void ComposeSortingAlgo()
        {
            Console.WriteLine("Hello World!");

            ArrayTestBed numbersTestBed = new ArrayTestBed(10000);
            Random       rnd            = new Random(100);

            for (int i = 0; i <= 9999; i++)
            {
                numbersTestBed.Insert((int)(rnd.NextDouble() * 10000));
            }



            Sort(bubbleSortAlgorithm, numbersTestBed.CloneObject(), numbersTestBed.CloneObject(), "Bubble");
            Sort(selectionSortAlgorithm, numbersTestBed.CloneObject(), numbersTestBed.CloneObject(), "Selection");
            Sort(insertionSortAlgorithm, numbersTestBed.CloneObject(), numbersTestBed.CloneObject(), "Insertion");
        }
        public int[] ReverseSort(ArrayTestBed arrayObject)
        {
            int tempValue;
            var numberArray = arrayObject.arr;
            int j;

            for (int i = 0; i <= arrayObject.upper; i++)
            {
                tempValue = numberArray[i];
                j         = i;
                while (j > 0 && numberArray[j - 1] < tempValue)
                {
                    numberArray[j] = numberArray[j - 1];
                    --j;
                }
                numberArray[j] = tempValue;
                if (ShowLog)
                {
                    arrayObject.DispayElements();
                }
            }
            return(numberArray);
        }
Example #6
0
        public int[] ReverseSort(ArrayTestBed testBed)
        {
            int temp;
            var arrayObject = testBed.arr;

            for (int i = testBed.upper; i > 0; i--)
            {
                for (int j = 0; j < i; ++j)
                {
                    if (arrayObject[j] < arrayObject[j + 1])
                    {
                        temp               = arrayObject[j];
                        arrayObject[j]     = arrayObject[j + 1];
                        arrayObject[j + 1] = temp;
                    }
                }

                if (ShowLog)
                {
                    testBed.DispayElements();
                }
            }
            return(arrayObject);
        }