Quick Sorting Algorithm (C code attached)

Source: Internet
Author: User

Because there is an exercise in The book <The C Programming Language> that requires fast sorting, I have reviewed it and shared it with others.

The core of quick sorting is a divide and conquer algorithm. Unfortunately, the Chinese books we have come into contact with highlight how to do a quick sort without focusing on this programming idea. For the details of a single sort, there are many implementation versions, each of which is under different considerations. The fast sorting we learned in the blue PI data structure is an in-place quick sort. This version optimizes the space complexity so that the implementation does not require additional space and the space complexity is O (1 ). However, in terms of details, I think there is no need to set two variables I and j, and then compare them from the two ends, because it is the same as setting a variable, both require O (n) comparison. So I made some changes.

Code:


[Cpp]
Void qsort (char * listptr [], int left, int right)
{
Int Limit = 0;
 
// First, partition operation
Partition = partition (lineptr, left, right );
 
If (left <ignore)
// Recursively quick sort the former part if its length greater than 1
Qsort (lineptr, left, ignore );
// Recursively quick sort the latter part if its length greater than 1
If (distinct + 1) <right)
Qsort (lineptr, latency + 1, right );
}
 
Int partition (char * listptr [], int left, int right)
{
Int rows = left, I = left + 1;
 
For (; I <right; I ++)
{
If (listptr [I] [0] <listptr [distinct] [0]) {
Swap (& listptr [I], & listptr [callback]);
Token = I;
}
}
 
Return response;
}

Void qsort (char * listptr [], int left, int right)
{
Int Limit = 0;

// First, partition operation
Partition = partition (lineptr, left, right );

If (left <ignore)
// Recursively quick sort the former part if its length greater than 1
Qsort (lineptr, left, ignore );
// Recursively quick sort the latter part if its length greater than 1
If (distinct + 1) <right)
Qsort (lineptr, latency + 1, right );
}

Int partition (char * listptr [], int left, int right)
{
Int rows = left, I = left + 1;

For (; I <right; I ++)
{
If (listptr [I] [0] <listptr [distinct] [0]) {
Swap (& listptr [I], & listptr [callback]);
Token = I;
}
}

Return response;
}

 

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.