Linux C Quick Sort method

Source: Internet
Author: User

#include <stdio.h>//Array Length # define Length ((sizeof (array))/(sizeof (ARRAY[0)))/* * Quick Sort * * Parameter description: * A-  -the array to be sorted * L--the left edge of the array (for example, starting from the start position, then l=0) * R--The right edge of the array (for example, sort up to the end of the array, then R=a.length-1) */void quick_sort (int a[], int        l, int R) {if (L < r) {int i,j,x;        i = l;        j = r;        x = A[i];            while (I < J) {while (I < J && A[j] > x) j--;//Right-to-left find the first number less than X            if (I < j) {//a[i++] = a[j];a[i] = a[j];i++;} while (I < J && A[i] < x) i++;        From left to right, find the first number greater than X if (I < j) {A[j] = a[i];j--;}        } A[i] = x; Quick_sort (A, L, i-1); /* Recursive call */Quick_sort (A, i+1, R);    /* Recursive call */}}void main () {int i;    int a[] = {30,40,60,10,20,50};    int ilen = LENGTH (a);    printf ("before sort:");    for (i=0; i<ilen; i++) printf ("%d", a[i]);    printf ("\ n"); Quick_sort (A, 0, Ilen-1);    printf ("After sort:");    for (i=0; i<ilen; i++) printf ("%d", a[i]); printf ("\ n");}

 

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.