예제 #1
0
        public void Sort(int[] input)
        {
            var watch = Stopwatch.StartNew();

            try
            {
                for (int i = 0; i <= input.Length - 2; i++)
                {
                    for (int j = 0; j <= input.Length - 2; j++)
                    {
                        if (input[j] > input[j + 1])
                        {
                            int Temp = input[j];
                            input[j]     = input[j + 1];
                            input[j + 1] = Temp;
                        }
                    }
                }

                watch.Stop();
            }
            finally
            {
                PrintArray printArray = new PrintArray();
                printArray.PrintIntegerArray(input);
                Console.WriteLine($" Bubble Sort executed in {watch.ElapsedTicks} ticks");
            }
        }
예제 #2
0
        public void Sort(int[] input)
        {
            var watch = Stopwatch.StartNew();

            try
            {
                for (int i = 0; i < input.Length - 1; i++)
                {
                    for (int j = i + 1; j > 0; j--)
                    {
                        if (input[j - 1] > input[j])
                        {
                            int TemporaryVariable = input[j];
                            input[j]     = input[j - 1];
                            input[j - 1] = TemporaryVariable;
                        }
                    }
                }

                watch.Stop();
            }
            finally
            {
                PrintArray printArray = new PrintArray();
                printArray.PrintIntegerArray(input);
                Console.WriteLine($" Insertion Sort executed in {watch.ElapsedTicks} ticks");
            }
        }
예제 #3
0
        static void Main(string[] args)
        {
            BubbleSort    bubbleSort    = new BubbleSort();
            InsertionSort insertionSort = new InsertionSort();
            SelectionSort selectionSort = new SelectionSort();
            ShellSort     shellSort     = new ShellSort();
            PrintArray    printArray    = new PrintArray();

            Console.Write("Unsorted Array \n");
            int[] Array = new int[] { 9, 8, 7, 6, 5, 4, 3, 2, 1 };
            printArray.PrintIntegerArray(Array);
            Console.Write("\n");
            Console.Write("\n");
            Console.Write("Insertion Sorted Array \n");
            insertionSort.Sort(Array);
            Console.Write("\n");
            Console.Write("Bubble Sorted Array \n");
            bubbleSort.Sort(Array);
            Console.Write("\n");
            Console.Write("Selection Sorted Array \n");
            selectionSort.Sort(Array);
            Console.Write("\n");
            Console.Write("Shell Sorted Array \n");
            shellSort.Sort(Array, 9);
        }
예제 #4
0
        public void Sort(int[] input)
        {
            var watch = Stopwatch.StartNew();

            try
            {
                int smallestIndex;
                for (int i = 0; i < input.Length - 1; i++)
                {
                    smallestIndex = i;
                    for (int currentIndex = i + 1; currentIndex < input.Length; currentIndex++)
                    {
                        if (input[currentIndex] < input[smallestIndex])
                        {
                            smallestIndex = currentIndex;
                        }
                    }
                    int TemporaryVariable = input[i];
                    input[i]             = input[smallestIndex];
                    input[smallestIndex] = TemporaryVariable;
                }

                watch.Stop();
            }
            finally
            {
                PrintArray printArray = new PrintArray();
                printArray.PrintIntegerArray(input);
                Console.WriteLine($" Selection Sort executed in {watch.ElapsedTicks} ticks");
            }
        }
예제 #5
0
        public void Sort(int[] input, int array_size)
        {
            var watch = Stopwatch.StartNew();

            try
            {
                int i, j, inc, temp;
                inc = 3;
                while (inc > 0)
                {
                    for (i = 0; i < array_size; i++)
                    {
                        j    = i;
                        temp = input[i];
                        while ((j >= inc) && (input[j - inc] > temp))
                        {
                            input[j] = input[j - inc];
                            j        = j - inc;
                        }
                        input[j] = temp;
                    }
                    if (inc / 2 != 0)
                    {
                        inc = inc / 2;
                    }
                    else if (inc == 1)
                    {
                        inc = 0;
                    }
                    else
                    {
                        inc = 1;
                    }
                }

                watch.Stop();
            }
            finally
            {
                PrintArray printArray = new PrintArray();
                printArray.PrintIntegerArray(input);
                Console.WriteLine($" Shell Sort executed in {watch.ElapsedTicks} ticks");
            }
        }