Tải bản đầy đủ (.pptx) (6 trang)

Discrrete mathematics for computer science series warmup

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (862.3 KB, 6 trang )

Series Warmup


A really bad way to maintain order








Given a list L in order




Unbeknownst to Joe, Sort does a bubble sort

Suppose elements get added dynamically
Problem is to keep it in order
Joe has a sort function available Sort(L)
He learned in CS50 not to reinvent any wheels
So when a new element x arrives he appends it to the end of L: L
<- append(L,x) and then calls Sort(L)
How many item comparisons, starting from an empty list and
adding n elements?


Recall Bubble Sort Analysis






((n-1)∙n)/2 comparisons to sort a list of length n
So

(i − 1)ι 1 ν 2 1 ν
∑ 2 = 2 ∑ι − 2 ∑ι
ι=1
ι=1
ι=1
ν

But what is the sum of the first n squares?







n=4, sum of first 4
squares
Area = n across and
tall
ν




How big isi the white
space? i=1
Let


ν

S(n) = ∑ ι2
ι=1
ν

Ι (ν) = ∑ ι =
ι=1

ν(ν + 1)
2


ν

S(n) = ∑ ι2
ι=1

ν(ν + 1)
Ι (ν) = ∑ ι =
2
ι=1
ν

ν


S(n) + ∑ Ι ( ϕ) =
ι=1

(ν + 1)(ν)(ν + 1)
2

ι2 ν ι (ν + 1)(ν)(ν + 1)
Σ(ν) + ∑ + ∑ =
2
ι=1 2
ι=1 2
ν

3
ν3
ν ν2 ν
2
Σ(ν) = + ν + − −
2
2
2 4 4
ν 3 ν2 ν
Σ(ν) = + +
3 2 6
1 ν 2 1 ν
ν 3 ν2 ν ν2 ν
∑ i − 2 ∑ ι = 6 + 4 + 12 − 4 − 4
2 i=1
ι=1


ν 3 3ν2 ν
= +

6
4
6

Of course this is Θ(n3)!


FINIS



×