c语言经典排序算法(8种-含源代码)(国外英文资料)
c语言经典排序算法(8种-含源代码)(国外英文资料)
A man is better than a man
Common classical sorting algorithm
1. Hill sorting
dichotomy
Direct insertion
Take the sentinel's direct collation
Bubble sort 5.
6. Select the sorting
7. Quick sort
8. Heap sort
A.hill (or, in the case of a small, small, incremental sort, which was brought up by D.L.S hell in 1959)
/ * Shell sort method * /
# include < stdio, h >
Void sort (int v [], int n)
{
Int gap, I, j, temp;
For (gap = n / 2; gap > 0; gap / = 2) / * set the sequence of steps for the length of the steps in half, until the gap is reduced to 1 * /
{
For (I = gap; I < n; I + +) / * to each element * /
{
For (j = I - gap; (j > = 0) && (v + gap [j] [j] > v); j - = gap) / * compare the size of the gap is far away from the two elements, according to the sort direction to decide how to switch * /
{
Temp = v [j];
V [j] = v [j + gap];
V + gap [j] = temp;
}
}
}
}
Binary interpolation
/ * binary insert method * /
Void HalfInsertSort (int a [], int len)
{
Int I, j, temp.
Int low, high, mid;
"For" (I = 1; I < len; I + +)
{
Temp = a [I]; / * save the pre-element * /
Low = 0;
High = I - 1;
While (low < = high) / * in a [low... high], find the location of an ordered insert * /
{
So, mid is low plus high over 2; / * find the middle element * /
If a [mid] > temp / * if the intermediate element is larger than the previous element, the current element is inserted into the left side of the intermediate element
{
High = mid - 1;
}
Else if the intermediate element is smaller than the current element, the previous element is inserted into the right side of the intermediate element
{
Low = mid + 1;
}
} / * find the location of the current element, between low and high * /
For (j = I - 1; j > high; j -) / *
{
A [j + 1] = a [j];
}
A [high + 1] = temp; / * insert * /
}
}
Three. Direct insertion
/ * directly insert method * /
So, void InsertionSort (int input, int len)
{
Int I, j, temp.
"For" (I = 1; I < len; I + +)
{
Temp = input [I]; / * manipulate the current element,