========================================================== ==========
Function: quick sorting
Input: array name (that is, the first address of the array), subscript of the starting and ending elements in the array
========================================================== ==========
*/
/*
========================================================== ================
AlgorithmSimple Description:
Quick sorting is an essential improvement of Bubble sorting. Its basic idea is
After scanning, the length of the sorting sequence can be greatly reduced. In the Bubble sorting
Scanning can only ensure that the maximum number of values is moved to the correct position, while the length of the sequence to be sorted may only
Reduce by 1. Quick sorting can be ensured by ScanningNumber(Use it as the benchmark)
The numbers on the left are smaller than those on the right. And then process it in the same way.
The number of the left and right sides of it until there is only one element on the left and right of the benchmark. It is composed
C. A. R. Hoare proposed in 1962.
Obviously, quick sorting can be implemented using recursion, and of course stack-based recursive sorting can also be implemented. The following
Functions are implemented recursively. If you are interested, you can change them to non-recursive functions.
Fast sorting is unstable. Optimal Algorithm time complexity O (nlog2n) and Worst O (n2)
========================================================== ==================
*/
Void quick_sort (int * X, int low, int high)
{
Int I, j, T;
If (low
{
I = low;
J = high;
T = * (x + low);/* Number of temporary points */
While (I <j)/* cyclic scan */
{
While (I <J & * (x + J)> T)/* If the value on the right is larger than the value on the reference point, it is still placed on the right */
{
J --;/* move a forward position */
}
If (I <j)
{
* (X + I) = * (x + J);/* loop exit above: A number smaller than the benchmark value. Replace the benchmark value */
I ++;/* move a position behind and use this as the benchmark */
}
While (I <J & * (x + I) <= T)/* on the left, as long as the value is less than or equal to the benchmark, it is still placed on the left */
{
I ++;/* move a location behind */
}
If (I <j)
{
* (X + J) = * (x + I);/* loop exit above: A number larger than the benchmark value. Put it on the right */
J --;/* move a forward position */
}
}
* (X + I) = T;/* after scanning, place it in the appropriate position */
Quick_sort (x, low, I-1);/* sort the number on the left of the reference point quickly */
Quick_sort (x, I + 1, high);/* sort the number on the right of the benchmark */
}
}
/*
========================================================== ==========
Function: heap sorting
Input: array name (that is, the first address of the array), number of elements in the array
========================================================== ==========
*/
/*
========================================================== ================
A brief description of the algorithm concept:
Heap sorting is a kind of tree-based sorting that effectively improves the Direct selection and sorting.
The heap is defined as follows: a sequence with n elements (H1, H2,..., HN ).
Meet (HI> = h2i, HI> = 2I + 1) or (Hi <= h2i, hi <= 2I + 1) (I = 1, 2 ,..., n/2)
It is called heap. Here we only discuss the heap that meets the conditions of the former.
From the definition of heap, we can see that the heap top element (that is, the first element) must be a heap top element. Full Binary Tree
Intuitively represents the heap structure. The heap top is the root, and the others are the left and right subtree.
Initially, the sequence of numbers to be sorted is regarded as a binary tree for sequential storage, and their storage order is adjusted,
Make it a heap, and the number of root nodes in the heap is the maximum. Then, the root node and the last node of the heap
Switch. Then adjust the preceding (n-1) number to make it heap. Wait until there are only two nodes.
And exchange them to obtain the ordered sequence of N nodes.
From the algorithm description, the heap sorting requires two processes: creating a heap and the last element of the heap top and heap.
Switch location. Therefore, heap sorting consists of two functions. One is the heap build penetration function, and the other is to call the penetration function repeatedly.
The sorting function.
Heap sorting is unstable. Algorithm time complexity O (nlog2n ).
*/
/*
Function: penetration heap
Input: array name (that is, the first address of the array), number of elements involved in heap creation, starting from the first element
*/
Void sift (int * X, int N, int S)
{
Int T, K, J;
T = * (x + S);/* Temporary Start Element */
K = s;/* Start Element subscript */
J = 2 * k + 1;/* element subscript of the right subtree */
While (j <n)
{
If (j <n-1 & * (x + J) <* (x + J + 1 ))/*Judge whetherMeet the heap condition: If yes, continue the next round of comparison; otherwise, adjust. */
{
J ++;
}
If (T <* (x + J)/* adjust */
{
* (X + k) = * (x + J );
K = J;/* after adjustment, the start element is also adjusted */
J = 2 * k + 1;
}
Else/* does not need to be adjusted. It is already a heap and exits the loop. */
{
Break;
}
}
* (X + k) = T;/* place the Start Element in the correct position */
}
/*
Function: heap sorting
Input: array name (that is, the first address of the array), number of elements in the array
*/
Void heap_sort (int * X, int N)
{
Int I, K, T;
Int * P;
For (I = n/2-1; I> = 0; I --)
{
Sift (x, N, I);/* Initial heap creation */
}
For (k = n-1; k> = 1; k --)
{
T = * (x + 0);/* place the heap top to the end */
* (X + 0) = * (x + k );
* (X + k) = T;
Sift (x, K, 0);/* Create a heap after the remaining number */
}
}
Void main ()
{
# Define max 4
Int * P, I, a [Max];
/* Enter Test Data */
P =;
Printf ("input % d number for sorting: \ n", max );
For (I = 0; I <Max; I ++)
{
Scanf ("% d", P ++ );
}
Printf ("\ n ");
/* Select sorting for test */
P =;
Select_sort (p, max );
/**/
/* Insert sorting directly in the test */
/*
P =;
Insert_sort (p, max );
*/
/* Test the Bubble Sorting */
/*
P =;
Insert_sort (p, max );
*/
/* Test quick sorting */
/*
P =;
Quick_sort (p, 0, MAX-1 );
*/
/* Test heap sorting */
/*
P =;
Heap_sort (p, max );
*/
For (P = A, I = 0; I <Max; I ++)
{
Printf ("% d", * P ++ );
}
Printf ("\ n ");
System ("pause ");
}
========================================================== ====