Ejemplo n.º 1
0
        static void Main(string[] args)
        {
            int arraySize = 10000000;

            RandomArray arr = new RandomArray(arraySize);

            arr.FillArrayRandom();

            if (arraySize < 10001)
            {
                Console.WriteLine("Size of the shuffled array :" + arraySize);
                Console.WriteLine("Bubble Sort Took(miliseconds):" + BubbleSort.Sort(arr.Clone()));
                Console.WriteLine("Selection Sort Took(miliseconds):" + SelectionSort.Sort(arr.Clone()));
                Console.WriteLine("Insertion Sort Took(miliseconds):" + InsertionSort.Sort(arr.Clone()));
            }


            Console.WriteLine("Shell Sort Took(miliseconds):" + ShellSort.Sort(arr.Clone()));
            Console.WriteLine("Merge Sort Took(miliseconds):" + MergeSort.Sort(arr.Clone()));
            if (arraySize < 100000)
            {
                Console.WriteLine("Quick Sort Took(miliseconds):" + QuickSort.Sort(arr.Clone()));
            }
            Console.WriteLine("Counting Sort Took(miliseconds):" + CountingSort.Sort(arr.Clone()));
            Console.WriteLine("Radix Sort Took(miliseconds):" + RadixSort.Sort(arr.Clone()));
        }
Ejemplo n.º 2
0
        public static int[] SwithCase(int[] array, int current, int intTemp)
        {
            int[] array1 = new int[0];
            switch (current)
            {
            case 1:
                InsertionSort insertionSort = new InsertionSort();
                array1 = insertionSort.Sort(array);
                foreach (int inchvorban in array1)
                {
                    Console.Write(inchvorban + ",");
                }
                Console.WriteLine();
                Console.WriteLine("Insertion Sort time" + insertionSort.time);
                break;

            case 2:
                BubbleSort bubbleSort = new BubbleSort();
                array1 = bubbleSort.Sort(array);
                foreach (int inchvorban in array1)
                {
                    Console.Write(inchvorban + ",");
                }
                Console.WriteLine();
                Console.WriteLine("Bubble Sort time" + bubbleSort.time);
                break;

            case 3:
                QuickSort quickSort = new QuickSort();
                array1 = quickSort.Sort(array);
                foreach (int inchvorban in array1)
                {
                    Console.Write(inchvorban + ",");
                }
                Console.WriteLine();
                Console.WriteLine("Quick Sort time" + quickSort.time);
                break;

            case 4:

                HeapSort heapSort = new HeapSort();
                array1 = heapSort.Sort(array);
                foreach (int inchvorban in array1)
                {
                    Console.Write(inchvorban + ",");
                }
                Console.WriteLine();
                Console.WriteLine("Heap Sort time" + heapSort.time);
                break;

            case 5:
                MergeSort mergeSort = new MergeSort();
                mergeSort.Sort(array);
                break;

            default:
                break;
            }
            return(array1);
        }
Ejemplo n.º 3
0
        static void Main(string[] args)
        {
            //int[] bubbleSortInput = new int[] { 1, 5, 3, 10, 9, 15 };
            //Console.Write("Unsorted array for Bubble sort: ");
            //PrintArray(bubbleSortInput);
            //BubbleSort(bubbleSortInput);
            //Console.Write("After Bubble sort: ");
            //PrintArray(bubbleSortInput);

            //int[] selectionSortInput = new int[] { 1, 5, 3, 10, 9, 15 };
            //Console.Write("Unsorted array for Selection sort: ");
            //PrintArray(selectionSortInput);
            //SelectionSort(selectionSortInput);
            //Console.Write("After Selection sort: ");
            //PrintArray(selectionSortInput);

            //int[] insertionSortInput = new int[] { 1, 5, 3, 10, 9, 15 };
            //Console.Write("Unsorted array for Insertion sort: ");
            //PrintArray(insertionSortInput);
            //SelectionSort(insertionSortInput);
            //Console.Write("After Insertion sort: ");
            //PrintArray(insertionSortInput);


            int[] quickSortInput = new int[] { 1, 5, 3, 10, 9, 15, 0, 2, 4 };
            Console.Write("Unsorted array for Quick sort: ");
            PrintArray(quickSortInput);
            QuickSort.Sort(quickSortInput, 0, quickSortInput.Length - 1);
            Console.Write("After Quick sort: ");
            PrintArray(quickSortInput);
        }
Ejemplo n.º 4
0
        static void Sort(int[] values, int start, int end)
        {
            if (start >= end)
            {
                return;
            }

            int pivotIndex = (start + end) / 2;
            int pivotValue = values[pivotIndex];
            int temp       = values[pivotIndex];

            values[pivotIndex] = values[end];
            values[end]        = temp;
            int storeIndex = start;

            for (int i = start; i < end; i++)
            {
                if (values[i] < pivotValue)
                {
                    temp               = values[i];
                    values[i]          = values[storeIndex];
                    values[storeIndex] = temp;
                    storeIndex        += 1;
                }
            }
            temp = values[storeIndex];
            values[storeIndex] = values[end];
            values[end]        = temp;

            QuickSort.Sort(values, start, storeIndex - 1);
            QuickSort.Sort(values, storeIndex + 1, end);
        }
