Example #1
0
        public static void Sort(IComparable[] a, int lo, int hi)
        {
            if (hi <= lo + 10)
            {
                Insertion.Sort(a, lo, hi);
                return;
            }

            int         lt = lo, gt = hi;
            IComparable v = a[lo];
            int         i = lo;

            while (i <= gt)
            {
                int cmp = a[i].CompareTo(v);
                if (cmp < 0)
                {
                    Exchange(a, lt++, i++);
                }
                else if (cmp > 0)
                {
                    Exchange(a, i, gt--);
                }
                else
                {
                    i++;
                }
            }

            Sort(a, lo, lt - 1);
            Sort(a, gt + 1, hi);
        }
        private void Select(int input)
        {
            switch (input)
            {
            case 1:
                sortData = Bubble.Sort(sortData);
                break;

            case 2:
                sortData = Insertion.Sort(sortData);
                break;

            case 3:
                sortData = Selection.Sort(sortData);
                break;

            case 4:
                sortData = Heap.Sort(sortData);
                break;

            case 5:
                sortData = Quick.Sort(sortData, 0, sortData.Length - 1);
                break;

            case 6: sortData = Merge.Sort(sortData);
                break;

            default:
                Console.WriteLine("Wrong input");
                break;
            }

            PrintResults();
        }
Example #3
0
        private void QuickMedThree(List <int> list, int start, int end)
        {
            const int cutoff = 10;//point at which we switch to insertion sort.

            if (start + cutoff > end)
            {
                Insertion.InsertionSortS(list, start, end);
            }
            else
            {
                int middle = (start + end) / 2;             //find median of three for pivot
                if (list[middle] < list[start])             //  by sorting them and pivot is
                {                                           //  in the middle position
                    Swap(list, start, middle);
                    NumSwaps++;
                }

                if (list[end] < list[start])
                {
                    Swap(list, start, end);
                    NumSwaps++;
                }

                if (list[end] < list[middle])
                {
                    Swap(list, middle, end);
                    NumSwaps++;
                }
                //place pivot at position(end-1) since we know that array[end] >= array[middle]
                int pivot = list[middle];
                Swap(list, middle, end - 1);
                NumSwaps++;
                //begin partitioning
                int left, right;
                for (left = start, right = end - 1; ;)
                {
                    while (list[++left] < pivot)
                    {
                        ;
                    }
                    while (pivot < list[--right])
                    {
                        ;
                    }
                    if (left < right)
                    {
                        Swap(list, left, right);
                        NumSwaps++;
                    }
                    else
                    {
                        break;
                    }
                }
                //restore pivot
                Swap(list, left, end - 1);
                QuickMedThree(list, start, left - 1); //recursively sort left subset
                QuickMedThree(list, left + 1, end);   //recursively sort right subset
            }
        }
Example #4
0
        public static void Sort(IComparable[] a, int lo, int hi)
        {
            //if (hi <= lo) return; Improve..
            if (hi <= lo + 10)
            {
                Insertion.Sort(a, lo, hi);
                return;
            }
            int j = Partition(a, lo, hi);

            Sort(a, lo, j - 1);
            Sort(a, j + 1, hi);
        }
