예제 #1
0
        static void Main(string[] args)
        {
            int[] Arr = new int[] { 1, 100, 45, 62, 3, 2, 1000 };

            //Merge Sort
            Merge_Sort mergeobj = new Merge_Sort();

            mergeobj.MergeSort(Arr);
            Console.WriteLine("Sorting using Merge Sort Method");
            mergeobj.Print(Arr);

            //Counting Sort
            Console.WriteLine("Sorting using Counting Sort Method");
            CountingSort obj = new CountingSort();

            obj.Counting_Sort(Arr, 10001);

            //Heap Sort
            Console.WriteLine("Sorting using Heap Sort Method");
            HeapSort heap = new HeapSort();

            heap.Heap_Sort(Arr);

            Console.ReadKey();
        }
예제 #2
0
        static void Main(string[] args)
        {
            List<int> list = new List<int>();

            Random rand = new Random();

            for (int i = 0; i < 100000; i++ )
            {
                list.Add(rand.Next(-500,500));
            }

            List<ISorting> sortings = new List<ISorting>();
            HeapSort heapSort = new HeapSort();
            sortings.Add(heapSort);
            CoctailSort coctailSort = new CoctailSort();
            sortings.Add(coctailSort);
            ShellSort shellSort = new ShellSort();
            sortings.Add(shellSort);

            string[] name = new string[] { "heapsort", "coctailsort", "shellsort" };
            int num = 0;
            foreach (ISorting i in sortings)
            {
                Stopwatch stopWatch = new Stopwatch();
                stopWatch.Start();
                i.Sort<List<int>, int>(list);
                stopWatch.Stop();
                Console.WriteLine("Time of {0} sort = {1}", name[num], stopWatch.Elapsed);
                num++;
            }

            Console.ReadKey();
        }
예제 #3
0
        static void Main(string[] args)
        {
            Table table = new Table();

            table.CreateRandomTable();
            int[] cloneTable = new int[table.SortTable.Length];
            cloneTable = (int[])table.SortTable.Clone();
            int[] cloneTable2 = new int[table.SortTable.Length];
            cloneTable2 = (int[])table.SortTable.Clone();


            SelectionSort sort = new SelectionSort();

            sort.Sort(table.SortTable, typeof(SelectionSort));


            InsertionSort insertionSort = new InsertionSort();

            insertionSort.Sort(cloneTable, typeof(InsertionSort));

            HeapSort heapSort = new HeapSort();

            heapSort.Sort(cloneTable2, typeof(HeapSort));

            Console.ReadKey();
        }
예제 #4
0
        static string[] CallHeapSort()
        {
            string[] HS = new string[s.Length];
            for (int i = 0; i < s.Length; i++)
            {
                HS[i] = s[i];
            }

            HeapSort  heapSort  = new HeapSort();
            Stopwatch stopwatch = new Stopwatch();

            stopwatch.Start();

            heapSort.Sort(HS);

            stopwatch.Stop();
            Console.WriteLine("\nHeap Sort - Time Elapsed: " + stopwatch.Elapsed);

            //var temp = " ";
            //foreach (string str in SS)
            //{
            //    if (!str.StartsWith(temp[0]))
            //    {
            //        Console.Write("\n" + str[0] + ": ");
            //    }
            //    Console.Write(str + " ");
            //    temp = str;
            //}

            return(HS);
        }
        static void HeapSortTest()
        {
            int[] arr = { -11, 12, -42, 0, 1, 90, 68, 6, -9 };
            HeapSort.Sort(arr);
            Console.WriteLine(string.Join(" | ", arr));

            string[] stringValues = { "Mary", "Marcin", "Ann", "James", "George", "Nicole" };
            HeapSort.Sort(stringValues);
            Console.WriteLine(string.Join(" | ", stringValues));
        }
예제 #6
0
        static void Main(string[] args)
        {
            List<int> seznam = new List<int>();
            int pocet = 10000;
            Random r = new Random();
            while (pocet-- > 0)
            {
                seznam.Add(r.Next());
            }

            //InsertSort<int> sorting = new InsertSort<int>(seznam);
            //BubbleSort<int> sorting = new BubbleSort<int>(seznam);
            //ShakerSort<int> sorting = new ShakerSort<int>(seznam);
            //QuickSort<int> sorting = new QuickSort<int>(seznam);
            HeapSort<int> sorting = new HeapSort<int>(seznam);
            Stopwatch sw = new Stopwatch();
            sw.Start();
            sorting.sort();
            sw.Stop();
            Console.WriteLine("Seřazeno za: " + sw.ElapsedMilliseconds + " ms");
        }
