C Array Sorting - Quick Sorting - C Zero Basic Tutorial

Catalog

Zero Foundation C/C++ Learning Route Recommendation: C/C++ Learning Directory >> Introduction to C Language Foundation

1. brief introduction

After the previous studies, we have learned Array traversal In development, we often encounter sorting arrays, such as: sorting academic achievements, height, age, and so on; In C Language There are four common sorts of arrays:

2. Array Quick Sort Principle

Quick Sort is one of the best algorithms at present. The idea to implement this algorithm is to add array The sorting problem is considered to be the sorting problem of two decimal arrays, and each small array can continue to be considered two smaller arrays, recursively until the maximum array length size is 2.

3. Array Quick Sort Actual

/******************************************************************************************/
//@Author: Ape says programming
//@Blog (personal blog address): www.codersrc.com
//@File:C Language Tutorial - C Language Array Sorting - Quick Sorting
//@Time:2021/06/08 08:00
//@Motto: Do not stumble to thousands of miles, do not accumulate small streams into rivers and seas, the wonderful needs of programmed life need to be accumulated relentlessly!
/******************************************************************************************/

#include<stdlib.h>
#include<stdio.h>
//Quick Sort
void quickSort(int *arr, int l, int r)
{
    //Write code here for quick sorting
    int i, j, x, temp;
    if (l < r)
    {
        i = l;
        j = r;
        x = arr[(l + r) / 2];
        //Axis with intermediate elements
        while (1)
        {
            while (i <= r && arr[i] < x)
                i++;
            while (j >= 0 && arr[j] > x)
                j--;
            if (i >= j) //Jump out when you meet
                break;
            else
            {
                temp = arr[i];
                arr[i] = arr[j];
                arr[j] = temp;
                //exchange
            }
        }
        quickSort(arr, l, i - 1); //Quick Row on Left Half
        quickSort(arr, j + 1, r); //Quick Row on Right Half
    }
}
void main()
{
    int a[] = { 9, 8, 7, 6, 5, 4, 3, 2, 1, 10 };
    int len = sizeof(a) / sizeof(int);
    for (int i = 0; i < len; i++)
    {
        printf("Before sorting: index:%d  value:%d\n", i, a[i]);
    }
    //Quick Sort
    quickSort(a, 0, 9);
    printf("-----------------------------------\n");
    for (int i = 0;i<len;i++)
    {
        printf("After sorting: index:%d  value:%d\n", i, a[i]);
    }
    system("pause");
}

/*
Output:

Before sorting: index:0 value:9
 Before sorting: index:1 value:8
 Before sorting: index:2 value:7
 Before sorting: index:3 value:6
 Before sorting: index:4 value:5
 Before sorting: index:5 value:4
 Before sorting: index:6 value:3
 Before sorting: index:7 value:2
 Before sorting: index:8 value:1
 Before sorting: index:9 value:10
-----------------------------------
After sorting: index:0 value:1
 After sorting: index:1 value:2
 After sorting: index:2 value:3
 After sorting: index:3 value:4
 After sorting: index:4 value:5
 After sorting: index:5 value:6
 After sorting: index:6 value:7
 After sorting: index:7 value:8
 After sorting: index:8 value:9
 After sorting: index:9 value:10
 Press any key to continue...
*/

4. Guess you like it

  1. Install Visual Studio
  2. Install Visual Studio Plugin Visual Assist
  3. Visual Studio 2008 Uninstall
  4. Visual Studio 2003/2015 Uninstall
  5. C Language Format Controller/Placeholder
  6. C Language Logical Operators
  7. C Trinomial Operator
  8. C Language Comma Expression
  9. C Language for Loop
  10. C Language while Loop
  11. C Language do while and while loops
  12. C language switch statement
  13. C language goto statement
  14. C Language char String
  15. Differences between C sizeof and strlen functions
  16. C language strcpy and strcpy_s-function difference
  17. C language memcpy and memcpy_s Difference
  18. C Language Array Definition and Use
  19. C Language Array Traversal
  20. C Language Array Sorting - Bubble Sorting
  21. C Language Array Sorting - Selection Sorting
  22. C Language Array Sort - Insert Sort
  23. C Language Array Sorting - Quick Sorting

No reprinting without permission: Ape-talk programming » C Language Array Sorting - Quick Sorting

Added by nadeauz on Wed, 22 Dec 2021 04:49:05 +0200