Quick sort algorithm in C and C# (Ascending and Descending order)

Lionsure 2020-07-05 Original by the website

The less the sort algorithm compares, the lower the complexity and the faster the speed. The quick sort algorithm is an algorithm that minimizes the number of comparisons as much as possible. Its efficiency ranks among the top of commonly used sort algorithms and is widely used in various sorts. Let's start with its basic idea and gradually understand it.

 

1. The basic idea of quick sort algorithm

Quick sort adopts the idea of smelting, dividing the array(or List) to be sorted into several parts, the first comparison divides the array into two parts, and the second time uses recursion into four parts, until the sort is completed.

 

2. Quick sort algorithm in C# (Ascending order)

/// <summary>
       /// Quick sort
       /// </summary>
       /// <param name="arr">Array to be sorted</param>
       /// <param name="left">Array left index</param>
       /// <param name="right">Array right index</param>

       public void QuickSort(int[] arr, int left, int right)
       {
              int temp;

       //If the left index is less than the right, the sort is not yet complete
              if (left < right)
              {
                     //Take the element in the middle as a reference for comparison, move to the left less than it, move to the right greater than it
                     int middle = arr[(left + right) / 2];
                     int i = left - 1;
                     int j = right + 1;

              while (true)
                     {
                            while (arr[++i] < middle && i < right) ;//Find elements smaller than the middle value from the left
                            while (arr[--j] > middle && j > 0) ;//Find elements larger than the middle value from the right

                     if (i >= j)
                                   break;

                     temp = arr[i];
                            arr[i] = arr[j];
                            arr[j] = temp;
                     }
                     QuickSort(arr, left, i - 1);
                     QuickSort(arr, j + 1, right);
              }
       }

Call:

int[] a = new int[] { 21, 5, 57, 105, 66, 15, 163, 98 };
       QuickSort(a, 0, 7);

foreach (int n in a)
       label.Text += n.ToString() + " ";

Output result: 5 15 21 57 66 98 105 163

 

 

3. Quick sort algorithm in C# (Descending order)

Descending order is basically the same as ascending order, except that the elements move in the opposite direction, just change the first < in the "while (arr[++i] < middle && i < right)" to >, change the first > in the "while (arr[--j] > middle && j > 0)" to <, the code is as follows:

/// <summary>
       /// Quick sort(Descending order)
       /// </summary>
       /// <param name="arr">Array to be sorted</param>
       /// <param name="left">Array left index</param>
       /// <param name="right">Array right index</param>

       public void QuickSort(int[] arr, int left, int right)
       {
              int temp;

       //If the left index is less than the right, the sort is not yet complete
              if (left < right)
              {
                     //Take the element in the middle as a reference, move to the right less than it, move to the left greater than it
                     int middle = arr[(left + right) / 2];
                     int i = left - 1;
                     int j = right + 1;

              while (true)
                     {
                            while (arr[++i] > middle && i < right) ;//Find elements smaller than the middle value from the left
                            while (arr[--j] < middle && j > 0) ;//Find elements larger than the middle value from the right

                     if (i >= j)
                                   break;

                     temp = arr[i];
                            arr[i] = arr[j];
                            arr[j] = temp;
                     }
                     QuickSort(arr, left, i - 1);
                     QuickSort(arr, j + 1, right);
              }
       }

Call:

int[] a = new int[] { 21, 5, 57, 105, 66, 15, 163, 98 };
       QuickSort(a, 0, 7);

foreach (int n in a)
       label.Text += n.ToString() + " ";

Output result: 163 105 98 66 57 21 15 5

 

 

4. Quick sort algorithm in C (Ascending order)

#include <stdio.h>
       #include <stdlib.h>

void QuickSort(int a[], int left, int right)
       {
              int i = left;
              int j = right; 
              int temp = a[i];

       if( left < right)
              {
                     while(i < j)
                     {
                            while(a[j] >= temp && i < j)
                            {
                                   j--;
                            }
                            a[i] = a[j];

                     while(a[i] <= temp && i < j)
                            {
                                   i++;
                            }
                            a[j] = a[i];
                     }
                     a[i] = temp;

              QuickSort(a, left, i - 1);
                     QuickSort(a, j + 1, right);
              }
       }

void main ()
       {
              int a[8] = {21, 5, 57, 105, 66, 15, 163, 98};
              QuickSort(a, 0, 7);

       int i;
              for (i = 0; i < 8; i++)
                     printf("%d,",a[i]);
              printf("\n");
       }

Output result: 5,15,21,57,66,98,105,163

 

 

5. Quick sort algorithm in C (Descending order)

#include <stdio.h>
       #include <stdlib.h>

void QuickSort(int a[], int left, int right)
       {
              int i = left;
              int j = right;
              int temp = a[i];

       if( left < right)
              {
                     while(i < j)
                     {
                            while(a[j] <= temp && i < j)
                            {
                                   j--;
                            }
                            a[i] = a[j];

                     while(a[i] >= temp && i < j)
                            {
                                   i++;
                            }
                            a[j] = a[i];
                     }
                     a[i] = temp;

              QuickSort(a, left, i - 1);
                     QuickSort(a, j + 1, right);
              }
       }

void main()
       {
              int a[8] = {21, 5, 57, 105, 66, 15, 163, 98};
              QuickSort(a, 0, 7);

       int i;
              for (i = 0; i < 8; i++)
                     printf("%d,",a[i]);
              printf("\n");
       }

Output result: 163,105,98,66,57,21,15,5