Algorithms Stability:
Stability algorithms - assuming the presence of a [i] = a [j] in the series, if the prior sorting, a [i] in a [j] the foregoing; and after sorting, a [i] is still in a [j] front. Then the sorting algorithm is stable!
Insertion sort:
Direct insertion sort:
Concept:
the record into a table has been sorted good sequence to obtain a new ordered list. (Time complexity is O (n ^ 2), the spatial complexity is O (1))
corresponding to a respective sequential search.
Examples:
{4,938,659,776,132,749}
#include <stdio.h>
#include <stdlib.h>
void InsertSort(int *a, int length)
{
int i , j;
for(i = 1;i <= length; i++)
{
if(a[i] < a[i - 1])
{
int temp = a[i];
for(j = i - 1; j >= 0 && a[j] > temp; j--)
{
a[j + 1] = a[j];
}
a[j+1] = temp;
}
}
for(i = 0; i <= length; i++)
printf("%d ", a[i]);
}
int main(void)
{
int i;
int a[] = {49, 38, 65, 97, 76, 13, 27, 49};
int k = sizeof(a) / sizeof(a[0]);
InsertSort(a, k - 1);
return 0;
}
Binary insertion sort:
Corresponds to binary search or binary search
concept: improved insertion sort.
Baidu Encyclopedia: https://baike.baidu.com/item/%E6%8A%98%E5%8D%8A%E6%8F%92%E5%85%A5%E6%8E%92%E5%BA% 8F / 8208853
first element disposed in the process of a new element is inserted are sorted array in the search for the insertion point, to be inserted into the region is a [low], the end of the element is set to a [high], the wheel comparison when to be inserted into the element and a [m], where m = (low + high) / 2 is compared, if smaller than the reference element, selecting a [low] to a [m-1] a new insertion region (i.e. high = m-1), or select a [m + 1] to a [high] a new insertion region (i.e., low = m + 1), and so on until low <= high is not established, i.e. after this position all the elements move one, and the new element is inserted a [high + 1]. (Time complexity and space complexity and inserted into the same sort)
Code:
https://www.cnblogs.com/tensory/p/6590799.html
void InsertSort2(int *a, int length)
{
int i , j, low, high, mid;
for(i = 1; i < length; i++)
{
low = 0;
high = i - 1;
while(low <= high)
{
mid = (low + high) / 2;
if(a[mid] < a[i])
{
low = mid + 1;
}else
{
high = mid - 1;
}
}
int temp = a[i];
for(j = i; j > low; j--)
a[j] = a[j - 1];
a[low] = temp;
}
for(i = 0; i <= length; i++)
printf("%d ", a[i]);
}
Hill Sort:
Concept:
Shell sort is the subject of pressing the recording packet in increments, the use of direct insertion sort each sorting algorithm; With increment is gradually reduced, more and more keywords each comprising, when the increment is reduced to 1 , the entire file is divided into just one set, the algorithm will terminate.
Code:
https://www.cnblogs.com/chengxiao/p/6104371.html
Exchange Sort:
Bubble Sort:
Quick Sort:
Select Sort:
Simply choose Sort:
Tree selection sort:
HEAPSORT:
Merge sort:
Radix Sort:
Multi-keyword Sort:
Chain Radix Sort:
External sorting:
And more balanced merge sort:
Replacement - Select Sort:
Best merge number:
The concept:
Example:
Bubble Sort:
The concept:
Example:
Quick Sort:
The concept:
Example:
Insertion sort:
The concept:
Example: