public static void DoTest ()
		{
            List<int> numbersList = new List<int> { 23, 42, 4, 16, 8, 15, 3, 9, 55, 0, 34, 12, 2, 46, 25 };

            var sortedList = numbersList.MergeSort();
			Debug.Assert (sortedList[0] == 0, "Wrong min value!");
		}
Example #2
0
        /// <summary>
        /// </summary>
        /// <param name="args">
        /// The args.
        /// </param>
        private static void Main(string[] args)
        {
            // Create an unsorted array of string elements
            int[] unsorted = { 10, 2, 8, 6, 4, 7, 9, 3, 5, 7 };

            // int[] unsorted = { 10, 2 };
            List<int> list = new List<int>(unsorted);

            list.MergeSort();
            list.QuickSort(0, list.Count - 1);
        }
        static void Main(string[] args)
        {
            List<int> listOfIntegers = new List<int>() { 4, 23, 2, 15, 11, 64, 7, 3, 1 };
            List<int?> listOfNullableIntegers = new List<int?>() { 4, null, 2, 15, null, 64, 7, 11, 66, 5, 8 };
            List<string> listOfStrings = new List<string> {"asd", "asdf", "mnogodulugstring" , "s", "ae", string.Empty, "asdec"};

            listOfIntegers.BubbleSort();
            foreach (var item in listOfIntegers)
            {
                Console.Write("{0} ", item);
            }

            Console.WriteLine();

            listOfNullableIntegers.SelectionSort(new OddEvenComparer());
            foreach (var item in listOfNullableIntegers)
            {
                Console.Write("{0} ", item);
            }

            Console.WriteLine();

            listOfStrings.SelectionSort(new StringLengthComparer());
            foreach (var item in listOfStrings)
            {
                Console.Write("{0} ", item);
            }

            Console.WriteLine();

            Console.WriteLine(listOfIntegers.BSearch(1)); // 0

            List<int> listOfIntegersQS = new List<int>() { 4, 5, 1, 3, 2 };
            List<int> listOfIntegersMS = new List<int>() { 1, 7, 13, 3, 9, 5, 11 };

            listOfIntegersQS.QuickSort();
            foreach (var item in listOfIntegersQS)
            {
                Console.Write("{0} ", item);
            }

            Console.WriteLine();

            listOfIntegersMS.MergeSort();
            foreach (var item in listOfIntegersMS)
            {
                Console.Write("{0} ", item);
            }

            Console.WriteLine();
        }
        static void Main ( string[] args )
        {
            List<int> array = new List<int>() { 2 , 4 , 1 , 6 , 10 };
            Console.WriteLine(array.BinarySearch(10 , Comparer<int>.Default));

            List<int> sortedArrayBS = array.BubbleSort<int>(Comparer<int>.Default).ToList();
            List<int> sortedArraySS = array.SelectionSort<int>(Comparer<int>.Default).ToList();
            List<int> sortedArrayMS = array.MergeSort<int>(Comparer<int>.Default).ToList();
            List<int> sortedArrayQS = array.QuickSort<int>(Comparer<int>.Default).ToList();

            Console.Write("Original array: " + array.Count + "\n ");
            foreach(var item in array)
            {
                Console.Write(item + " ");
            }
            Console.WriteLine();
            Console.Write("Bubble Sort: ");
            foreach(var item in sortedArrayBS)
            {
                Console.Write(item + " ");
            }
            Console.WriteLine();
            Console.Write("Selection Sort: ");
            foreach(var item in sortedArraySS)
            {
                Console.Write(item + " ");
            }
            Console.WriteLine();
            Console.Write("Merge Sort: ");
            foreach(var item in sortedArrayMS)
            {
                Console.Write(item + " ");
            }
            Console.WriteLine();
            Console.Write("Quick Sort: ");
            foreach(var item in sortedArrayQS)
            {
                Console.Write(item + " ");
            }
            Console.WriteLine();
            Console.ReadKey();
        }
