Пример #1
0
        static void Main(string[] args)
        {
            // -------------------- Bubble Sort --------------------
            var bubbleNumbers = new[] { 7, 3, 1, 4, 6, 2, 3 };
            var bubbleSorter  = new BubbleSort();

            bubbleSorter.Sort(bubbleNumbers);
            Console.WriteLine($"[{string.Join(", ", bubbleNumbers)}]");

            // -------------------- Selection Sort --------------------
            var selectionNumbers = new[] { 7, 3, 1, 4, 6, 2, 3 };
            var selectionSorter  = new SelectionSort();

            selectionSorter.Sort(selectionNumbers);
            Console.WriteLine("[{0}]", string.Join(", ", selectionNumbers));

            // -------------------- Insertion Sort --------------------
            var insertionNumbers = new[] { 7, 3, 1, 4, 6, 2, 3 };
            var insertionSorter  = new InsertionSort();

            insertionSorter.Sort(insertionNumbers);
            Console.WriteLine("[{0}]", string.Join(", ", insertionNumbers));

            // -------------------- Merge Sort --------------------
            int[] mergeNumbers = { 7, 3, 1, 4, 6, 2, 3 };
            var   mergeSorter  = new MergeSort();

            mergeSorter.Sort(mergeNumbers);
            Console.WriteLine("[{0}]", string.Join(", ", mergeNumbers));

            // -------------------- Quick Sort --------------------
            int[] quickNumbers = { 7, 3, 1, 4, 6, 2, 3 };
            var   quickSorter  = new QuickSort();

            quickSorter.Sort(quickNumbers);
            Console.WriteLine("[{0}]", string.Join(", ", quickNumbers));

            // -------------------- Counting Sort --------------------
            int[] countingNumbers = { 7, 3, 1, 4, 6, 2, 3 };
            var   countingSorter  = new CountingSort();

            countingSorter.Sort(countingNumbers);
            Console.WriteLine("[{0}]", string.Join(", ", countingNumbers));

            // -------------------- Bucket Sort --------------------
            int[] bucketNumbers = { 7, 3, 1, 4, 6, 2, 3 };
            var   bucketSorter  = new BucketSort();

            bucketSorter.Sort(bucketNumbers, 3);
            Console.WriteLine("[{0}]", string.Join(", ", bucketNumbers));
        }
Пример #2
0
        static void BubbleTest()
        {
            int[] integerValues = { -11, 12, -42, 0, 1, 90, 68, 6, -9 };
            BubbleSort.Sort(integerValues);
            Console.WriteLine(string.Join(" | ", integerValues));

            float[] floatValues = { -11.2f, 12.56f, -42.59f, 0.0f, 1.1f, 90.9f, 68.68f, 6.1f, -9.8f };
            BubbleSort.Sort(floatValues);
            Console.WriteLine(string.Join(" | ", floatValues));

            string[] stringValues = { "Mary", "Marcin", "Ann", "James", "George", "Nicole" };
            BubbleSort.Sort(stringValues);
            Console.WriteLine(string.Join(" | ", stringValues));
        }
        public static void Main(string[] args)
        {
            // Bubble sort.
            Console.WriteLine("Bubble sort");
            int[]      arr        = new int[] { 5, 4, 3, 2, 1 };
            BubbleSort bubbleSort = new BubbleSort(arr);

            bubbleSort.Sort();
            Console.WriteLine(bubbleSort.ToString());

            //Insertion sort
            Console.WriteLine("Insertion sort");
            InsertionSort insertionSort = new InsertionSort(new int[] { 5, 4, 3, 2, 1 });

            insertionSort.Sort();
            Console.WriteLine(insertionSort.ToString());


            //Selection sort
            Console.WriteLine("Selection sort");
            SelectionSort selectionSort = new SelectionSort(new int[] { 5, 4, 3, 2, 1 });

            selectionSort.Sort();
            Console.WriteLine(selectionSort.ToString());

            // Quicksort
            Console.WriteLine("Quick sort");
            QuickSort quickSort = new QuickSort(new int[] { 5, 4, 3, 2, 1 });

            quickSort.Sort();
            Console.WriteLine(quickSort.ToString());

            // Merge sort
            Console.WriteLine("Merge sort");
            MergeSortImpl mergeSortImpl = new MergeSortImpl(new int[] { 5, 4, 3, 2, 1 });

            mergeSortImpl.Sort();
            Console.WriteLine(mergeSortImpl.ToString());

            // Heap sort
            Console.WriteLine("Heap sort");
            HeapSortImpl heapSortImpl = new HeapSortImpl(new int[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 });

            heapSortImpl.Sort();
            Console.WriteLine(heapSortImpl.ToString());
            Console.Read();
        }
