计算机算法导论_第8章

合集下载
相关主题
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
1. A in-place sorting algorithm 2. Comparison sort 3. The counting sort algorithm 4. The radix sort algorithm 5. The bucket sort algorithm
h
6
Order statistics
h
19
Counting sort
for i ← 1 to k do C[i] ← 0
for j ← 1 to n do C[A[ j]] ← C[A[ j]] + 1 ⊳ C[i] = |{key = i}|
for i ← 2 to k do C[i] ← C[i] + C[i–1] ⊳ C[i] = |{key ≤ i}|
h
16
Lower bound for decision-tree sorting
h
17
Lower bound for comparison sorting
Corollary. Heapsort and merge sort are asymptotically optimal comparison sorting algorithms.
h
9
How fast can we sort?
All the sorting algorithms we have seen so far are comparison sorts: only use comparisons to determine the relative order of elements.
• The tree contains the comparisons along all possible instruction traces.
• The running time of the algorithm = the length of the path taken.
Biblioteka Baidu
• Worst-case running time = height of tree.
h
3
The structure of the data
Definition:
Record = key + satellite data
Assumption:
The input consists only of numbers
h
4
Why sorting?
1. The need inherent in an application 2. Algorithms often use sorting as a key subroutine 3. A wide variety of sorting algorithms, a rich set
•E.g.,insertion sort, merge sort, quicksort, heapsort. The best worst-case running time that we’ve seen for comparison sorting is O(n lg n) . Is O(n lg n) the best we can do? Decision trees can help us answer this question.
Introduction to Algorithms
计算机算法导论
2007~2008年第一学期
h
1
Sorting and Order Statistics
Introduction
h
2
Sorting problem
Definition:
Input: A sequence <a1, a2, …, an> of numbers. Output: A permutation <a'1, a'2, …, a'n>, such that a'1 ≤ a'2 ≤ … ≤ a'n .
h
10
Decision-tree model
A decision tree can model the execution of any comparison sort:
• One tree for each input size n.
• View the algorithm as splitting whenever it compares two elements.
The ith order statistic of a set of n numbers is the ith smallest number in the set.
h
7
8、 Sorting in linear time
h
8
8.1 Lower bounds for sorting
Assumption: 1. All of the input elements are distinct 2. All comparisons have the form ai ≤ aj
of techniques 4. A problem can be proved a nontrivial lower
bound. 5. Many engineering issues come to fore when
implementing sorting algorithms.
h
5
Sorting algorithms
for j ← n downto 1 do B[C[A[ j]]] ← A[ j] C[A[ j]] ← C[A[ j]] – 1
h
20
Running time
If k = O(n), then counting sort takes Θ(n) time. • But, sorting takes Ω(n lg n) time! • Where’s the fallacy? Answer: • Comparison sorting takes Ω(n lg n) time. • Counting sort is not a comparison sort. • In fact, not a single comparison between elements occurs!
h
18
8.2 Counting sort
Sorting in linear time
Counting sort: No comparisons between elements. • Input: A[1 . . n], where A[ j]∈{1, 2, …, k} . • Output: B[1 . . n], sorted. • Auxiliary storage: C[1 . . k] .
相关文档
最新文档