Esempio n. 1
0
        static void Main(string[] args)
        {
            var merge = new MergeSort(new int[] { 4, 8, 5, 7, 4, 9, 3, 2, 1, 10, 22, 78, 17, 201, 78, 2, 10, 24, 27 });

            merge.mergeSort();

            foreach (var i in merge.a)
            {
                Console.WriteLine(i);
            }
        }
Esempio n. 2
0
        static void Main(string[] args)
        {
            int[] input = Console.ReadLine()
                          .Split()
                          .Select(int.Parse)
                          .ToArray();

            MergeSort <int> .Sort(input);

            Console.WriteLine(string.Join(" ", input));
        }
Esempio n. 3
0
        public void Test2()
        {
            // Arrange
            var arr = new[] { 27.0, 49.0, 3.0, 43.0 };

            // Act
            MergeSort.Merge(arr, 0, 1, 3);

            // Assert
            CollectionAssert.AreEqual(new[] { 3.0, 27.0, 43.0, 49.0 }, arr);
        }
Esempio n. 4
0
        public static void Main(string[] args)
        {
            var input = new int[] { 5, 4, 9, 3, 2, 4, 5, 12, 99, 101, 1, 7, 9, 10, 13 };

            var result = MergeSort.Sort(input);


            foreach (var item in result)
            {
                Console.WriteLine(item);
            }
        }
Esempio n. 5
0
        static void Main(string[] args)
        {
            int[] numbers = { 7, 3, 5, 2, 3, 1, 5, 8 };
            var   sorter  = new MergeSort();

            sorter.Sort(numbers);

            foreach (var item in numbers)
            {
                System.Console.WriteLine(item);
            }
        }
        static void Main(string[] args)
        {
            int[]           nums   = Console.ReadLine().Split().Select(int.Parse).ToArray();
            MergeSort <int> sorter = new MergeSort <int>();
            var             sorted = sorter.Sort(nums);

            Console.WriteLine(string.Join(" ", sorted));
            //char[] chars = Console.ReadLine().Split().Select(char.Parse).ToArray();
            //MergeSort<char> sorter = new MergeSort<char>();
            //var sorted = sorter.Sort(chars);
            //Console.WriteLine(string.Join(", ", sorted));
        }
Esempio n. 7
0
        static void Main(string[] args)
        {
            var MergeSort = new MergeSort();

            int[] arr = { 1, 5, 4, 11, 20, 8, 2, 98, 90, 16, 89, -4 };

            MergeSort.Mergesort(arr, 0, arr.Length - 1);
            Console.WriteLine("Sorted Values:");
            for (int i = 0; i < arr.Length; i++)
            {
                Console.WriteLine(arr[i]);
            }
        }
Esempio n. 8
0
        static void Main(string[] args)
        {
            int[] arr = { 1, 5, 4, 11, 20, 8, 2, 98, 90, 16, 3, 100, 83, 24, 18, 33, 44, 76 };

            MergeSort mergeSort = new MergeSort(arr);

            mergeSort.printContent();
            mergeSort.sortSeq(0, arr.Length - 1);
            mergeSort.printContent();

            Console.WriteLine("\n Now concurrent sort will be running ...");
            //ExampleMergeSort.sortCon(arr);
        }
Esempio n. 9
0
        static void Main(string[] args)
        {
            var myArray = new MergeSort();

            Console.WriteLine("Before sorting:  {0}", myArray);
            Console.WriteLine("");

            myArray.Sort();                     // perofrm the sort

            Console.WriteLine("After sorting:  {0}", myArray);

            System.Threading.Thread.Sleep(10000);
        }
Esempio n. 10
0
        static void Main(string[] args)
        {
            var unsorted = new List <int> {
                6, 8, 3, 5, 1, 9, 2, 7, 4, 10
            };

            var sortedItems = MergeSort.Sort(unsorted);

            foreach (var item in sortedItems)
            {
                Console.WriteLine(item);
            }
        }