Example #5
0
        static void Main(string[] args)
        {
            while (true)
            {
                Console.Write("------------\nElements: ");
                int n = Convert.ToInt32(Console.ReadLine());
                Console.WriteLine("------------");
                var sortme = new List<int>();
                var orderme = new List<Foo>();
                DateTime startTime;

                Random randNum = new Random();
                IReadOnlyList<int> li = Enumerable.Repeat(0, n).Select(i => randNum.Next(0, 500)).ToArray();

                Console.WriteLine("\n** Sort items by value **\n");

                sortme = new List<int>(li);
                startTime = DateTime.Now;
                sortme.BubbleSort();
                Console.WriteLine("Bubble took: {0} ms", (DateTime.Now - startTime).TotalMilliseconds);

                sortme = new List<int>(li);
                startTime = DateTime.Now;
                sortme.InsertionSort();
                Console.WriteLine("Insertion took: {0} ms", (DateTime.Now - startTime).TotalMilliseconds);

                sortme = new List<int>(li);
                startTime = DateTime.Now;
                sortme.SelectionSort();
                Console.WriteLine("Selection took: {0} ms", (DateTime.Now - startTime).TotalMilliseconds);

                sortme = new List<int>(li);
                startTime = DateTime.Now;
                sortme.HeapSort();
                Console.WriteLine("Heap took: {0} ms", (DateTime.Now - startTime).TotalMilliseconds);

                sortme = new List<int>(li);
                startTime = DateTime.Now;
                sortme.MergeSort();
                Console.WriteLine("Merge took: {0} ms", (DateTime.Now - startTime).TotalMilliseconds);

                Console.WriteLine();

                Console.WriteLine("\n** Sort items by key **\n");

                IReadOnlyList<Foo> list = new List<int>(li).ConvertAll(x => new Foo { bar = x });

                orderme = new List<Foo>(list);
                startTime = DateTime.Now;
                orderme.BubbleSort(x => x.bar);
                Console.WriteLine("Bubble took: {0} ms", (DateTime.Now - startTime).TotalMilliseconds);

                orderme = new List<Foo>(list);
                startTime = DateTime.Now;
                orderme.InsertionSort(x => x.bar);
                Console.WriteLine("Insertion took: {0} ms", (DateTime.Now - startTime).TotalMilliseconds);

                orderme = new List<Foo>(list);
                startTime = DateTime.Now;
                orderme.SelectionSort(x => x.bar);
                Console.WriteLine("Selection took: {0} ms", (DateTime.Now - startTime).TotalMilliseconds);

                orderme = new List<Foo>(list);
                startTime = DateTime.Now;
                orderme.HeapSort(x => x.bar);
                Console.WriteLine("Heap took: {0} ms", (DateTime.Now - startTime).TotalMilliseconds);

                orderme = new List<Foo>(list);
                startTime = DateTime.Now;
                orderme.MergeSort(x => x.bar);
                Console.WriteLine("Merge took: {0} ms", (DateTime.Now - startTime).TotalMilliseconds);

                Console.WriteLine();
            }
        }
