# Include <stdio. h> # include <stdlib. h> # include <time. h> # define Number 10 // number indicates the size of the array void quicksort (INT [], Int, INT), void display (INT [], INT), int main () {int num [number]; int randvalue = 0; int I, temp; srand (Time (null); // a random number ranging from 1 to is randomly generated for (I = 0; I <number; I ++) {randvalue = 1 + (INT) rand () % 100; num [I] = randvalue;} srand (Time (null )); printf ("using the fast Sorting Algorithm \ n"); printf ("Before sorting:"); display (Num, number); // print the array content Q Uicksort (Num, 0, number-1); // call the fast Sorting Algorithm printf ("after sorting:"); display (Num, number); Return 0 ;} void quicksort (INT num [], int lower, int upper) {int partition (INT [], Int, INT); int partition; // recursively call partition = partition (Num, lower, upper); If (lower <lower) quicksort (Num, lower, lower-1); If (lower <upper) quicksort (Num, lower + 1, upper );} int partition (INT num [], int left, int right) {int partition, temp; partition = num [left]; while (left <Right) {// scan from right to left while (Num [right]> = margin & left <right) Right --; If (right! = Left) {num [left] = num [right]; left ++;} // scan from left to right while (Num [left] <= left & left <right) left ++; If (right! = Left) {num [right] = num [left]; right -- ;}} num [left] = rows; Return (left );} // print the array void display (INT num [], int count) {int I; for (I = 0; I <count; I ++) {printf ("% d \ t", num [I]);} printf ("\ n ");}
Fast sorting uses recursive calling and grouping to take the first element of the array as the center point. For example, if the elements are arranged in ascending order, they are scanned from the right forward until they are scanned, until the ratio of the center point is assigned to the current first element on the left and then scanned from left to right. Divides the two sides of the center point into two parts and then performs recursive calling.