Ejemplo n.º 1
0
        static void Main(string[] args)
        {
            // -------------------- Bubble Sort --------------------
            var bubbleNumbers = new[] { 7, 3, 1, 4, 6, 2, 3 };
            var bubbleSorter  = new BubbleSort();

            bubbleSorter.Sort(bubbleNumbers);
            Console.WriteLine($"[{string.Join(", ", bubbleNumbers)}]");

            // -------------------- Selection Sort --------------------
            var selectionNumbers = new[] { 7, 3, 1, 4, 6, 2, 3 };
            var selectionSorter  = new SelectionSort();

            selectionSorter.Sort(selectionNumbers);
            Console.WriteLine("[{0}]", string.Join(", ", selectionNumbers));

            // -------------------- Insertion Sort --------------------
            var insertionNumbers = new[] { 7, 3, 1, 4, 6, 2, 3 };
            var insertionSorter  = new InsertionSort();

            insertionSorter.Sort(insertionNumbers);
            Console.WriteLine("[{0}]", string.Join(", ", insertionNumbers));

            // -------------------- Merge Sort --------------------
            int[] mergeNumbers = { 7, 3, 1, 4, 6, 2, 3 };
            var   mergeSorter  = new MergeSort();

            mergeSorter.Sort(mergeNumbers);
            Console.WriteLine("[{0}]", string.Join(", ", mergeNumbers));

            // -------------------- Quick Sort --------------------
            int[] quickNumbers = { 7, 3, 1, 4, 6, 2, 3 };
            var   quickSorter  = new QuickSort();

            quickSorter.Sort(quickNumbers);
            Console.WriteLine("[{0}]", string.Join(", ", quickNumbers));

            // -------------------- Counting Sort --------------------
            int[] countingNumbers = { 7, 3, 1, 4, 6, 2, 3 };
            var   countingSorter  = new CountingSort();

            countingSorter.Sort(countingNumbers);
            Console.WriteLine("[{0}]", string.Join(", ", countingNumbers));

            // -------------------- Bucket Sort --------------------
            int[] bucketNumbers = { 7, 3, 1, 4, 6, 2, 3 };
            var   bucketSorter  = new BucketSort();

            bucketSorter.Sort(bucketNumbers, 3);
            Console.WriteLine("[{0}]", string.Join(", ", bucketNumbers));
        }
Ejemplo n.º 2
0
        static void InsertionTest()
        {
            int[] integerValues = { -11, 12, -42, 0, 1, 90, 68, 6, -9 };
            InsertionSort.Sort(integerValues);
            Console.WriteLine(string.Join(" | ", integerValues));

            float[] floatValues = { -11.2f, 12.56f, -42.59f, 0.0f, 1.1f, 90.9f, 68.68f, 6.1f, -9.8f };
            InsertionSort.Sort(floatValues);
            Console.WriteLine(string.Join(" | ", floatValues));

            string[] stringValues = { "Mary", "Marcin", "Ann", "James", "George", "Nicole" };
            InsertionSort.Sort(stringValues);
            Console.WriteLine(string.Join(" | ", stringValues));
        }