Пример #4
0
        static void Main(string[] args)
        {
            var rnd     = new Random();
            var arrSize = rnd.Next(1, 1000);

            int[] arr = Enumerable.Repeat(0, arrSize).Select(x => rnd.Next(int.MinValue, int.MaxValue)).ToArray();
            var   bs  = (int[])arr.Clone();

            BubbleSort.Sort(bs);
            Assert.IsTrue(IsSorted(bs));

            var ss = (int[])arr.Clone();

            SelectionSort.Sort(ss);
            Assert.IsTrue(IsSorted(ss));

            var @is = (int[])arr.Clone();

            InsertionSort.Sort(@is);
            Assert.IsTrue(IsSorted(@is));

            var ms = (int[])arr.Clone();

            MergeSort.Sort(ms);
            Assert.IsTrue(IsSorted(ms));

            var qs = (int[])arr.Clone();

            QuickSort.Sort(qs);
            Assert.IsTrue(IsSorted(qs));

            var cs = (int[])arr.Clone();

            CountingSort.Sort(cs);
            Assert.IsTrue(IsSorted(cs));

            var rs = (int[])arr.Clone();

            RadixSort.Sort(rs);
            Assert.IsTrue(IsSorted(rs));

            var hs = (int[])arr.Clone();

            HeapSort.Sort(hs);
            Assert.IsTrue(IsSorted(hs));
        }
        static void Main(string[] args)
        {
            // Arrays to sort
            int[]    integerValues = { -11, 12, -42, 0, 1, 90, 68, 6, -9 };
            string[] stringValues  = { "Mary", "Marcin", "Ann", "James", "George", "Nicole" };

            // Apply methods here
            SelectionSort.Sort(integerValues);
            SelectionSort.Sort(stringValues);
            InsertionSort.Sort(integerValues);
            BubbleSort.Sort(integerValues);
            QuickSort.Sort(integerValues);

            // Read output here
            Console.WriteLine(string.Join(" | ", integerValues));
            Console.WriteLine(string.Join(" | ", stringValues));
            Console.ReadLine();
        }
Пример #6
0
        public static List <int> Sort5(List <int> input)
        {
            List <int> result = new List <int>();
            int        pivot  = input[(int)(input.Count / 4)];

            List <int> smaller   = new List <int>();
            List <int> larger    = new List <int>();
            List <int> pivotList = new List <int>();

            foreach (int number in input)
            {
                if (number < pivot)
                {
                    smaller.Add(number);
                }
                else if (number == pivot)
                {
                    pivotList.Add(number);
                }
                else
                {
                    larger.Add(number);
                }
            }

            if (smaller.Count > 20)
            {
                result.AddRange(Sort5(smaller));
            }
            else
            {
                result.AddRange(BubbleSort.Sort(smaller));
            }
            result.AddRange(pivotList);
            if (larger.Count > 20)
            {
                result.AddRange(Sort5(larger));
            }
            else
            {
                result.AddRange(BubbleSort.Sort(larger));
            }
            return(result);
        }
Пример #7
0
        static void Main(string[] args)
        {
            Random random = new Random();

            Console.WriteLine("Enter the Number");

            int c = Console.Read();

            int[] arr = new int[c];

            for (int i = 0; i < c; i++)
            {
                arr[i] = random.Next();
            }


            Console.WriteLine("Choose The Algorhitm ");

            Console.WriteLine("1: InsertSort");

            Console.WriteLine("2: BubbleSort");

            Console.WriteLine("3: Quick Sort");

            Console.WriteLine("4: Heap Sort");

            Console.WriteLine("5: Merge Sort");

            Console.WriteLine("6: All");



            string s = Console.ReadLine();

            if (s.Length > 1)
            {
                for (int i = Convert.ToInt32(s.Substring(0, 1)); i <= Convert.ToInt32(s.Substring(2, 3)); i++)
                {
                    switch (Convert.ToString(i))
                    {
                    case "1":
                        InsertSort.Sort(arr);
                        break;

                    case "2":
                        BubbleSort.Sort(arr);
                        break;

                    case "3":
                        QuickSort.Sort(arr, 0, arr.Length - 1);
                        break;

                    case "4":
                        HeapSort.Sort(ref arr);
                        break;

                    case "5":
                        MergeSort.Sort(ref arr, 0, arr.Length - 1);
                        break;
                    }
                }
            }
            else
            {
                switch (s)
                {
                case "1":
                    InsertSort.Sort(arr);
                    break;

                case "2":
                    BubbleSort.Sort(arr);
                    break;

                case "3":
                    QuickSort.Sort(arr, 0, arr.Length - 1);
                    break;

                case "4":
                    HeapSort.Sort(ref arr);
                    break;

                case "5":
                    MergeSort.Sort(ref arr, 0, arr.Length - 1);
                    break;

                case "6":
                    InsertSort.Sort(arr);
                    BubbleSort.Sort(arr);
                    QuickSort.Sort(arr, 0, arr.Length - 1);
                    HeapSort.Sort(ref arr);
                    MergeSort.Sort(ref arr, 0, arr.Length - 1);
                    break;
                }
            }

            Console.ReadLine();
        }