Introduction to Algorithm 3rd Edition

Our first algorithm, insertion sort, solves the sorting problem:
Input: A sequence of n numbers
a0,a1,a2,a3.......an
Output:
A pernutation (reordering)
a1,a2,a3......an
of the input sequence such that
a1a2a3......an
The numbers that we wish to sort are also known as the keys. The input comes to us in the form of an array with n elements.
Insertion-Sort(A)

for j=2 to A.length
    key=A[j]
    //Insert A[j] into the sorted sequence A[1...j-1].
    i=j-1
    while i>0 and A[i]>key
        A[i+1]=A[i]
        i=i-1
        A[i+1]=key

(In the later chapter, the code I will use is Python as an example to run the program.)

We use loop invariants to help us understand why an algorithm is correct. We must show three things about a loop invariant:
Initialization: It is true prior to the first iteration of the loop.
Maintenance: If it is true before an iteration of the loop, it remains true before the next iteration.
Termination: When the loop terminates, the invariant gives us a useful property that helps us show that the algorithm is correct.
对于插入排序:
初始化:
首先证明在第一次循环迭代之前(当j=2时),循环不变是成立。所以子数组A[1..j-1]仅由单个元素A[1]组成,实际上就是A[1]中原来的元素。而且该子数组是排序好的(当然很平凡)。这表明第一次循环迭代之前循环不变式成立。
保持:
其次处理第二条性质:证明每次迭代保持循环不变式。有些通俗地讲,for循环体的第4~7行将A[j-1]、A[j-2]、A[j-3]等向右移动一个位置,直到找到A[j]的适当位置,第8行将A[j]的值插入该位置。这时子数组A[1..j]由原来在A[1..j]中的元素组成,但已按序排列。那么对for循环的下一次迭代增加j将保持循环不变式。
终止:
导致for循环终止的条件是j>A.length=n。因此每次循环迭代j增加1,那么必有j=n+1。在循环不变式的表述中将j用n+1代替,我们有:子数组A[1..n]由原来的A[1..n]中的元素组成,但已按序排列。注意到,子数组A[1..n]就是整数组,我们推测出整个数组已经排序。
因此算法正确。

Logarithms
We shall use following notations:
lgn=log2n (binary logarithm)
lnn=logen (natural logarithm)
lgkn=(lgn)k (exponentiation logarithm)
lglgn=lg(lgn) (composition)
For all real a>0,b>0,c>0,and n,
a=blogba
logc(ab)=logca+logcb
logban=nlogba
logb(1/a)=logba
logba=1/logab
alogbc=c(logba)
where, in each equation above, logarithm bases are not 1.

More recurrence examples
a. T(n)=4T(n/3)+nlgn
b. T(n)=3T(n/3)+nlng
c. T(n)=4T(n/2)+n2n
d. T(n)=3T(n/32)+n/2
e. T(n)=2T(n/2)+n/lgn
f. T(n)=T(n/2)+T(n/4)+T(n/8)+n
g. T(n)=T(n1)+1/n
h. T(n)=T(n1)+lgn
i. T(n)=T(n2)+1/lgn
j. T(n)=nT(n)+n

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值