提示
基于比较的排序算法在最坏情况下至少需要比较 次
2024年12月20日大约 2 分钟
提示
基于比较的排序算法在最坏情况下至少需要比较 Ω(nlgn) 次
quicksort(A,p,r)
if p < r
// Partition the subarray around the pivot, which ends up in A[q].
q = partition(A,p,r)
quick_sort(A,p,q-1)
quick_sort(A,q+1,r)
PARENT(i)
return i/2
LEFT(i)
return 2i
RIGHT(i)
return 2i+1
hire_assistant(n)
best = 0 // candidate 0 is a least-qualified dummy candidate
for i = 1 to n
interview candidate i
if candidate i is better than candidate best
best = i
hire candidate i
matrix_multiply(A,B,C,n)
for i = 1 to n
for j = 1 to n
for k = 1 to n
C[i][j] = C[i][j] + A[i][k] * B[k][j]
公式
O(g(n))={f(n):0≤f(n)≤cg(n)∀n≥n0}
insertion_sort(A,n)
for i = 2 to n
key = A[i]
// Insert A[i] into the sorted subarray A[1:i-1].
j = i - 1
while j > 0 and A[j] > key
A[j + 1] = A[j]
j = j - 1
A[j + 1] = key