C language code implementation for fast sorting

Source: Internet
Author: User

Author: Li Huiqin,Hua Qing vision embedded college lecturer.

In essence, quick sorting is an improvement of "Bubble Sorting". The entire sorting process can be summarized as follows: the original sorting data is divided into several parts for segmented sorting through the n-hop sorting, in each sort process, the data to be sorted is divided into two parts by the specified Keyword: the larger part and the smaller part than the keyword, splits the entire to-be-arranged series into several small series and sorts them separately. Assume that we are sorting the number of columns quickly. The C language code is as follows:

# Include <stdio. h>
Int partition (int * data, int low, int high)
{Int T = 0;
T = data [low];
While (low
{While (low <High & Data [High]> = T)
High --;
Data [low] = data [High];
While (low <High & Data [low] <= T)
Low ++;
Data [High] = data [low];
}
Data [low] = T;
Return low;
}

Void sort (int * data, int low, int high) // re-determine the pivot during each trip in the fast sorting, determine the low and high values of each small record
{If (low> = high)
Return;
Int optional tloc = 0;
Required tloc = partition (data, low, high );
Sort (data, low, pivotloc-1 );
Sort (data, repeated tloc + 1, high );
}
Void quick_sort (int * data, int N) // calls the sort process.
{Sort (data, 0, n-1 );}

Int main ()
{Int I;
Int data [] = };
Quick_sort (data, sizeof (data)/sizeof (INT ));
For (I = 0; I <sizeof (data)/sizeof (INT); I ++)
Printf ("% d", data [I]);
Printf ("/N ");
Return 0;
}

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.