C language: Sort an array using a Quick Sort Algorithm

Source: Internet
Author: User
# 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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.