Beispiel #1
0
        public int[] MergeSort_Recursive(int[] arr, int left, int right)
        {
            int mid;

            if (right > left)
            {
                mid = (right + left) / 2;

                MergeSort_Recursive(arr, left, mid);
                Console.WriteLine("Right: " + right.ToString());
                Console.WriteLine("Left: " + left.ToString());
                PrintArray.printArray(arr);
                Console.WriteLine("Next Call");

                MergeSort_Recursive(arr, (mid + 1), right);
                Console.WriteLine("Right: " + right.ToString());
                Console.WriteLine("Left: " + left.ToString());
                PrintArray.printArray(arr);
                Console.WriteLine("Next Call");

                return(DoMerge(arr, left, (mid + 1), right));
            }
            else
            {
                return(arr);
            }
        }
Beispiel #2
0
        /*Merge sort is a sorting technique based on divide and conquer technique.
         * With worst-case time complexity being Ο(n log n), it is one of the most respected algorithms.
         * Merge sort first divides the array into equal halves and then combines them in a sorted manner.
         */

        /*Step 1 − if it is only one element in the list it is already sorted, return.
         * Step 2 − divide the list recursively into two halves until it can no more be divided.
         * Step 3 − merge the smaller lists into new list in sorted order.
         */

        private static int[] DoMerge(int[] arr, int left, int mid, int right)
        {
            PrintArray.printArray(arr);
            Console.WriteLine("left: {0} - mid: {1} - rigth: {2}", left, mid, right);
            int[] temp = new int[25];
            int   i, left_end, num_elements, tmp_pos;

            left_end     = (mid - 1);
            tmp_pos      = left;
            num_elements = (right - left + 1);

            while ((left <= left_end) && (mid <= right))
            {
                if (arr[left] <= arr[mid])
                {
                    temp[tmp_pos++] = arr[left++];
                }
                else
                {
                    temp[tmp_pos++] = arr[mid++];
                }
            }

            while (left <= left_end)
            {
                temp[tmp_pos++] = arr[left++];
            }

            while (mid <= right)
            {
                temp[tmp_pos++] = arr[mid++];
            }

            for (i = 0; i < num_elements; i++)
            {
                arr[right] = temp[right];
                right--;
            }
            return(arr);
        }
Beispiel #3
0
        static void Main(string[] args)
        {
            Console.WriteLine("/*-----------------------*/");

            Console.WriteLine("Chose one:");
            Console.WriteLine("1  - bubble sort");
            Console.WriteLine("2  - insert sort");
            Console.WriteLine("3  - select sort");
            Console.WriteLine("4  - merge sort");
            Console.WriteLine("5  -  sort");
            var opt = Console.ReadLine();

            switch (opt)
            {
            case "1":
                BubbleSort _bs = new BubbleSort();

                int[] arr1 = ArrayGenerator.array1D();
                int[] arr2 = ArrayGenerator.array1D();

                //bs.bubbleSort_rec(arrrec, arrrec.Length)
                Console.WriteLine(@"BUBBLE SORT");
                Console.WriteLine(@"Sorted array by for loop");
                PrintArray.printArray(arr1);
                PrintArray.printArray(_bs.bubbleSort(arr1));
                Console.WriteLine(@"/*-----------------------*/");
                Console.WriteLine(@"Sorted array by recursive method");
                PrintArray.printArray(arr2);
                PrintArray.printArray(_bs.bubbleSort_rec(arr2, arr2.Length));
                Console.WriteLine(@"/*-----------------------*/");
                break;

            case "2":
                InsertSort _is = new InsertSort();

                Console.WriteLine(@"INSERT SORT");
                int[] arr3 = ArrayGenerator.array1D();
                int[] arr4 = ArrayGenerator.array1D();

                Console.WriteLine(@"Sorted array by for\while loop");
                PrintArray.printArray(arr3);
                PrintArray.printArray(_is.insertSort(arr3));
                Console.WriteLine(@"/*-----------------------*/");
                Console.WriteLine(@"Sorted array by recursive method");
                PrintArray.printArray(arr4);
                PrintArray.printArray(_is.insertSort_rec(arr4, arr4.Length));
                Console.WriteLine(@"/*-----------------------*/");
                break;

            case "3":
                SelectionSort _ss = new SelectionSort();
                Console.WriteLine(@"SELECTION SORT");
                int[] arr5 = ArrayGenerator.array1D();

                Console.WriteLine(@"Sorted array by for loop");
                PrintArray.printArray(arr5);
                PrintArray.printArray(_ss.selectionSort(arr5));
                Console.WriteLine(@"/*-----------------------*/");
                break;

            case "4":
                MergeSort _ms = new MergeSort();
                Console.WriteLine(@"SELECTION SORT");
                int[] arr6 = ArrayGenerator.array1D();

                Console.WriteLine(@"Sorted array by for loop");
                PrintArray.printArray(arr6);
                PrintArray.printArray(_ms.MergeSort_Recursive(arr6, 0, arr6.Length - 1));
                Console.WriteLine(@"/*-----------------------*/");
                break;
            }

            Console.ReadKey();
        }