First, the quick sort
I have learned to sort
Divide and rule
Pivot Point
pivot:
Quick Sort
Bad news: in the original sequence, the pivot point there is not necessarily ...
Prerequisites: // pivot points must be already in place although not vice versa
derangement: 2 3 4... n 1
In particular: in the ordered sequence, all the elements are all pivot points; or vice versa
Quick sort is all the elements one by one into an axis point of the process
Question: How to exchange? How high the cost?
Configuration pivot point
Select the pivot point as a candidate training object, the first element typically queued m
Configuration used in the process and lo hi, the sequence is divided into three portions L, U, G
L is a prefix of any element not exceed a candidate axis points, G is a suffix, any element not less than a pivot point candidate
U is the sequence between the two, an element composed of an unknown size
The initial state of the entire sequence is U, L and G are empty
After startup, alternating attempts to hi to lo measured and moved, so that they are close to each other
lo with every step, a L unit to expand, Hi and G is, in this process, U is added to the elements of L or G,
Finally, Hi and lo refer to the same location, the pivot points previously selected candidates placed there, a real pivot point
Monotonicity + invariance
Initial situation, L and G are empty, pivot less than or equal to L G, satisfy NATURAL
U header elements have been removed as an axis point candidate backed up, it may be considered idle
generally:
Lo assumed idle, the left expanding subsequence G, as long as the end of the element _elem [hi] axis is not less than the candidate points, it is decremented by one unit for hi
Thus the original elements included in the G, the new elements of the last still meet these conditions, continue to fall under G until the end of the element at some point no longer meets the conditions
Hi lo element unit is transferred to the end of the idle, hi becomes idle
Further investigation _elem [lo], to expand the L, the first element until the pivot point candidate lo does not exceed in value
Hi lo will be transferred to the
Examples
The first shaft element 6 is taken as the candidate point to be cultured, then remove the backup, the unit is regarded as idle logically
At this time, i.e. first examine the end of the element 7, the shaft is greater than the candidate points 6, classified into G
The new end element 6 is less than 1, to be classified in the L sequence, is transferred to this purpose lo 1 at idle, i.e., the position of the first element
Extended to the right, 3 less than 6, and then expand, 8 is greater than 6,
Transferred to the position hi, lo idle
5 on the left side is less than 6, proceeds to lo, hi idle
6 is smaller than the right side, the right side is less than 5 6
6 is larger than 9 right, go hi, lo idle
4 is less than 6 left, go lo
右侧的U只剩下一个元素,此处应放6,是轴点
性能分析
平均性能
a4 快速排序:变种
不变性:
L和G在U左边
单调性
实现
实例
b1 选取:众数
选取与中位数
众数
必要条件
减而治之
算法
b3 选取:通用算法
尝试:蛮力
尝试:堆(A)
尝试:堆(B)
尝试:堆(C)
H:任取k个元素,组织为大顶堆 // O(k)
G:其余n-k个元素,组织为小顶堆 // O(n-k)
反复地:比较h和g // O(1)
如有必要,交换之 // O(2*(logk+log(n-k)))
直到:h<=g // O(min(k, n-k))
quickSelect()
linearSelect()
复杂度
希尔排序
Shellsort
实例
call-by-rank
Insertionsort
Shell's Sequence
---恢复内容结束---