How fast can we sort? (Depends on the sorting model: what you can do with the elements)
Comparison sorts: only use comparisons to determine relative order of elements:
quicksort - Theta(nlgn) randomized version
heapsort - Theta(nlgn)
merge sort - Theta(nlgn)
insertion sort - Theta(n^2)
Any comparison sorting algorithms require at least Omega(nlgn) comparisons in the worst case: proof by decision tree model, height h of the decision tree corresponds to the worst case time, each of the n! permutations of the input appears as some leaf, n! < 2^h, thus h = Omega(nlgn).
Heap sort and merge sort are asymptotically optimal comparison sorts (worst-case also have upper bound nlgn).
Counting sort
Assume each of the n input elements is an integer in the range 0 to k. If k=O(n), the sort runs in Theta(n) time.
Basic idea: for each input element x determine number of elements less than x.
Input: A[1..n], each A[i] is in the range 0 to k.
Output: B[1..n] = sorted A
Temp storage: C[0..k] for counting, k is the range.
COUNTING-SORT(A, B, k)
for i := 0 to k // O(k)
do C[i] := 0
for j := 1 to length[A] //O(n)
do C[A[i]] := C[A[j]] + 1
for i := 1 to k // O(k)
do C[i] := C[i] + C[i - 1]
for j := length[A] downto 1 //O(n)
do B[C[A[j]]] := A[j]
C[A[j]] := C[A[j]] - 1
So the running time is O(n+k), if k = O(n) then O(n) time. Counting sort is only effective to integers with small range, meaning that k is small is a requirement.
Another important property of counting sort: Stability, which is used in the following algorithm: radix sort.
Stable sort preserves the relative order of equal elements.
[Q] Which other sorts are stable? http://en.wikipedia.org/wiki/Stable_sort#Stability
Radix sort [Herman Hollerith -1890], effective to large range integers. Idea is to sort on the least significant digits of those integers with a stable sorting algorithm, say, the counting sort.
[Ex]. 3 2 9 7 2 0 7 2 0 3 2 9
4 5 7 3 5 5 3 2 9 3 5 5
6 5 7 4 3 6 4 3 6 4 3 6
8 3 9 => 4 5 7 => 8 3 9 => 4 5 7
4 3 6 6 5 7 3 5 5 6 5 7
7 2 0 3 2 9 4 5 7 7 2 0
3 5 5 8 3 9 6 5 7 8 3 9
Proof the correctness: use induction on digit t.
1. assume by induction the array is sorted on low-order t-1 digits.
2. sort on digit t:
- if two elements have same digits on the position t, after sorting they have
the same order because of stability.
- if no same digits, they are just sorted.
So the induction hypothesis is true.
In practice, integers or other stuffs in computer world are binary bits, so group them as digits, for example, 32 bits integers can be grouped as four bits digits or bytes, etc.
Analysis:
- if using counting sort as the stability requirement, we need O(k+n) for each round (digit).
- suppose sorting n integers, each is b bits, so the range is [0, 2^b - 1].
- split integer into b/r "digits", each r bits, thus each digit ranges in [0, 2^r - 1], k = 2^b.
The counting sort runs b/r rounds.
Time: O(b/r * (n + k))
= O(b/r * (n + 2^r))
so r is choosed properly to minimize the time: r = lgn =>
Time: O(bn/lgn).
分享到:
相关推荐
算法导论 lecture3
NULL 博文链接:https://danix800.iteye.com/blog/762473
NULL 博文链接:https://danix800.iteye.com/blog/761602
算法导论 lecture2
麻省理工 算法导论 lecture 麻省理工 算法导论 lecture
麻省理工算法导论全英课件,lecture12。主要讲述正交范围搜索等内容。
MIT2011年算法导论公开课lecture1字幕
包含算法导论中文版,英文版,以及部分习题答案。适合学习算法的大学生及专业人士。
完整版算法导论 包括: 第二版算法导论 英语pdf版、chm版 算法导论 中文版(非机械工业出版) Instructor Manual导师手册 Lecture Notes讲义 algorithms, data structures, and problem solving with c++ 算法,数据...
[麻省理工学院-算法导论].Introduction.to.Algorithms.-.Lecture.Notes 算法导论-课堂笔记 讲义
[麻省理工学院-算法导论].Introduction.to.Algorithms.-.Lecture.Notes
【计算机视觉】Lecture9:立体视觉算法 计算机视觉.pdf
课程介绍及相关基础知识概述 Le ctu re2 :进程管理与调度 Lecture3:中断处理、系统调用 Lecture4:内核同步 Lecture5:内存管理(1) Lecture6:内存管理(2) Lecture7 :文件系统 Lecture8:内核设备模型、时钟...
[麻省理工学院-算法导论].Introduction.to.Algorithms.-.Lecture.Notes
目前最全的算法导论第三版的答案 清晰度极好 另每章都有章节重点总结 在第二版基础上新增习题的解答详尽清楚 很适合 用这本经典教材的同学! Contents Revision History R-1 Preface P-1 Chapter 2: Getting Started...
CS224D: Deep Learning for NLP Course Instructor: Richard Socher Lecture Notes: Part III
Lecture 2: Models of Computation
Lecture 3: Objective-C
Lecture 1: Introduction and Peak Finding