C language-sort-quick sort

Source: Internet
Author: User

A quick sort is an improvement to the bubbling sort. Its basic idea is: by a trip to sort the data to be sorted into two separate parts, one part of all the data is smaller than the other part of all the data, and then the second method of the two parts of the data are quickly sorted, the entire sorting process can be recursive, so as to achieve the entire data into an ordered sequence.

Suppose the array to be sorted is a[1] ... A[n], the first arbitrary selection of data (usually the first data) as the key data, and then put all the more than its number in front of it, all the larger than its number is placed behind it, this process is called a fast sort of a trip. A quick sort of algorithm is:

1) Set two variables I, J, when the sorting starts i=0,j=n-1;

2) with the first array element as the key data, assign the value to X, i.e. x=a[0];

3) Forward search from J, that is, after the start of the search, find the first value less than X, the two exchange;

4) from I start backward search, that is, from the front to start backward search, find the first value greater than X, the two exchange;

5) Repeat the 3rd and 4 steps until i=j;

---------------------Reference Code-------------------------

void Quiksort (int a[],int low,int high) {    int i = low;    int j = high;      int temp = A[i];       if (Low < high)    {when                  (i < J)         {while (            (a[j] >= temp) && (i < J))            {                 j--;
   
    }            A[i] = a[j];            while ((A[i] <= temp) && (i < J))            {                i++;             }              a[j]= a[i];        }        A[i] = temp;        Quiksort (a,low,i-1);        Quiksort (A,j+1,high);    }    else    {        return;    }} int main () {    int arry[5] = {23,1,21,4,19};    Quiksort (arry,0,4);    for (i=0;i<5;i++)    {        printf ("%d", Arr[i]);    }    printf ("\ n");}
   

  

C language-sort-quick sort

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.