Selection in expected linear time O(n)
来源:互联网 发布:rrt路径规划算法 编辑:程序博客网 时间:2024/06/06 04:04
Selection in expected linear time
The general selection problem appears more difficult than the simple problem of finding a minimum. Yet, surprisingly, the asymptotic running time for both problems is the same: theta(n).
In this section, we present a divide-and-conquer algorithm for the selection problem. The algorithm R ANDOMIZED-SELECT is modeled after the quicksort algorithm of Chapter 7. As in quicksort, we partition the input array recursively. But unlike quicksort, which recursively processes both sides of the partition, R ANDOMIZED -S ELECT works on only one side of the partition. This difference shows up in the analysis: whereas quicksort has an expected running time of theta{n*lg(n)}, the expected running time of RANDOMIZED-S ELECT is O(n), assuming that the elements are distinct.
下面是random_select()的实现:
返回在输入数据中,第i大的数据,时间复杂度是O(n)
"""Code writer : EOFCode date : 2015.01.15Code file : random_select.pye-mail : jasonleaster@gmail.comCode description : Here is a implementation for random-select in Python.Function @random_select(A, p, r, i) will return the i-thelement from the @A which's range is @p to @r."""import randomdef partition(A, p, r) : x = A[r] i = p-1 for j in range(p, r) : if A[j] <= x : i += 1 # swap A[i] and A[j] A[i], A[j] = A[j], A[i] A[i+1], A[r] = A[r], A[i+1] return i+1def random_partition(A, p, r) : i = random.randint(p,r) A[r], A[i] = A[i], A[r] return partition(A, p, r)def random_select(A, p, r, i) : if p == r : return A[p] q = random_partition(A, p ,r) k = q - p + 1 if i == k : return A[q] elif i < k : return random_select(A, p, q-1, i) else : return random_select(A, q+1, r, i-k)#-------------------testing code---------------------------A = [13,19,9,5,12,8,7,4,21,2,6,11]print "Before sorting A= " , Ax = random_select(A,0,len(A)-1, 5)print "After sorting A= " , sorted(A), x
- Selection in expected linear time O(n)
- Selection in expected linear time
- 线性时间排序 Sorting in linear time O(n)
- 【算法导论学习-015】数组中选择第i小元素(Selection in expected linear time)
- Solution to Maximum Subarray in linear-time algorithm with time complexity is O(n)
- 算法导论 Chapter 9.3 Selection in worst-case linear time
- good article————K’th Smallest/Largest Element in Unsorted Array | Set 2 (Expected Linear Time)
- Algs4-1.4.18 Find local minimum in n x n matrix in O(n) time
- Linear Time Selection[An Ω(nlogn) Sorting Lower Bound]
- [算法导论 Ch9 中位数和顺序统计量] Selection in O(n)
- Find duplicates in O(n) time and O(1) extra space
- Sort a linked list in O(n log n) time using constant space complexity
- leetcode_Sort a linked list in O(n log n) time using constant space complexity.
- Sort a linked list in O(n log n) time using constant space complexity.
- Sort a linked list in O(n log n) time using constant space complexity.
- Sort a linked list in O(n log n) time using constant space complexity.
- Sort a linked list in O(n log n) time using constant space complexity.
- Sort a linked list in O(n log n) time using constant space complexity.
- 实现无序数组
- 开发工具
- 搭建自己的Git服务器
- 发布几个google的代理地址
- 修改UISearchBar的Cancel为中文
- Selection in expected linear time O(n)
- 在ASP中 使用 kindeditor-4.1.4编辑器的详细教程
- Android studio 使用Assets文件夹
- BZOJ 3560 DZY Loves Math V 数论
- Simditor上传图片(ASP.NET+JS/Jquery)
- dsa算法(16)
- 三大运营商合力围剿黑卡 重罚违规渠道
- 真正的理解setup time/hold time
- jsp页面中 href传递值