[Disclaimer: All Rights Reserved. You are welcome to reprint it. Do not use it for commercial purposes. Contact Email: feixiaoxing @ 163.com]
Quick sorting is a sort method that is often used in programming. However, many friends are afraid of fast sorting. They think that recursive sorting is a very complicated program. In fact, this is not necessarily the case. As long as we use the method, we can achieve quick sorting by ourselves.
First, let's review the basic steps of quick sorting:
1. determine the validity of input parameters
2. Use the first data in the array as the origin for comparison. Data smaller than the data is arranged on the left, and data larger than the data is arranged on the right.
3. Sort the array on the left and data on the right in the same way as (2) According to the (2) method.
What should I do in actual code writing?
A) First, determine the validity of the data?
Void quick_sort (int array [], int length)
{
Int median = 0;
If (NULL = array | 0 = length)
Return;
_ Quick_sort (array, 0, length-1 );
}
Void quick_sort (int array [], int length)
{
Int median = 0;
If (NULL = array | 0 = length)
Return;
_ Quick_sort (array, 0, length-1 );
}
B) Search for the intermediate number and sort the data on the left and right respectively.
Void _ quick_sort (int array [], int start, int end)
{
Int middle;
If (start> = end)
Return;
Middle = get_middle (array, start, end );
_ Quick_sort (array, start, middle-1 );
_ Quick_sort (array, middle + 1, end );
}
Void quick_sort (int array [], int length)
{
Int median = 0;
If (NULL = array | 0 = length)
Return;
_ Quick_sort (array, 0, length-1 );
}
Void _ quick_sort (int array [], int start, int end)
{
Int middle;
If (start> = end)
Return;
Middle = get_middle (array, start, end );
_ Quick_sort (array, start, middle-1 );
_ Quick_sort (array, middle + 1, end );
}
Void quick_sort (int array [], int length)
{
Int median = 0;
If (NULL = array | 0 = length)
Return;
_ Quick_sort (array, 0, length-1 );
} C) How should we arrange the intermediate number here?
Int get_middle (int array [], int start, int end)
{
Int front = 0;
Int tail = end-start;
Int value = array [start];
Int length = end-start + 1;
Int loop = start + 1;
While (loop <= end ){
If (array [loop] <value ){
GQuickSort [front] = array [loop];
Front ++;
} Else {
GQuickSort [tail] = array [loop];
Tail --;
}
Loop ++;
}
GQuickSort [front] = value;
Memmove (& array [start], gQuickSort, sizeof (int) * (length ));
Return start + front;
}
Int get_middle (int array [], int start, int end)
{
Int front = 0;
Int tail = end-start;
Int value = array [start];
Int length = end-start + 1;
Int loop = start + 1;
While (loop <= end ){
If (array [loop] <value ){
GQuickSort [front] = array [loop];
Front ++;
} Else {
GQuickSort [tail] = array [loop];
Tail --;
}
Loop ++;
}
GQuickSort [front] = value;
Memmove (& array [start], gQuickSort, sizeof (int) * (length ));
Return start + front;
}
Note: gQuickSort is a global array mainly used as a temporary array for sorting. You can use various methods flexibly in the actual environment.
D) The basic quick sorting is complete. How can we test it? How many simple test cases can we compile?
Static void test1 ()
{
Int array [] = {1 };
Quick_sort (array, sizeof (array)/sizeof (int ));
}
Static void test2 ()
{
Int array [] = {2, 1 };
Quick_sort (array, sizeof (array)/sizeof (int ));
Assert (1 = array [0]);
Assert (2 = array [1]);
}
Static void test3 ()
{
Int array [] = {4, 3, 2, 1 };
Quick_sort (array, sizeof (array)/sizeof (int ));
Assert (1 = array [0]);
Assert (2 = array [1]);
Assert (3 = array [2]);
Assert (4 = array [3]);
}
Static void test4 ()
{
Int array [] = {3, 2, 1 };
Quick_sort (array, sizeof (array)/sizeof (int ));
Assert (1 = array [0]);
Assert (2 = array [1]);
Assert (3 = array [2]);
}