, Stable and non-stable sorting
Simply put, After all equal numbers are sorted by some sort method, they can still maintain their relative order before sorting.
This sorting method is stable. On the contrary, it is unstable.
For example, A1, A2, A3, A4, and A5 are before a group of numbers. A2=A4, Which is A1, A2, A4, A3, A5,
We can say that this sort is stable, because A2 is before A4, and it is still before A4. For example, A1, A4,
A2, A3, and A5 are not stable.
2, Internal sorting and external sorting
In the sorting process, all the numbers to be sorted are in the memory and their storage order is adjusted in the memory;
In the sorting process, only part of the number is transferred to the memory, and the memory adjustment is used to store the number in the external storage. The sorting method is called external sorting.
3,AlgorithmTime Complexity and space complexity
The time complexity of an algorithm refers to the computing workload required to execute an algorithm.
The space complexity of an algorithm generally refers to the memory space required to execute this algorithm.
========================================================== ================
*/
/*
========================================================== ==========
Function: Select 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:
In the number of groups to be sorted, the minimum number is selected to exchange with the number at the first position;
Then find the smallest number in the remaining number and exchange it with the number in the second position.
Until the second to last number is compared with the last number.
the selected sorting is unstable. Algorithm complexity O (n2) -- [the square of N]
====================================== ======================================< br> */
void select_sort ( int * X, int N)
{< br> int I, j, Min, T;
for (I = 0 ; I = 2 ] The number is already in the order
. Now we need to insert the nth number into the previous order number, so that the N number
is also in the order. This repeats until all the rows are sorted.
Direct insertion and sorting are stable. Algorithm time complexity O (n2)--[N square]
========================================================== ==================
*/
VoidInsert_sort (Int *X,IntN)
{
IntI, j, T;
For (I = 1 ; I = 0 && T0; H = K) /* Loop To no comparison range */
{
For (J = 0 , K = 0 ; J * (X + J + 1 )) /* Put the big one behind and the small one at the front. */
{
T = * (X + J );
* (X + J) = * (X + J + 1 );
* (X + J + 1 ) = T; /* Complete exchange */
K = J; /* Save the position of the last sink. In this way, K is sorted. */
}
}
}
}
/*
========================================================== ==========
Function: Hill 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:
In the direct insertion sorting algorithm, insert a number at a time to add only one node to the sequence,
This does not help insert the next number. If the comparison is relatively long distance (called
Incremental), so that when the number moves across multiple elements, a comparison may be eliminated.
Multiple elements are exchanged. D. L. Shell was implemented in the sorting algorithm named by him in 1959.
This idea. The number of groups to be sorted by the algorithm is divided into several groups according to an incremental D.
The subscript of the record is different. D. Sort all the elements in each group, and then use a small increment.
Sort it and then sort it in each group. When the increment is reduced to 1, the entire number to be sorted is divided
A group. The sorting is complete.
The following function is an implementation of a hill sorting algorithm. The first half of the sequence is incremental,
It will be halved each time until the increment is 1.
Hill sorting is unstable.
========================================================== ==================
*/
VoidShell_sort (Int *X,IntN)
{
IntH, J, K, T;
For (H = N / 2 ; H > 0 ; H = H / 2 ) /* Control Increment */
{
For (J = H; j = 0 && TT) /* If the value on the right is larger than the benchmark value, it is placed on the right. */
{
J -- ; /* Move to one location */
}
If(I=H2i, hi> =2I+1) Or (hi=0; I--)
{
Sift (x, N, I );/*Initial heap Creation*/
}
For (K = N - 1 ; K > = 1 ; K -- )
{
T = * (X + 0 ); /* Stack top to the end */
* (X + 0 ) = * (X + K );
* (X + K) = T;
Sift (x, K, 0 ); /* Build the heap with the remaining number */
}
}
VoidMain ()
{
# DefineMax 4
Int *P, I, a [Max];
/* Input Test Data */
P = A;
Printf ( " Input % d number for sorting: \ n " , Max );
For (I = 0 ; I < Max; I ++ )
{
Scanf ( " % D " , P ++ );
}
Printf ( " \ N " );
/*Test selection sorting*/
P=A;
Select_sort (p, max );
/**/
/*Test direct insertion sorting*/
/*
P =;
Insert_sort (p, max );
*/
/*Test 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 = , I = 0 ; I MAX; I ++ )
{< br> printf ( " % d " , * P ++ );
}
printf ( " \ n " );
System ( " pause " );
}