Fast sorting of C ා

Keywords: C# less

Algorithm description

1. Assuming that the first element of the array is pivot, set the index of the first (begin) and last (end) of the array;

2. Compare the corresponding element of the last index with pivot. If the corresponding element of the last index is greater than pivot, subtract one (end -) from the last index until the element is greater than pivot. Cover the element to the first place, and the value on the corresponding index is empty;

3. Compare the corresponding element of the first index with the pivot. If the corresponding element of the first index is smaller than the pivot element, add one (begin + +) to the first index until it is smaller than the pivot element. Cover the element to the vacated position in step 2, and at the same time empty the value on the corresponding index;

4. Repeat step 2 and step 3 until begin==end ends the cycle. At this time, begin and end have pointed to the same position, and the "pivot" element is covered to this position;

5. At this time, the elements in front of the position are smaller than the "pivot" elements, and the elements behind the position are larger than the "pivot" elements. At the end of the first cycle, perform the same steps for the two parts, and finally sort the values in the array through "recursion";

code implementation

        public int[] Quick(int[] arr, int begin, int end) // Recursion by self calling
        {
            if (begin >= end)
            {
                return arr;
            }

            int pivotIndex = QuickCondition(arr, begin, end); // Get pivot index

            Quick(arr, begin, pivotIndex - 1); // Compare again for all elements smaller than pivot
            Quick(arr, pivotIndex + 1, end);// Compare again for all elements larger than pivot

            return arr;
        }

        public int QuickCondition(int[] arr, int begin, int end)
        {
            int pivot = arr[begin]; // Set pivot first element

            while (begin < end)
            {
                while (arr[end] > pivot && begin < end) // Index less than pivot element found by comparison, replaced
                {
                    end--;
                }

                arr[begin] = arr[end];

                while (arr[begin] < pivot && begin < end) // Find the index greater than pivot element by comparison and replace it
                {
                    begin++;
                }

                arr[end] = arr[begin];
            }
            arr[begin] = pivot; // When begin == end Jump out of loop to override pivot to index

            return begin;
        }

Complete code

using System;

namespace QuickSortApplication
{
    class Program
    {
        static void Main(string[] args)
        {
            var setArray = new SetArray();
            var quickSort = new QuickSort();

            int[] arr = setArray.GetArray();
            int[] array = quickSort.Quick(arr, 0, arr.Length - 1);
            quickSort.DisPlay(array);
            
            Console.ReadLine();
        }
    }

    class SetArray
    {
        public int[] GetArray()
        {
            int length;
            int[] arr;

            Console.WriteLine("Please enter the array length:");
            length = Convert.ToInt32(Console.ReadLine());

            arr = new int[length];

            for (int i = 0; i <= length - 1; i++)
            {
                Console.Write("Please enter the{0}Digit value:", i);
                arr[i] = Convert.ToInt32(Console.ReadLine());
            }

            Console.Clear();

            Console.Write("arr[] = {");
            foreach (int item in arr)
            {
                Console.Write(item + " ");
            }
            Console.Write("}\n");
            return arr;
        }
    }

    class QuickSort
    {
        public int[] Quick(int[] arr, int begin, int end) // Recursion by self calling
        {
            if (begin >= end)
            {
                return arr;
            }

            int pivotIndex = QuickCondition(arr, begin, end); // Get pivot index

            Quick(arr, begin, pivotIndex - 1); // Compare again for all elements smaller than pivot
            Quick(arr, pivotIndex + 1, end);// Compare again for all greater than pivot elements

            return arr;
        }

        public int QuickCondition(int[] arr, int begin, int end)
        {
            int pivot = arr[begin]; // Set pivot first element

            while (begin < end)
            {
                while (arr[end] > pivot && begin < end) // Index less than pivot element found by comparison, replaced
                {
                    end--;
                }

                arr[begin] = arr[end];

                while (arr[begin] < pivot && begin < end) // Find the index greater than pivot element by comparison and replace it
                {
                    begin++;
                }

                arr[end] = arr[begin];
            }
            arr[begin] = pivot; // When begin == end Jump out of loop to override pivot to index

            return begin;
        }

        public void DisPlay(int[] arr)
        {
            Console.Write("Quick sort:");
            foreach (int item in arr)
            {
                Console.Write(item + " ");
            }
            Console.WriteLine();
        }
    }
}

Posted by DusterG20 on Fri, 03 Apr 2020 04:49:37 -0700