Ejemplo n.º 3
0
        public static void Main(string[] args)
        {
            // Bubble sort.
            Console.WriteLine("Bubble sort");
            int[]      arr        = new int[] { 5, 4, 3, 2, 1 };
            BubbleSort bubbleSort = new BubbleSort(arr);

            bubbleSort.Sort();
            Console.WriteLine(bubbleSort.ToString());

            //Insertion sort
            Console.WriteLine("Insertion sort");
            InsertionSort insertionSort = new InsertionSort(new int[] { 5, 4, 3, 2, 1 });

            insertionSort.Sort();
            Console.WriteLine(insertionSort.ToString());


            //Selection sort
            Console.WriteLine("Selection sort");
            SelectionSort selectionSort = new SelectionSort(new int[] { 5, 4, 3, 2, 1 });

            selectionSort.Sort();
            Console.WriteLine(selectionSort.ToString());

            // Quicksort
            Console.WriteLine("Quick sort");
            QuickSort quickSort = new QuickSort(new int[] { 5, 4, 3, 2, 1 });

            quickSort.Sort();
            Console.WriteLine(quickSort.ToString());

            // Merge sort
            Console.WriteLine("Merge sort");
            MergeSortImpl mergeSortImpl = new MergeSortImpl(new int[] { 5, 4, 3, 2, 1 });

            mergeSortImpl.Sort();
            Console.WriteLine(mergeSortImpl.ToString());

            // Heap sort
            Console.WriteLine("Heap sort");
            HeapSortImpl heapSortImpl = new HeapSortImpl(new int[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 });

            heapSortImpl.Sort();
            Console.WriteLine(heapSortImpl.ToString());
            Console.Read();
        }
Ejemplo n.º 4
0
        static void Main(string[] args)
        {
            var rnd     = new Random();
            var arrSize = rnd.Next(1, 1000);

            int[] arr = Enumerable.Repeat(0, arrSize).Select(x => rnd.Next(int.MinValue, int.MaxValue)).ToArray();
            var   bs  = (int[])arr.Clone();

            BubbleSort.Sort(bs);
            Assert.IsTrue(IsSorted(bs));

            var ss = (int[])arr.Clone();

            SelectionSort.Sort(ss);
            Assert.IsTrue(IsSorted(ss));

            var @is = (int[])arr.Clone();

            InsertionSort.Sort(@is);
            Assert.IsTrue(IsSorted(@is));

            var ms = (int[])arr.Clone();

            MergeSort.Sort(ms);
            Assert.IsTrue(IsSorted(ms));

            var qs = (int[])arr.Clone();

            QuickSort.Sort(qs);
            Assert.IsTrue(IsSorted(qs));

            var cs = (int[])arr.Clone();

            CountingSort.Sort(cs);
            Assert.IsTrue(IsSorted(cs));

            var rs = (int[])arr.Clone();

            RadixSort.Sort(rs);
            Assert.IsTrue(IsSorted(rs));

            var hs = (int[])arr.Clone();

            HeapSort.Sort(hs);
            Assert.IsTrue(IsSorted(hs));
        }
Ejemplo n.º 5
0
        public static void Main(string[] args)
        {
            var a1 = KLD.OneMillion;
            var a2 = KLD.OneMillion;
            var a3 = KLD.OneMillion;

            try
            {
                Console.WriteLine("INSERTION SORT");
                KLD.StartTimer();
                insertion.Sort(ref a1);
                KLD.StopTimer();
            }
            catch
            {
                Console.WriteLine("Error encountered");
            }

            try
            {
                Console.WriteLine("\nQUICK SORT");
                KLD.StartTimer();
                quick.Sort(ref a1);
                KLD.StopTimer();
            }
            catch
            {
                Console.WriteLine("Error encountered");
            }

            try
            {
                Console.WriteLine("\nMERGE SORT");
                KLD.StartTimer();
                merge.Sort(ref a1);
                KLD.StopTimer();
            }
            catch
            {
                Console.WriteLine("Error encountered");
            }

            Console.WriteLine("\nThe End");
            Console.ReadLine();
        }
        static void Main(string[] args)
        {
            // Arrays to sort
            int[]    integerValues = { -11, 12, -42, 0, 1, 90, 68, 6, -9 };
            string[] stringValues  = { "Mary", "Marcin", "Ann", "James", "George", "Nicole" };

            // Apply methods here
            SelectionSort.Sort(integerValues);
            SelectionSort.Sort(stringValues);
            InsertionSort.Sort(integerValues);
            BubbleSort.Sort(integerValues);
            QuickSort.Sort(integerValues);

            // Read output here
            Console.WriteLine(string.Join(" | ", integerValues));
            Console.WriteLine(string.Join(" | ", stringValues));
            Console.ReadLine();
        }
