Introduction to Algorithms (Counting Sort, Radix Sort, Lower Bounds for Sorting)

本文探讨了比较模型的基本概念及其限制,并介绍了决策树作为分析比较算法的一种方式。此外,文章还讨论了如何在特定条件下实现线性时间复杂度的排序算法,包括计数排序和基数排序的方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Comparison model

  • all input items are black boxes (ADTS)
  • only operations allowed are comparisons (<,<=,>,>=)
  • time cost = # comparisons

Decision tree

any comparison algorithm can be viewed as a tree of all possible comps & outcomes

  •  internal node = binary decision
  • leaf = output
  • root-to-leaf path = algorithm execution
  • path length = running time
  • the height of tree = worst-case running time

 Lower bound

Claim:

  • n preprocessed items
  • finding a given item among them in comparison model requires \Omega(lgn) in worst-case
  • sorting n items requires \Omega(nlgn)                  

Linear time sorting(Integer sorting) 

  • assume n keys sorting are integers ∈ {0, 1, 2, 3, ... , k-1} (&each fits in a word)
  • can do a lot more than comparisons
  • can sort in O(n) time

Counting Sort

L = array of k empty lists lists

for j in range n:
    L[key(A[j])].append(A[j])

output = []

for i in range k: 
    output.extend(L[i])

Radix Sort

  • imagine each integer in base b
  • d = log_{b}^{k}
  • sort by least significant digit
  • ...
  • sort by most signigicant digit
  • use counting sort for digit sort

\Theta ((n+b)log_{b}^{k})

minimized when b = n

\Theta (nlog_{n}^{k})

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值