Ejemplo n.º 5
0
        static void Main(string[] args)
        {
            InsertionSort insertionSort = new InsertionSort();
            BubbleSort    bubbleSort    = new BubbleSort();
            QuickSort     quickSort     = new QuickSort();
            HeapSort      heapSort      = new HeapSort();
            MergeSort     mergeSort     = new MergeSort();

            int[] arr            = StartInput();
            int[] sortedArray    = null;
            int[] alNumbersArray = Utilities.Input();
            for (int i = 0; i < alNumbersArray.Length; i++)
            {
                switch (alNumbersArray[i])
                {
                case 1: sortedArray = insertionSort.Sort(CopyArray(arr)); break;

                case 2: sortedArray = bubbleSort.Sort(CopyArray(arr)); break;

                case 3: sortedArray = quickSort.Sort(CopyArray(arr)); break;

                case 4: sortedArray = heapSort.Sort(CopyArray(arr)); break;

                case 5: sortedArray = mergeSort.Sort(CopyArray(arr)); break;

                default: break;
                }
            }
            Algorithm[] alArr = { insertionSort, bubbleSort, quickSort, heapSort, mergeSort };
            FinalOutput(alArr);
            Console.ReadLine();
        }
Ejemplo n.º 6
0
        static void Main(string[] args)
        {
            /*
             * http://vnoi.info/wiki/algo/basic/sorting
             */

            var arr = new[] { 5, 2, 4, 6, 7, 3};

            /*
             * O(N^2)
             */
            //BubbleSort.Sort(arr);
            //SelectionSort.Sort(arr);
            //InsertionSort.Sort(arr);

            /*
             * O(NlogN)
             */
            //arr = MergeSort.Sort(arr, 0, arr.Length);
            QuickSort.Sort(arr, 0, arr.Length - 1);

            Console.WriteLine("Result:");

            for (var i = 0; i < arr.Length; i++)
            {
                Console.Write("{0} ", arr[i]);
            }

            Console.WriteLine();
            Console.ReadLine();
        }
Ejemplo n.º 7
0
        static void sortBucket(LinkedList <int> linkedList)
        {
            List <int> bucketList = new List <int>(linkedList);
            QuickSort  quickSort  = new QuickSort(bucketList);

            bucketList = (List <int>)quickSort.Sort();
            linkedList = new LinkedList <int>(bucketList);
        }
Ejemplo n.º 8
0
 static void Main(string[] args)
 {
     int[] numbersArray = { 2, 4, 1, 6, 8, 5, 3, 7 };
     int[] sortedArray  = QuickSort.Sort(numbersArray);
     foreach (var item in sortedArray)
     {
         Console.WriteLine(item);
     }
 }
Ejemplo n.º 9
0
        public static void Main()
        {
            int[] unsorted = Console.ReadLine()
                             .Split(' ', StringSplitOptions.RemoveEmptyEntries)
                             .Select(int.Parse)
                             .ToArray();

            //MergeSort<int>.Sort(unsorted);
            QuickSort <int> .Sort(unsorted);

            Console.WriteLine(string.Join(' ', unsorted));
        }
Ejemplo n.º 10
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));
        }
Ejemplo n.º 11
0
        static void QuicksortTest()
        {
            int[] integerValues = { -11, 12, -42, 0, 1, 90, 68, 6, -9 };
            QuickSort.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 };
            QuickSort.Sort(floatValues);
            Console.WriteLine(string.Join(" | ", floatValues));

            string[] stringValues = { "Mary", "Marcin", "Ann", "James", "George", "Nicole" };
            QuickSort.Sort(stringValues);
            Console.WriteLine(string.Join(" | ", stringValues));
        }
Ejemplo n.º 12
0
        private static SortResult SortArrayWithTimeResult(string identifier, int[] array, bool useMedianOfThree, int insertionSortSize)
        {
            var quickSort   = new QuickSort(useMedianOfThree, insertionSortSize);
            var startTime   = DateTime.Now;
            var sortedArray = quickSort.Sort(array);

            if (insertionSortSize > 0)
            {
                quickSort.InsertionSort(sortedArray, 0, sortedArray.Length - 1);
            }
            var stopTime = DateTime.Now;

            var totalMilliseconds = (stopTime - startTime).TotalMilliseconds;

            return(new SortResult(identifier, totalMilliseconds, sortedArray));
        }
Ejemplo n.º 13
0
        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();
        }
Ejemplo n.º 14
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));
        }
Ejemplo n.º 15
0
        public static void Main(string[] args)
        {
            var a1 = KLD.OneMillion;
            var a2 = KLD.OneMillion;
            var a3 = KLD.OneMillion;

            try
            {
                Console.WriteLine("INSERTION SORT");
                KLD.StartTimer();
                insertion.Sort(ref a1);
                KLD.StopTimer();
            }
            catch
            {
                Console.WriteLine("Error encountered");
            }

            try
            {
                Console.WriteLine("\nQUICK SORT");
                KLD.StartTimer();
                quick.Sort(ref a1);
                KLD.StopTimer();
            }
            catch
            {
                Console.WriteLine("Error encountered");
            }

            try
            {
                Console.WriteLine("\nMERGE SORT");
                KLD.StartTimer();
                merge.Sort(ref a1);
                KLD.StopTimer();
            }
            catch
            {
                Console.WriteLine("Error encountered");
            }

            Console.WriteLine("\nThe End");
            Console.ReadLine();
        }
        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();
        }
Ejemplo n.º 17
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();
        }