Example #6
0
        static unsafe void Main(string[] args)
        {
            /*Consistency benkmark.*/
            Console.WriteLine("Consistency benchmarking...");
            List<int> consistencyList1 = new List<int>() { 13, 3, 1, 0, 11, 4, 12, 1 };
            List<int> consistencyList2 = new List<int>(consistencyList1);
            List<int> consistencyList3 = new List<int>(consistencyList1);
            List<int> consistencyList4 = new List<int>(consistencyList1);
            List<int> consistencyList5 = new List<int>(consistencyList1);

            Console.WriteLine("Quick Sort:");
            consistencyList1.QuickSort();
            foreach(int val in consistencyList1)
            {
                if (consistencyList1.IndexOf(val) != consistencyList1.Count-1)
                {
                    Console.Write(val + ",");
                }

                else
                {
                    Console.WriteLine(val);
                }

            }

            Console.WriteLine();

            Console.WriteLine("Insertion Sort:");
            consistencyList2.InsertionSort();
            foreach (int val in consistencyList2)
            {
                if (consistencyList2.IndexOf(val) != consistencyList2.Count - 1)
                {
                    Console.Write(val + ",");
                }

                else
                {
                    Console.WriteLine(val);
                }

            }

            Console.WriteLine();

            Console.WriteLine("Bubble Sort:");
            consistencyList3.BubbleSort();
            foreach (int val in consistencyList3)
            {
                if (consistencyList3.IndexOf(val) != consistencyList3.Count - 1)
                {
                    Console.Write(val + ",");
                }

                else
                {
                    Console.WriteLine(val);
                }

            }

            Console.WriteLine();

            Console.WriteLine("Merge Sort:");
            consistencyList4.MergeSort();
            foreach (int val in consistencyList4)
            {
                if (consistencyList4.IndexOf(val) != consistencyList4.Count - 1)
                {
                    Console.Write(val + ",");
                }

                else
                {
                    Console.WriteLine(val);
                }

            }

            Console.WriteLine();

            Console.WriteLine("Heap Sort:");
            consistencyList5.HeapSort();
            foreach (int val in consistencyList5)
            {
                if (consistencyList5.IndexOf(val) != consistencyList5.Count - 1)
                {
                    Console.Write(val + ",");
                }

                else
                {
                    Console.WriteLine(val);
                }

            }

            Console.WriteLine();

            /*Time Benchmark*/
            int numVal = 15000;
            Console.WriteLine("Time benchmarking..." + numVal + " values");
            List<int> myNewList = new List<int>();

            Random randomizer = new Random();

            for (int i = 0; i < numVal;i++)
            {
                myNewList.Add(randomizer.Next(0, 20000));
            }

            List<int> myNewList1 = new List<int>(myNewList);
            List<int> myNewList2 = new List<int>(myNewList);
            List<int> myNewList3 = new List<int>(myNewList);
            List<int> myNewList4 = new List<int>(myNewList);
            List<int> myNewList5 = new List<int>(myNewList);

            //sort by default
            var watchDefault = System.Diagnostics.Stopwatch.StartNew();
            myNewList.Sort();
            watchDefault.Stop();
            Console.WriteLine("Default CLR sort: " + watchDefault.ElapsedMilliseconds + " ms");

            //sort by custom quick
            watchDefault.Restart();
            myNewList1.QuickSort();
            watchDefault.Stop();
            Console.WriteLine("Custom Quick sort: " + watchDefault.ElapsedMilliseconds + " ms");

            //sort by custom insertion
            watchDefault.Restart();
            myNewList2.InsertionSort();
            watchDefault.Stop();
            Console.WriteLine("Custom Insertion sort: " + watchDefault.ElapsedMilliseconds + " ms");

            //sort by custom bubble
            watchDefault.Restart();
            myNewList3.BubbleSort();
            watchDefault.Stop();
            Console.WriteLine("Custom Bubble sort: " + watchDefault.ElapsedMilliseconds + " ms");

            //sort by custom merge
            watchDefault.Restart();
            myNewList4.MergeSort();
            watchDefault.Stop();
            Console.WriteLine("Custom Merge sort: " + watchDefault.ElapsedMilliseconds + " ms");

            //sort by custom heap
            watchDefault.Restart();
            myNewList5.HeapSort();
            watchDefault.Stop();
            Console.WriteLine("Custom Heap sort: " + watchDefault.ElapsedMilliseconds + " ms");

            //done
            Console.WriteLine("Done!");

            //default pause
            Console.ReadLine();
        }
Example #7
0
 static void Main(string[] args)
 {
     IList<int> array = new List<int> { 5, 2, 1, 4, 3 };
     IList<int> sorted = array.MergeSort<int>(Comparer<int>.Default);
    // Console.WriteLine(index);
 }