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
- Install Visual Studio
- Install Visual Studio Plugin Visual Assist
- Visual Studio 2008 Uninstall
- Visual Studio 2003/2015 Uninstall
- C Language Format Controller/Placeholder
- C Language Logical Operators
- C Trinomial Operator
- C Language Comma Expression
- C Language for Loop
- C Language while Loop
- C Language do while and while loops
- C language switch statement
- C language goto statement
- C Language char String
- Differences between C sizeof and strlen functions
- C language strcpy and strcpy_s-function difference
- C language memcpy and memcpy_s Difference
- C Language Array Definition and Use
- C Language Array Traversal
- C Language Array Sorting - Bubble Sorting
- C Language Array Sorting - Selection Sorting
- C Language Array Sort - Insert Sort
- C Language Array Sorting - Quick Sorting
No reprinting without permission: Ape-talk programming » C Language Array Sorting - Quick Sorting