Esempio n. 11
0
        static void Main(string[] args)
        {
            List <int> unsorted = new List <int>();
            List <int> sorted;

            Stopwatch stopWatch = new Stopwatch();



            Random random = new Random();

            Console.WriteLine("Original array elements:");
            for (int i = 0; i < 10; i++)
            {
                unsorted.Add(random.Next(0, 100));
                Console.Write(unsorted[i] + " ");
            }
            Console.WriteLine();

            stopWatch.Start();

            MergeSort mergesort = new MergeSort(unsorted);

            TimeSpan ts = stopWatch.Elapsed;

            string elapsedTime = String.Format("{0:00}:{1:00}:{2:00}.{3:00}",
                                               ts.Hours, ts.Minutes, ts.Seconds,
                                               ts.Milliseconds / 10);

            Console.WriteLine("\nRunTime: " + elapsedTime + "\n");



            sorted = mergesort.Mresult;

            Console.WriteLine("Sorted array elements: ");
            foreach (int x in sorted)
            {
                Console.Write(x + " ");
            }

            //for (int i = 0; i-1 < sorted.Count; i++)
            //{
            //	if (sorted[i] > sorted[i+1])
            //	{
            //		Console.Write("\nNOT SORTED");
            //		break;
            //	}
            //}
            Console.Write("\n");
        }
Esempio n. 12
0
        /// <summary>
        /// main method to declare test arrays and call MergeSortMethod to pass test arrays in
        /// </summary>
        /// <param name="args">default</param>
        static void Main(string[] args)
        {
            Console.WriteLine("Hello World!");

            int[] array1 = { 8, 4, 23, 42, 16, 15 };
            int[] array2 = { 20, 18, 12, 8, 5, -2 };
            int[] array3 = { 5, 12, 7, 5, 5, 7 };
            int[] array4 = { 2, 3, 5, 7, 13, 11 };

            MergeSort.MergeSortMethod(array1);
            MergeSort.MergeSortMethod(array2);
            MergeSort.MergeSortMethod(array3);
            MergeSort.MergeSortMethod(array4);
        }
Esempio n. 13
0
        // Driver method
        public static void Main(String[] args)
        {
            int[] arr = { 12, 11, 13, 5, 6, 7 };

            Console.WriteLine("Given Array");
            PrintArray(arr);

            MergeSort ob = new MergeSort();

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

            Console.WriteLine("\nSorted array");
            PrintArray(arr);
        }
Esempio n. 14
0
        public static void Main(string[] args)
        {
            const int count = 100000;
            var       array = Enumerable.Range(1, count).Reverse().ToArray();

            var sw = new Stopwatch();

            sw.Start();
            MergeSort.Start(array);
            sw.Stop();

            Console.WriteLine(sw.ElapsedMilliseconds + "ms");
            // 8ms for 100000

            Console.WriteLine(array.IsAscSort());
        }
Esempio n. 15
0
        static void Main(string[] args)
        {
            int SIZE = 15;

            int[]     myArray;
            MergeSort mergeSortArray = new MergeSort(SIZE, out myArray);

            Console.WriteLine("The initial unsorted Array is : ");
            DisplayArrayElements(myArray);

            mergeSort(myArray, 0, myArray.Length - 1);

            Console.WriteLine("\n\n\nThe sorted Array is : ");
            DisplayArrayElements(myArray);

            Console.ReadKey();
        }
Esempio n. 16
0
        static void Main(string[] args)
        {
            int[] arr1 = new int[] { 7, 8, 2, 0, 4, 6 };
            int[] arr2 = new int[] { 20, 16, 40, 8, 32, 9 };

            Console.Write("arr1: ");
            Console.WriteLine(String.Concat(Array.ConvertAll(arr1, item => $"{item} ")));
            MergeSort.Sort(arr1);
            Console.Write("sorted arr1: ");
            Console.WriteLine(String.Concat(Array.ConvertAll(arr1, item => $"{item} ")));

            Console.Write("arr2: ");
            Console.WriteLine(String.Concat(Array.ConvertAll(arr2, item => $"{item} ")));
            MergeSort.Sort(arr2);
            Console.Write("sorted arr1: ");
            Console.WriteLine(String.Concat(Array.ConvertAll(arr2, item => $"{item} ")));
        }