예제 #7
0
        static void Main(string[] args)
        {
            Console.Write("Enter size : ");
            int size = int.Parse(Console.ReadLine());

            // Generate array with random elements
            int[] arr = Generator(size);

            // Array for sorting
            int[] changedArr = new int[arr.Length];

            // Array for calculating time
            TimeSpan[] delta = new TimeSpan[5];

            Print(arr);

            Console.Write("Select which algorithm you want to perform :\n1-Insertion sort\n" +
                          "2-Bubble sort\n3-Quicksort\n4-Heap sort\n5-Merge sort\n6-All\nEnter number(s) : ");

            string n = Console.ReadLine();

            string[] num;

            // Exploration of input string
            if (n.Contains(','))
            {
                num = n.Split(',');
            }
            else if (n.Contains('-'))
            {
                num = new string[int.Parse((n[n.Length - 1]).ToString()) - int.Parse(n[0].ToString()) + 1];
                int t = 0;
                for (int i = int.Parse(n[0].ToString()); i <= int.Parse((n[n.Length - 1]).ToString()); i++)
                {
                    num[t++] = i.ToString();
                }
            }
            else
            {
                num = new string[] { n }
            };

            // Call needed function or functions
            for (int i = 0; i < num.Length; i++)
            {
                switch (num[i])
                {
                case "1": changedArr = InsertionSort.Sort(arr); Print(changedArr); Console.WriteLine("Memory = " + InsertionSort.MemoryAllocation() + " byte"); break;

                case "2": changedArr = BubbleSort.Sort(arr); Print(changedArr); Console.WriteLine("Memory = " + BubbleSort.MemoryAllocation() + " byte"); break;

                case "3": changedArr = QuickSort.Sort(arr); Print(changedArr); Console.WriteLine("Memory = " + QuickSort.MemoryAllocation() + " byte"); break;

                case "4": changedArr = HeapSort.Sort(arr); Print(changedArr); Console.WriteLine("Memory = " + HeapSort.MemoryAllocation() + " byte"); break;

                case "5": changedArr = MergeSort.Sort(arr); Print(changedArr); Console.WriteLine("Memory = " + MergeSort.MemoryAllocation() + " byte"); break;

                case "6":
                    DateTime inT1 = DateTime.Now;
                    changedArr = InsertionSort.Sort(arr); Print(changedArr);
                    DateTime inT2 = DateTime.Now;  delta[0] = inT2 - inT1;
                    Console.WriteLine("Memory = " + InsertionSort.MemoryAllocation() + " byte");

                    DateTime buT1 = DateTime.Now;
                    changedArr = BubbleSort.Sort(arr); Print(changedArr);
                    DateTime buT2 = DateTime.Now;  delta[1] = buT2 - buT1;
                    Console.WriteLine("Memory = " + BubbleSort.MemoryAllocation() + " byte");

                    DateTime quT1 = DateTime.Now;
                    changedArr = QuickSort.Sort(arr); Print(changedArr);
                    DateTime quT2 = DateTime.Now;  delta[2] = quT2 - quT1;
                    Console.WriteLine("Memory = " + QuickSort.MemoryAllocation() + " byte");

                    DateTime heT1 = DateTime.Now;
                    changedArr = HeapSort.Sort(arr); Print(changedArr);
                    DateTime heT2 = DateTime.Now;  delta[3] = heT2 - heT1;
                    Console.WriteLine("Memory = " + HeapSort.MemoryAllocation() + " byte");

                    DateTime meT1 = DateTime.Now;
                    changedArr = MergeSort.Sort(arr); Print(changedArr);
                    DateTime meT2 = DateTime.Now;  delta[4] = meT2 - meT1;
                    Console.WriteLine("Memory = " + MergeSort.MemoryAllocation() + " byte");

                    for (int j = 0; j < delta.Length; j++)
                    {
                        if (delta[j] == delta.Min())
                        {
                            Console.ForegroundColor = ConsoleColor.Green;
                            Console.WriteLine(delta[j]);
                            Console.ResetColor();
                        }
                        else
                        {
                            Console.WriteLine(delta[j]);
                        }
                    }

                    break;

                default: Console.WriteLine("Incorrect number!"); break;
                }
            }
        }