Hill sort, heap sort, merge sort

Disclaimer: This article is a blogger original article, follow the CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source link and this statement.
This link: https://blog.csdn.net/weixin_kab777/article/details/102736029

Shell sort

Thought:
(1) the direct insertion sort of thinking, as if to make an orderly array, the time complexity can make approaches O (n). Therefore, the algorithm is also concentrated in the orderly array. First, a method of grouping a packet array, for example array 11 is assumed here that the number, we can be divided into three groups of 1, then a 1 is set.
(2) to a given width of a packet array, the outer loop can be directly from the start i = 0 + width, and with the current value stored tmp arr [i], and then backwards until i ++ been traversed array; inner loop j o a need to start backwards from the first outer loop current in the left group, each pass width length, i.e. j- = width, and each requires care tmp, tmp is less than if arr [J], then arr [ j] assigned arr [j + width], j and backwards; otherwise jump directly cause this inner loop, the inner loop ends after the value assigned tmp arr [j + width]. Then enter a next outer loop.
Time complexity: O (n ^ 1.3 ~ n ^ 1.5)
Complexity Space: O (1)
Stability: Unstable

Heapsort

Thought:
(1) the analog array divided into a complete binary tree, then we can learn the characteristics of complete binary tree: about any known parent or a child node can find the remaining two. For example: a known parent node n, the left child of 2 n-+. 1, the right child node is 2 n-2 +; known left or right child nodes is n, the parent node (n-1) / 2.
(2) heap sort, heap must first maximum configuration, starting from the root of the current leaf node, so that not less than child nodes parent node. Tmp with the first start position stored value ARR [start], the outer loop beginning at start i, I- backwards until i = 0; j from the current inner loop is left child start of each cycle next to the current the left child node j, and j to limit the length of the array in -1. Entering the inner loop, first determines whether the value is greater than the left child and right child nodes, so if less than the value of j j ++ secure a large current value is stored in the child node. Tmp then compared with the current value of arr [j], if tmp> arr [j], is the current out of the inner loop, or arr [start] = arr [j ] and let the end of the start = j, the inner loop condition after , so ARR [start] = tmp;
(. 3) constructed on the basis of the maximum stack, starting from 0, the need to step through a loop len-1 times, the exchange first with the last parent node of the current tree tree, then make another adjustment, that is to call the building the largest heap of the inner loop.
Time complexity: O (nlogn)
Complexity Space: O (1)
Stability: Unstable

Merge sort

Thought :( 1) by a length of a cyclic manner by the packet 2 progressively smaller value found in the group assigned to another array, each cycle can be ordered within each group to ensure, so the next cycle group is equivalent to the number of groups in the right half of the left and the assignment group ordered to the other along the array, then pass the current array of another array.
(2) Group achieved: two cases
the first: the presence of the right half of the group; i.e. is low2 <len;
Analyzing: Only low1 <= high1 && low2 <Comparative cycle arr [low1] = high2 case with arr [high1 ], a small value is assigned to brr.
After not meet the conditions, is determined right or left group, the remaining groups, less low determination condition is high, then the remaining number assigned to brr.

The second: Only the left half of the group, that is low1 <len. Directly to the number in the group assigned a brr.

Time complexity: O (nlogn)
Complexity Space: O (n)
Stability: Unstable

Guess you like

Origin blog.csdn.net/weixin_kab777/article/details/102736029