Algorithm Design and Analysis Course Review notes 3-- linear-time sorting method
Linear Time Sorting
Recalling sort
insertion sort:
easy to realize, suitable for small-scale input (typically less than 50 elements), a good approximation of the effect of the sorted input
worst case: O (n- 2 )
Merge sort:
Partition: segmentation, recursive processing, linear time merging
the worst case: O (nlgn)
Common of the above algorithm: Based on the comparison between elements twenty-two
obtained by comparison between the relative order of the input elements, the comparison operation are:
<
,
≤
,
=
,
≥
, or
>
(In the worst case, the sort of thinking based on the comparison must be done Ω (nlgn) comparisons)
Decision Tree Model (Decision Tree Model)
- It represents all possible comparison of sorting algorithms based on a comparison
- Neglect of other operations, such as control, data transfer and other operations
- Only consider compare
- Three basic elements
! n-permutations corresponding to the same number of leaf nodes
example: 123,132,213,231,312,321 total of six arrangement
decision tree leaf nodes 6 corresponding thereto must appear
Worst case: from the root to the leaves of the longest path
Lemma: tree height h of any binary tree up to 2 h leaf nodes
Any comparison based sorting algorithms:! N ≤ number of leaf nodes ≤2 H
(! N-) h≥lg = [Omega] (nlgn)
Counting Sort
- The elements to be sorted is an integer between 0 to k
- For each element x, is less than or equal to the number of determined elements of the element x
- The case where the count will be placed in positions corresponding to the elements
Algorithm is as follows:
Array A is input, an output array B for storing the sorted array to provide temporary storage space C
CountingSort(A,B,k)
让C[0……k]成为一个新数组
for i=0 to k Θ(k)
C[i]=0
for j=1 to A.length Θ(n)
C[A[j]]=C[A[j]]+1
//C[i]现在存储了等于i的元素数目
for i=1 to k Θ(k)
C[i]=C[i]+C[i-1]
//C[i]现在存储了小于等于i的元素数目
for j=A.length downto i Θ(n)
//不从i到A.length而选择从A.length到i,这样保证了稳定性
//即相等的输入元素的位置先后顺序在排列后输出的先后顺序不会改变.
B[C[A[j]]]=A[j] //根据计数的情况将元素放到相应的位置
C[A[j]]=C[A[j]]-1 //取出后减1
Equal input position of the element does not change the order after the order of arrangement of output:
the time cost: Θ (n + k)
Radix Sort
Expanding the range of sortable integer
number of groups represented by:
Example:
16536. 1 ⋅ = 10 . 4 +. 6 ⋅ 10 . 3 + 10. 5 ⋅ 2 + ⋅. 3 10 . 1 + 0. 6 ^ ⋅10
= 1 ⋅ 25 + 0 ⋅ 24 + 1 ⋅ 23 + 1 ⋅ 22 + 0 ⋅21+ 1 ⋅ 20=45
Algorithm is as follows: starting from the last is the one who is least
RadixSort(A,d)
for i=1 to d
使用稳定的排序方法对第i位排序
If counting sequencing, a row need Θ (n + k), it is necessary bit row d Θ (d (n + k))
Bucket sort
- (Input in [0, 1) uniform, homogeneous distribution)
- The [0, 1) n aliquots, called empty bucket
- The n input dispatched into the hollow barrel
- Each bucket sorting
- Sequentially traverse the elements in each bucket
Algorithm is as follows:
BucketSort (A)
n-= A.length
let B [0 ...... n-1] becomes the new array
for i = 0 to n-1
Let B [i] becomes an empty list
for i = 1 to n
to A [i] is inserted into the list B [
nA[i]
]
for I = 0. 1 to n--
list B [i] for insertion sort
order traverse the elements in each bucket and connected to B [0], B [1
input satisfies consistency random distribution, the average operating cost of bucket sort is O (n)
Reference: classroom teachers Professor Khoo Teck red courseware