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;
}