Ejemplo n.º 7
0
        static void Main(string[] args)
        {
            int[]  input = new int[10];
            Random rnd   = new Random();

            for (int i = 0; i < 10; i++)
            {
                input[i] = rnd.Next(0, 100);
            }

            ISort <int> sorting = new InsertionSort <int>();

            foreach (var item in input)
            {
                Console.Write(item + " ");
            }
            Console.WriteLine("After sorting");
            sorting.Sort(input);
            foreach (var item in input)
            {
                Console.Write(item + " ");
            }
            Console.ReadLine();
        }
Ejemplo n.º 8
0
        static void Main(string[] args)
        {
            int arraySize = 10;
            int arrayUpperLimits = 100;
            int repeatTimes = 1;
            bool enableLogging = true;

            int[] inputArray;
            double[] executionTime = new double[repeatTimes]; 
            Stopwatch stopwatch = new Stopwatch();

            //Slow - O(n^2)
            InsertionSort mInsertionSort = new InsertionSort();
            SelectionSort mSelectionSort = new SelectionSort();
            BubbleSort mBubbleSort = new BubbleSort();

            //Fast - O(n*lgn)
            MergeSort mMergeSort = new MergeSort();
            HeapSort mHeapSort = new HeapSort();
            QuickSort mQuickSort = new QuickSort();
           
            //Linear - O(n)
            CountingSort mCountingSort = new CountingSort();
            RadixSort mRadixSort = new RadixSort();
            BucketSort mBucketSort = new BucketSort();

            //Test the execution time repeatly.
            for (int i = 0; i < repeatTimes; i++)
            {
                //Always create new Array object, even though the contents are the same.
                //Because each array will be modified at the end of each loop.
                inputArray = ArrayHandler.Create(arraySize, enableLogging, arrayUpperLimits);
                //inputArray = ArrayHandler.CreateAlmostSorted(arraySize, enableLogging);
                //inputArray = new int[] { 122, 44, 122, 55, 33, 55, 44, 23};
                stopwatch.Start();
                //----------------------------------------------------------------------------------------//
                //             A L G O R I T H M         T E S T E D        H E R E                       //
                //----------------------------------------------------------------------------------------//

                //------- 0.Sort in VC# -------
                //Array.Sort(inputArray);                                                         //7ms 10^5

                //#####################################  O(n*n)  #############################################

                //------- 1.Insertion Sort ~ O(n^2) -------
                mInsertionSort.Sort(inputArray);                                                //95ms 10^4
                //mInsertionSort.SortWithTrace(inputArray);
                //mInsertionSort.Sort_Recursive(inputArray);

                //------- 2.Selection Sort ~ O(n^2) -------
                //mSelectionSort.Sort(inputArray);                                                //164ms 10^4
                //mSelectionSort.SortWithTrace(inputArray);

                //------- 3.Bubble Sort ~ O(n^2) -------
                //mBubbleSort.Sort(inputArray);                                                   //600ms 10^4
                //mBubbleSort.OriginalBubbleSort(inputArray);                                     //550ms 10^4

                //###################################  O(n*lgn)  #############################################

                //------- 4.Merge Sort ~ O(n*lgn) -------
                //mMergeSort.Sort(inputArray);                                                    //27ms 10^5
                //mMergeSort.Sort_Enhanced(inputArray);                                           //25ms 10^5

                //------- 5.Heap Sort ~ O(n*lgn) -------
                //mHeapSort.Sort(inputArray);                                                     //53ms 10^5

                //------- 6.Quick Sort ~ O(n*lgn) -------
                //mQuickSort.Sort(inputArray);                                                      //40ms 10^5
                //mQuickSort.Sort_Hoare(inputArray, enableLogging);                               //23ms 10^5
                //mQuickSort.Sort_Lomuto(inputArray, enableLogging);

                //######################################  O(n)  ##############################################

                //------- 7.Counting Sort ~ O(n) -------
                //inputArray = mCountingSort.Sort(inputArray);                                    //2ms 10^5

                //------- 8.Radix Sort ~ O(n) -------
                //inputArray = mRadixSort.Sort(inputArray, enableLogging);                        //114ms 10^5

                //------- 9.Bucket Sort ~ O(n) -------
                //inputArray = mBucketSort.Sort(inputArray);                                      //13ms 10^5

                //------------------------------------------------------------------------------------------
                //             A L G O R I T H M         T E S T        E N D E D
                //------------------------------------------------------------------------------------------
                stopwatch.Stop();
                executionTime[i] = stopwatch.ElapsedMilliseconds;
                Console.Write(executionTime[i] + " ");
                Console.WriteLine("");
                stopwatch.Reset();
                ArrayHandler.Print(inputArray, enableLogging);
            }

            //Print Execution Time
            double ts = executionTime.Average();
            Console.WriteLine("Average Execution Time in milliseconds: " + ts);

            Console.ReadLine();
        }