Example #5
0
        private static void Sort(IComparable[] a, IComparable[] aux, int lo, int hi)
        {
            //if (hi <= lo) { return; } improve..
            if (hi <= lo + 7)
            {
                Insertion.Sort(a, lo, hi);
                return;
            }
            int mid = lo + (hi - lo) / 2;

            Sort(a, aux, lo, mid);
            Sort(a, aux, mid + 1, hi);
            if (!Less(a[mid + 1], a[mid]))
            {
                return;
            }
            Merge(a, aux, lo, mid, hi);
        }
        static void Main(string[] args)
        {
            Console.WriteLine("SORTING ALGORITHMS");
            int[] array = new int[10] {
                100, 50, 20, 40, 10, 60, 80, 70, 90, 30
            };
            DateTime start;

            Console.WriteLine("The real array: [{0}]\n", string.Join(", ", array));

            //bubble
            int[] test1 = new int[array.Length];
            Array.Copy(array, test1, array.Length);
            start = DateTime.Now;
            Console.WriteLine("\tBubble sort: [{0}] in {1}s", string.Join(", ", Bubble.Sort(test1)), (DateTime.Now - start).TotalSeconds);

            //selection
            int[] test2 = new int[array.Length];
            Array.Copy(array, test2, array.Length);
            start = DateTime.Now;
            Console.WriteLine("\tSelection sort: [{0}] in {1}s", string.Join(", ", Selection.Sort(test2)), (DateTime.Now - start).TotalSeconds);

            //insetrtion
            int[] test3 = new int[array.Length];
            Array.Copy(array, test3, array.Length);
            start = DateTime.Now;
            Console.WriteLine("\tInsertion sort: [{0}] in {1}s", string.Join(", ", Insertion.Sort(test3)), (DateTime.Now - start).TotalSeconds);

            //merge
            int[] test4 = new int[array.Length];
            Array.Copy(array, test4, array.Length);
            start = DateTime.Now;
            Console.WriteLine("\tMerge sort: [{0}] in {1}s", string.Join(", ", Merge.Sort(test4)), (DateTime.Now - start).TotalSeconds);

            //quick
            int[] test5 = new int[array.Length];
            Array.Copy(array, test5, array.Length);
            start = DateTime.Now;
            Console.WriteLine("\tQuick sort: [{0}] in {1}s", string.Join(", ", Quick.Sort(test5)), (DateTime.Now - start).TotalSeconds);

            Console.WriteLine("\nThe real array: [{0}]", string.Join(", ", array));
        }
Example #7
0
        private static void Main()
        {
            var selection = 0;

            while (selection != -1)
            {
                var arr = new List <int>(new[]
                {
                    4, 5, 2, 1, 3, 7, 6, 22, 23, 25, 27, 29, 8, 9, 10, 32, 31, 43, 54, 43, 42, 12, 34, 38, 37, 36, 19,
                    18, 11, 55, 57, 56, 59, 13
                });
                Console.Clear();
                Console.WriteLine("0. Unsorted\n1. Bubble\n2. Insertion\n3. Selection\n4. Heap\n5. Radix\n6. Quick\n7. Shell\n8. Merge\n9. Tim\n10. Cocktail");
                Console.Write("Your Choice : ");
                selection = int.Parse(Console.ReadLine() ?? throw new InvalidOperationException());
                var s = "";
                switch (selection)
                {
                case 0:
                    s = "Unsorted";
                    break;

                case 1:
                    arr = Bubble <int> .Sort(arr);

                    s = "Bubble";
                    break;

                case 2:
                    arr = Insertion <int> .Sort(arr);

                    s = "Insertion";
                    break;

                case 3:
                    arr = Selection <int> .Sort(arr);

                    s = "Selection";
                    break;

                case 4:
                    arr = Heap <int> .Sort(arr);

                    s = "Heap";
                    break;

                case 5:
                    arr = Radix.Sort(arr);
                    s   = "Radix";
                    break;

                case 6:
                    arr = Quick <int> .Sort(arr);

                    s = "Quick";
                    break;

                case 7:
                    arr = Shell <int> .Sort(arr);

                    s = "Shell";
                    break;

                case 8:
                    arr = Merge <int> .Sort(arr);

                    s = "Merge";
                    break;

                case 9:
                    arr = Tim <int> .Sort(arr);

                    s = "Tim";
                    break;

                case 10:
                    arr = Cocktail <int> .Sort(arr);

                    s = "Cocktail";
                    break;

                default:
                    Console.WriteLine("Choice Wrong");
                    break;
                }

                Console.Clear();
                Console.WriteLine(s + " Sort");
                foreach (var i in arr)
                {
                    Console.Write(i + " ");
                }
                Console.ReadKey();
            }
        }