Example #1
0
        static void Main(string[] args)
        {
            Console.WriteLine("Merge and Sort Routine");
            int[] numbers = { 5, 10, 3, 2, 4, 7, 1, 23, 56, 17, 19, 45, 12, 10, 7 };
            Console.WriteLine("Array before sort: 5, 10, 3, 2, 4, 7, 1, 23, 56, 17, 19, 45, 12, 10, 7");
            var sortedArray = MergeSort.MergeAndSort(numbers);

            Console.Write("Array after sort: ");
            foreach (int val in sortedArray)
            {
                Console.Write(val + ",");
            }
            Console.WriteLine();
        }
        public static void Main()
        {
            var arr = new int[] { 5, 4, 3, 2, 1, 32, 455, 2, 54, 6, 8, 6, 5, 34, 23, 2, 12 };

            Console.WriteLine("Unsorted Array!");
            PrintArray(arr);

            var merge = new MergeSort();

            merge.Sort(arr, 0, arr.Length - 1);

            Console.WriteLine("Sorted Array!");
            PrintArray(arr);
        }
Example #3
0
        public void CheckMergeSort(int[] array)
        {
            MergeSort.DoMergeSort(array);

            int  min      = array[0];
            bool isSorted = true;

            for (int i = 1; i < array.Length; i++)
            {
                if (min > array[i])
                {
                    isSorted = false;
                    break;
                }
            }

            Assert.True(isSorted);
        }
Example #4
0
        static void Main(string[] args)
        {
            Random r = new Random();

            Console.WriteLine("Merge Sort");
            Console.WriteLine("Type - size");
            int size = Int32.Parse(Console.ReadLine());

            int[] a = new int[size];

            for (int i = 0; i < size; i++)
            {
                a[i] = r.Next(size * 5);
            }
            Console.Write("Before: "); showArray(a);
            Console.Write("\nAfter: "); showArray(MergeSort.Sort(a, 0, a.Length - 1));
            Console.Write("\n");
        }
        static void Main(string[] args)
        {
            List <int> numbers = new List <int>();
            Random     objR    = new Random();
            MergeSort  objMS   = new MergeSort();

            for (int i = 0; i < 10; i++)
            {
                numbers.Add(objR.Next(1, 15));
            }

            for (int i = 0; i < 10; i++)
            {
                Console.WriteLine(objMS.ComputeMergeSort(numbers)[i]);
            }

            Console.ReadLine();
        }
Example #6
0
        private static void Main()
        {
            var input = Console.ReadLine();

            if (input == string.Empty)
            {
                return;
            }

            var arr = input
                      .Split()
                      .Select(int.Parse)
                      .ToArray();

            MergeSort <int> .Sort(arr);

            Console.WriteLine(string.Join(" ", arr));
        }
        static void Main(string[] args)
        {
            int inputArraySize;

            int[]     array;
            MergeSort _mergeSort = new MergeSort();

            int[]         result;
            Random        random        = new Random();
            StringBuilder stringBuilder = new StringBuilder();

            Console.WriteLine("Do you wish to entry array size manually (Input 1) or let the computer do it for you? (Input 2) ");
            int optionselected = Convert.ToInt32(Console.ReadLine());

            switch (optionselected)
            {
            case 1: Console.WriteLine("You have choosen to input size and element of the array manually");
                Console.WriteLine("Please enter size of the array");
                inputArraySize = Convert.ToInt32(Console.ReadLine());
                array          = new int[inputArraySize];
                result         = new int[inputArraySize];
                Console.WriteLine("Please enter elements of the array");
                for (int i = 0; i < inputArraySize; i++)
                {
                    array[i] = Convert.ToInt32(Console.ReadLine());
                    stringBuilder.Append(array[i]);
                    stringBuilder.Append(" | ");
                }
                Console.WriteLine("Entered elements in the input array are :");
                Console.WriteLine(stringBuilder.ToString());

                Console.WriteLine("Performing merge sort");
                result = _mergeSort.MergeSortFunc(array);
                stringBuilder.Clear();
                Console.WriteLine("Sorting is done.Here is the sorted array");
                for (int i = 0; i < inputArraySize; i++)
                {
                    stringBuilder.Append(result[i]);
                    stringBuilder.Append(" | ");
                }
                Console.WriteLine(stringBuilder.ToString());

                break;

            case 2:  Console.WriteLine("You have choosen to let computer prepare the input array for you and do the merge sort");


                Console.WriteLine("Selecting size of an array");
                inputArraySize = random.Next(10);
                Console.WriteLine("Size selected : " + inputArraySize);
                array  = new int[inputArraySize];
                result = new int[inputArraySize];
                Console.WriteLine("Entering elements of the array");
                for (int i = 0; i < inputArraySize; i++)
                {
                    array[i] = random.Next(0, 100);
                    stringBuilder.Append(array[i]);
                    stringBuilder.Append(" | ");
                }
                Console.WriteLine("Entered elements in the input array are :");
                Console.WriteLine(stringBuilder.ToString());

                Console.WriteLine("Performing merge sort");
                result = _mergeSort.MergeSortFunc(array);
                stringBuilder.Clear();
                Console.WriteLine("Sorting is done.Here is the sorted array");
                for (int i = 0; i < inputArraySize; i++)
                {
                    stringBuilder.Append(result[i]);
                    stringBuilder.Append(" | ");
                }
                Console.WriteLine(stringBuilder.ToString());

                break;

            default: Console.WriteLine("You havent choosen from the option available"); break;
            }
            Console.ReadLine();
        }