Esempio n. 17
0
        static void Main(string[] args)
        {
            try
            {
                var options = new Options();
                if (Parser.Default.ParseArguments(args, options))
                {
                    if (!File.Exists(options.InputFile))
                    {
                        Console.WriteLine("Input file does not exist");
                        return;
                    }

                    // Read input file to array
                    int[] array;
                    using (var sw = new StreamReader(options.InputFile))
                    {
                        var text = sw.ReadToEnd();
                        array = text.Split(',').Select(int.Parse).ToArray();
                    }

                    // Sort the array
                    var mergeSort = new MergeSort();
                    mergeSort.SortMultiThread(array);

                    // Write output to file
                    var outputFile = Path.Combine(
                        Path.GetDirectoryName(options.InputFile),
                        Path.GetFileNameWithoutExtension(options.InputFile) + "_result.txt");
                    using (var sr = new StreamWriter(outputFile, false))
                    {
                        sr.WriteLine(string.Join(",", array));
                    }

                    Console.WriteLine($"Results written to {outputFile}");
                }
                else
                {
                    Console.WriteLine(options.GetUsage());
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("Something bad happened, probably because there's hardly any input validation or hardening implemented here. The message was: " + ex.Message);
            }
        }
Esempio n. 18
0
        static void Main(string[] args)
        {
            Random rng = new Random();

            Console.WriteLine("please enter the amount of number to generate");
            int count = Convert.ToInt32(Console.ReadLine());
            List <IComparable> unsortedList = new List <IComparable>(count);

            Console.WriteLine("The following is the sorted list");
            for (int i = 0; i < count; i++)
            {
                unsortedList.Add(rng.Next());
            }
            MergeSort.Sort(unsortedList)
            .ForEach(x => Console.WriteLine(x));
            Console.WriteLine("Press any key to exit");
            Console.ReadKey();
        }
Esempio n. 19
0
        static void Main(string[] args)
        {
            var numbers = HelperFunctions.GenerateIntegers(100).ToList();

            Console.WriteLine("unsorted: " + String.Join(",", numbers));

            var test = MergeSort.Sort(numbers);

            Console.WriteLine();

            Console.WriteLine("sorted: " + String.Join(",", test));

            Console.WriteLine("*********************************************************");

#if RELEASE
            BenchmarkRunner.Run <MergeSortBenchmark>();
#endif
        }
Esempio n. 20
0
        static void Main(string[] args)
        {
            // Print starting message
            Console.WriteLine("Starting tests for MergeSort vs Threaded MergeSort.");

            // Declare variables needed
            List <int> listLengths = new List <int> {
                8, 64, 256, 1024
            };
            long startTime = 0, endTime = 0;

            foreach (int length in listLengths)
            {
                // 1. Create the randomized list
                List <int> toBeSorted = new List <int>(length);                   // create list with initial capacity 'length'
                RandomIntListGenerator.Fill(ref toBeSorted, 0, Int32.MaxValue);   // randomly fill list with values from 0 to Int32.MaxValue
                List <int> toBeSorted2 = new List <int>(toBeSorted);              // create identical copy

                // 2. Test the different sorting methods for each 'length'
                // declare sorting objects
                MergeSort <int>         mergeSort         = new MergeSort <int>();
                ThreadedMergeSort <int> threadedMergeSort = new ThreadedMergeSort <int>();
                // print starting message
                Console.WriteLine(String.Format("Sorting for size {0}.", length));
                // time the sort
                startTime = DateTimeOffset.Now.ToUnixTimeMilliseconds();
                mergeSort.Sort(toBeSorted);
                endTime = DateTimeOffset.Now.ToUnixTimeMilliseconds();
                // print off the sorting's time
                Console.WriteLine(String.Format("\tNormal Sort Time: {0} ms", endTime - startTime));
                // print list for debugging to see if it's actually sorted
                //printList(toBeSorted);
                // time the threaded sort
                startTime = DateTimeOffset.Now.ToUnixTimeMilliseconds();
                threadedMergeSort.Sort(toBeSorted2);
                endTime = DateTimeOffset.Now.ToUnixTimeMilliseconds();
                // print off the threaded sorting's time
                Console.WriteLine(String.Format("\tThreaded Sort Time: {0} ms", endTime - startTime));
                // print list for debugging to see if it's actually sorted
                //printList(toBeSorted2);
            }
        }
Esempio n. 21
0
        static void Main(string[] args)
        {
            int n = Convert.ToInt32(Console.ReadLine());

            int[]  arr  = new int[n];
            Random rand = new Random();

            for (int i = 0; i < n; i++)
            {
                Console.Write(arr[i] = rand.Next(0, 100));
                Console.Write("\t");
            }
            Console.WriteLine();
            MergeSort.Merge(arr);
            for (int i = 0; i < n; i++)
            {
                Console.Write(arr[i]);
                Console.Write("\t");
            }
        }
Esempio n. 22
0
        public static void Main(string[] args)
        {
            MergeSort <int> sort = new MergeSort <int>();
            Random          ran  = new Random();

            int[] a = new int[256];
            for (int i = 0; i < a.Length; i++)
            {
                a[i] = ran.Next(0, 100);
            }
            sort.sort(a);

            foreach (int t in a)
            {
                Console.WriteLine(t);
            }

            while (true)
            {
            }
        }
Esempio n. 23
0
        static void Main(string[] args)
        {
            int    Count = 10;
            Random r     = new Random();

            int[] a = new int[Count];

            for (int i = 0; i < a.Length; i++)
            {
                a[i] = r.Next(100);
            }

            MergeSort ms = new MergeSort();

            //ms.Execute(ref a,true);
            ms.Execute(ref a, false);

            foreach (int i in a)
            {
                Console.WriteLine(i);
            }
            Console.ReadLine();
        }
Esempio n. 24
0
        static void Main(string[] args)
        {
            const int numCount = 100000;
            const int min = 0, max = 100000;

            var mergeSort = new MergeSort(numCount, min, max);

            mergeSort.Merge();
            Console.WriteLine(mergeSort.ToString());


            var bubbleSorter = new BubbleSort.BubbleSort(numCount, min, max);

            bubbleSorter.Bubble();
            Console.WriteLine(bubbleSorter.ToString());

            var tempnums = new int[numCount];

            for (int i = 0; i < numCount; i++)
            {
                tempnums[i] = new Random().Next(min, max);
            }
        }
Esempio n. 25
0
        static void testSorts(int size)
        {
            MergeSort merger = new MergeSort();

            int[] list = new int[size];

            Console.Write(" Starting test for size " + size);

            list = makelist(size);

            Stopwatch C1 = Stopwatch.StartNew();

            MergeSort.Classic(list);
            C1.Stop();

            Stopwatch C2 = Stopwatch.StartNew();

            MergeSort.Threaded(list);
            C2.Stop();

            Console.WriteLine(" - Test Completed:");
            Console.WriteLine("   Normal Sort time (ms):       " + C1.ElapsedMilliseconds);
            Console.WriteLine("   Threaded Sort time (ms):     " + C2.ElapsedMilliseconds);
        }
Esempio n. 26
0
        static public void MainFunc()
        {
            long[] array  = Utilities.GenerateNumbers(Dim);
            long   min    = Int64.MaxValue;
            long   number = 0;

            Stopwatch s = new Stopwatch();

            for (int i = 0; i < 8; i++)
            {
                try
                {
                    s.Reset();
                    s.Start();

                    MergeSort <long> c = new MergeSort <long>(i, array);
                    c.Start();

                    s.Stop();

                    if (min > s.ElapsedMilliseconds)
                    {
                        min    = s.ElapsedMilliseconds;
                        number = i;
                    }

                    //Console.WriteLine(i.ToString() + " " + s.ElapsedMilliseconds.ToString() + " ms");
                }
                catch (OutOfMemoryException)
                {
                    Console.WriteLine(i.ToString() + " " + "OutOfMemoryException");
                }
            }

            Console.WriteLine("Min: " + min.ToString() + " ms [" + number.ToString() + " (" + Math.Pow(2, number).ToString() + ")]");
        }
Esempio n. 27
0
 public static List <int> OutputResultMergeSort(int[] arr)
 {
     return(MergeSort.SortUnsorted(arr, 0, arr.Length - 1).ToList());
 }
        public void Sort()
        {
            var numbers = HelperFunctions.GenerateIntegers(100).ToList();

            MergeSort.Sort(numbers);
        }
Esempio n. 29
0
        static void Main(string[] args)
        {
            List <int> unsorted = new List <int>();
            List <int> sorted;
            List <int> unsortedList = new List <int>();
            //number of threads
            int numbers = 2000;

            int[] unsortedThreadedList = new int[numbers];

            Stopwatch stopWatch1 = new Stopwatch();
            Stopwatch stopWatch2 = new Stopwatch();



            Random random = new Random();


            //generate random number list to sort
            Console.WriteLine("Original array elements:");
            for (int i = 0; i < numbers; i++)
            {
                int newVal = random.Next(0, numbers * 2);
                unsorted.Add(newVal);
                unsortedList.Add(newVal);
                unsortedThreadedList[i] = newVal;

                //Uncomment to see unsorted numbers
                // Console.Write(unsorted[i] + " ");
            }

            Console.WriteLine("\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");

            stopWatch1.Start();
            MergeSort mergesort = new MergeSort(unsorted);
            TimeSpan  ts        = stopWatch1.Elapsed;

            string elapsedTime = String.Format("{0:00}:{1:00}:{2:00}.{3:00}",
                                               ts.Hours, ts.Minutes, ts.Seconds,
                                               ts.Milliseconds / 10);

            Console.WriteLine("\nRunTime For Unthreaded: " + elapsedTime + "\n");

            sorted = mergesort.Mresult;

            Console.WriteLine("Sorted array elements: ");

            //Uncomment loop to see sorted numbers
            foreach (int x in sorted)
            {
                Console.Write(x + " ");
            }

            Console.WriteLine("\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");

            stopWatch2.Start();
            ThreadedMergeSort threadedMergesort = new ThreadedMergeSort(unsortedThreadedList);
            TimeSpan          tsThreaded        = stopWatch2.Elapsed;

            int[] threadedSorted = new int[unsortedThreadedList.Length];
            threadedSorted = threadedMergesort.Mnums;



            string ThreadedTime = String.Format("{0:00}:{1:00}:{2:00}.{3:00}",
                                                tsThreaded.Hours, tsThreaded.Minutes, tsThreaded.Seconds,
                                                tsThreaded.Milliseconds / 10);

            Console.WriteLine("\nRunTime For threaded: " + ThreadedTime + "\n");

            Console.WriteLine("Threaded based Sorted array elements: ");

            //Uncomment loop to see sorted numbers
            for (int i = 0; i < threadedSorted.Length; i++)
            {
                Console.Write(threadedSorted[i] + " ");
            }

            Console.Write("\n\nApplication has finished, press any key to continue \n\n");
            Console.ReadKey();
        }
Esempio n. 30
0
        static void Main(string[] args)
        {
            int    LEFT   = 0;
            int    RIGHT  = 99;
            string answer = "n";
            bool   display;
            bool   retry = true;

            while (retry)
            {
                Console.BackgroundColor = ConsoleColor.White;
                Console.ForegroundColor = ConsoleColor.Black;
                Console.Clear();
                Console.WriteLine("Merge Sort. Verloka Vadim, 2017");
                Console.WriteLine(new string('-', 50));
                //input
                int n = 0;
                Console.Write("Size of array, N=");
                int.TryParse(Console.ReadLine(), out n);
                Console.Write("Display array?[y/n] ");
                display = Console.ReadLine() == "y" ? true : false;

                //sorting
                Console.Write("Start sort? [y/n] ");
                answer = Console.ReadLine();
                if (answer == "y")
                {
                    long Avrg = 0;
                    for (int i = 0; i < 10; i++)
                    {
                        //array
                        if (display)
                        {
                            Console.Write($"\nArray - {i + 1}: ");
                        }
                        Random random = new Random((int)DateTime.Now.Ticks);
                        int[]  arr    = new int[n];
                        for (int j = 0; j < n; j++)
                        {
                            arr[j] = random.Next(LEFT, RIGHT);
                            if (display)
                            {
                                Console.Write($"{arr[j]} ");
                            }
                        }
                        if (display)
                        {
                            Console.Write("\n");
                        }

                        //sort
                        Stopwatch stopWatch = new Stopwatch();
                        stopWatch.Start();
                        /*Start*/
                        arr = MergeSort.Sort(arr);
                        /*STOP*/
                        stopWatch.Stop();
                        long ts = stopWatch.ElapsedMilliseconds;
                        Console.WriteLine($"{i}. Algorithm runtime [Milliseconds] - {ts}");
                        Avrg += ts;

                        if (display)
                        {
                            Console.WriteLine("Sorted array:");
                            foreach (var item in arr)
                            {
                                Console.Write($"{item} ");
                            }
                            Console.Write('\n');
                        }
                    }
                    Console.WriteLine($"Algorithm runtime, 9 iteration - {Avrg}");
                    Console.WriteLine($"Algorithm runtime, Avrg - {Avrg / 10}");

                    Console.Write("Retry?[y/n] ");
                    retry = Console.ReadLine() == "y" ? true : false;
                }
                else
                {
                    return;
                }
            }
        }