Ejemplo n.º 9
0
        static void Main(string[] args)
        {
            int[]         unsortedArray = new int[] { 12, 5, 45, 10, 20, 2, 1 };
            int[]         sortedArray   = new int[] { 1, 3, 5, 10, 39, 50, 57 };
            string[]      stringArray   = new string[] { "zz", "y", "aaz", "aa" };
            SelectionSort sort          = new SelectionSort();
            Searching     search        = new Searching();
            bool          result;



            #region 1. Insertion Sort

            InsertionSort insertionSort = new InsertionSort();
            insertionSort.Sort(unsortedArray);
            Console.WriteLine();
            insertionSort.SortVersion2(unsortedArray);
            Console.WriteLine();
            #endregion

            #region 2. Merge sort

            int[]          arr   = { 38, 27, 43, 3, 9, 82, 10 }; // this is the array to be sorted
            MergeSortArray merge = new MergeSortArray();

            //// Calling Merge Procedure
            merge.Mergesort(arr, 0, arr.Length - 1);
            ArrayHelpers.PrintIntArray(arr);



            //// Printing Sorted array. after merge sort
            //foreach (int a in arr)
            //{
            //    Console.Write(a + " ");
            //}

            #endregion

            #region 3. Binary sort

            Console.WriteLine("Binary Sort");
            ArrayHelpers.PrintIntArray(unsortedArray);
            BinarySort binarySort = new BinarySort();
            binarySort.Sort(unsortedArray);

            #endregion

            #region 4. Binary Search
            Console.WriteLine("Binary search");
            BinarySearch binarySearch = new BinarySearch();
            binarySearch.FindNumber(sortedArray, 57);


            #endregion



            //CArray nums = new CArray();
            //Random rnd = new Random(100);
            //for(int i=0; i<10; i++)
            //    nums.Insert((int)(rnd.NextDouble() * 100));



            //sort.StringBubbleSort(stringArray);
            //sort.BubbleSort(unsortedArray);



            //sort.SelectionSort(unsortedArray);
            //sort.SelectionSort(stringArray);


            //result = search.LinearSearch(unsortedArray, 60);
            //Console.WriteLine(result);

            //result = search.LinearSearchWithSwap(unsortedArray, 10);
            //Console.WriteLine(result);

            //int min = search.FindMin(unsortedArray);
            //Console.WriteLine(min);

            //result = search.BinarySearch(sortedArray, 57);
            //Console.WriteLine(result);

            //result = search.BinarySearchRecursive(sortedArray, 0, 6, 50);
            //    Console.WriteLine(result);

            //MergeTwoSortArray mergeArray = new MergeTwoSortArray();
            //mergeArray.mergeSortedArray();

            Console.ReadLine();
        }