CSE102 Algorithm总结:
一、 什么是算法? 为什么需要算法?
(What is algorithm? And why we need algorithm?)
1、 What is algorithm?
Algorithm design is a foundation forefficient and effective program.
2、 Why we need algorithm?
Algorithm is a sequence of precise andconcise instructions that guide you or computer to solve a class of specificproblem.
二、 渐近分析(asymptotic analysis)
1、 时间复杂度分析(Time complexity analysis)
时间复杂度分析的核心就是计数,即计算算法每个语句执行的数量。
2、 空间复杂度分析(Space complexity analysis)
空间复杂度中的空间指的是内存(包括物理内存、虚拟内存但不包括磁盘),一个程序占用的内存越少程序更优。
3、 量级与渐近线(magnitude and asymptotic)
如果一个算法比另外一个算法多一个步骤,我们不会认为这两个算法的效率有什么不同,多两个步骤也是如此,但是如果多100个,1000个效率就有区别了,我们怎么来区别这些效率呢,这时我们就需要在某个地方画一条线,此线范围内的算法的效率我们认为差不多,而超过这个范围的就是效率有很大的不同。因此,我们就引出了量级的概念(量级包括:常数级、对数级、多项式级、和指数级)。
注: 这里的渐进分析我们只观察当输入尺寸n趋向无穷时的算法表现。
Example:
Show that 2n2+4n is O(n2)
Solution:
Since n≤n2for all n>1
We have 2n2+4n ≤ 2n2+4n2≤6n2for all n>1
Therefore, by definition, 2n2+4nis O(n)
4、 量级效率大小:
O(1) < O(logn)<O(n)<O(n2)<….<O(nk)<O(2n)
三、 搜索与排序算法(Searching and sort)
1、 二分搜索(binary search):
注意二分搜索的输入数组必须是按从小到大排序的。
(1) pseudo code:
binarySearch(Array[0…n-1], first, last, X)
for i=0 to n-1 do
begin
midß (first+last)/2
iffirst>=last then
returnnot found
ifa[mid]=X then
returnmid
ifa[mid]>X then
returnbinarySearch(Array, first, mid-1, X)
ifa[mid]<X then
returnbinarySearch(Array, mid+1, last, X)
end
(2)时间复杂度:
Best case: O(1)
Worst Case: O(logn)
2、 冒泡排序(Bubble Sort):
(1) Pseudo code:
bubbleSort(Array[0…n-1])
for iß0 ton-1 do
begin
forjßI to n-1 do
begin
if Array[j]>Array[j+1] then
tempßArray[j]
Array[j]ßArray[j+1]
Array[j+1]ßtemp
end
end
(2) Time complexity:
O(n2)
3、 插入排序(Insertion Sort):
(1) pseudo code:
insertionSort(array(0…n-1))
for iß1 ton-1 do
begin
kßarray[i]
pßi
while p>0 andarray[p-1]>k do
array[p]ßarray[p-1]
pßp-1
array[p]ßkey
end
return array[0…n-1]
(2) Time complexity:
O(n2)
4. 选择排序(SelectionSort):
(1)Pseudo code:
selectSort(Array[0…n-1])
for iß0 ton-1 do
begin
minßi
forjßi+1 to n-1 do
ifa[j]<a[min] then
minßj
swapa[min] and a[i]
end
return array
(2)Time complexity:
O(n2)
四、 分治法(Divide and conquer)
1. What is divide and conquer?
A problem instance is divided into severalsmaller instances of the same problem. And the smaller instances are solved. Atlast, the solutions for the smaller instances are combined to get a solution tothe original problem.
2. (1) Calculate the timecomplexity of binary search with recursive formula:
1 if n = 1
T(n) =
T(n/2)+1 otherwise
Solution:
T(n/2)+1
=(T(n/4)+1)+1
=T(n/4)+2
.
.
.
=T(n/2k)+k
Since T(1) = 1
n/2k = 1
2k=n
K = logn
So when k = log n
T(n) = T(1) + logn = 1 + logn
Thus, the time complexity is O(logn)
(2)Show that T(n) = O(logn) by the substitutionmethod. (Hint: show that T(n)≤2logn for n≥2 by Mathematical induction)
Solution:
When n=1,
L.H.S = T(1) = 1; R.H.S = clogn=clog1=0
Because o<1 then the statement is false
Yet, when n=2
L.H.S = T(2) = T(1)+1 = 2; R.H.S = clogn = clog2 = c
when c = 2, R.H.S = 2
L.H.S≤R.H.S
Make a guess, T(n) ≤2logn
Assume true for all n’<n T(n/2)≤2log(n/2)
So T(n)=T(n/2)+1
≤ 2log(n/2)+1
=2(logn-log2)+1
=2(logn-1)+1
=2logn-1
<2logn
Thus, guess istrue T(n)≤2logn
3. 合并排序(Merge Sort)
合并排序用到了归并思想。
(1) Pseudo code:
mergeSort(array[0…n-1],p,q)
if p<q then
midß (p+q)/2
mergeSort(array,p,m)
mergerSort(array,m+1,q)
conquer(array,p,m,q)
else
returnarray
conquer(array[0…n-1],p,m,q)
p1ßp
p2ßm+1
cntß0
make b[0…100] be a new array
while p1<=m and p2<=q do
begin
ifa[p1]<a[p2]
b[cnt]ßa[p1]
p1ßp1+1
cntßcmt+1
else
b[cnt]ßa[p2]
p2ßp2+1
cntßcnt+1
end
if p1>m then
forißp2 to q do
begin
b[cnt]ßa[i]
cntßcnt+1
end
if p2>q then
forißp1 to m do
begin
b[cnt]ßa[i]
cntßcnt+1
end
for iß0 tocnt do
begin
a[i+p]ßb[i]
end
return a
(2) Time complexity:
O(nlogn)
The recurrent formula is:
1 if n=1
T(n) =
2T(n/2) + n otherwise