Seven, sorting algorithm

First, the concept of sorting and classification

General definition of the sort of

 Sort is a commonly performed operation of a computer, whose purpose is to set a "disorder" is adjusted to the data element "ordered" data element.

例如 : 将下列关键字序列
52, 49, 80, 36, 14, 58, 61, 23, 97, 75
调整为
14, 23, 36, 49, 52, 58, 61 ,75, 80, 97

Sorting mathematical definition
 sequence hypotheses contain n data elements as {R1, R2, ..., Rn }, the corresponding key sequence is {K1, K2, ..., Kn }, these keywords may be compared with each other, i.e., there is such a relationship between them:
Kp1 <= Kp2 <= ... <= the Kpn
Click on the intrinsic relationship rearranged recording sequence
{R p1, R p2, ... , R pn}
of operation is referred to sort .
Sort Stability
If there are two data elements r [i] in the sequence and r [j], their key k [i] == k [j ], and before the sort, the object r [i] in row r [j]. If, after sorting, the foregoing object is r [i] is still subject r [j], the method of this sort is said to be stable.
Multi-keyword sort

排序时需要比较的关键字多余一个
排序结果首先按关键字 1 进行排序
当关键字 1 相同时按关键字 2 进行排序
……
当关键字 n-1 相同时按关键字 n 进行排序

Sort of critical operations

比较:任意两个数据元素通过比较操作确定先后次序
交换: 数据元素之间需要交换才能得到预期结果

Sort Sort inner and outer

内排序:整个排序过程不需要访问外存便能完成
外排序:待排序的数据元素数量很大 , 整个序列的排序过程不可能在内存中完成

Sort trial

时间性能:关键性能差异体现在比较和交换的数量
辅助存储空间:为完成排序操作需要的额外的存储空间;必要时可以“ 空间换时间”
算法的实现复杂性:过于复杂的排序法会影响代码的可读性和可维护性 , 也可能影响排序的性能

Second, the basic sorting algorithm

Selection sort
each pass (e.g. the i-th times, i = 0, 1, ... , n- 2) selecting the smallest element in the keyword data elements to be behind the ni row, as the ordered sequence of elements of the i-th element.
Seven, sorting algorithm
Insertion sort
when inserting section i (i> = 1) of data elements, in front of the V [0], V [1 ], ..., V [i-1] is already sorted. In this case, [i-1], V [i-2], ... keyword is compared with V [i] keywords V, to find the insertion position is about V [i] is inserted, the original position of the object on the rearward Shun shift.
Seven, sorting algorithm

Bubble sort
is provided to be the number of elements in the row sequence of data elements is n. For times up to n-1, i = 1, 2, ... ..., n-1. Run after the i-th forward from, j = n-1, n -2, ... ..., i, pairwise comparisons V [j-1] and V [j] keywords. If the reverse occurs, then the switch V [j-1] and V [j].
Seven, sorting algorithm
Hill sorting
will be sort column divided into several groups, insertion sort within each group, so that substantially the entire ordered sequence, then the entire sequence insertion sort.

将n个数据元素分为d个子序列:
{R[1],R[1+d],R[1+2d],…,R[1+kd]}
{R[2],R[2+d],R[2+2d],…,R[2+kd]}
...
{R[d],R[2+d],R[3+2d],…,R[(k+1)d]}

Wherein, d is called delta, descending gradually decreases its value during the sorting process, sort until reduced to a last trip.

Quicksort
1) according to any one of the data elements to be taken (e.g. the sorted sequence: a first element) as a reference, according to the size of the key element around the entire sequence into two sub-sequences:
 the left sequence all elements or equal to the reference element is less than
 the right sequence are greater than all the elements of the reference element
 a reference element in row two subsequences intermediate
2) of these two sequences are performed above process is repeated until all objects in the corresponding row position until.
Seven, sorting algorithm

Merge sort
the two or more ordered sequences ordered into a new sequence
V [1] ... V [m ] and V [m +1] ... V [ n] -------- -----------> V [1] ... V [n]
such as two-way merging merge method.
Seven, sorting algorithm
Summary:
Shell sort, quick sort and merge sort sorted improve the time complexity of the algorithm to O (n * logn)
Sort the results Sort Hill and quick sort is unstable
algorithm source code

Guess you like

Origin blog.51cto.com/yinsuifeng/2415507