Introduction to Algorithms, Third Edition


d. How should we choose k in practice? 2-2



Download 4,84 Mb.
Pdf ko'rish
bet40/618
Sana07.04.2022
Hajmi4,84 Mb.
#534272
1   ...   36   37   38   39   40   41   42   43   ...   618
Bog'liq
Introduction-to-algorithms-3rd-edition

d.
How should we choose
k
in practice?
2-2
Correctness of bubblesort
Bubblesort is a popular, but inefficient, sorting algorithm. It works by repeatedly
swapping adjacent elements that are out of order.
B
UBBLESORT
.A/
1
for
i
D
1
to
A:
length
1
2
for
j
D
A:
length
downto
i
C
1
3
if
AŒj < AŒj
1
4
exchange
AŒj 
with
AŒj
1
a.
Let
A
0
denote the output of B
UBBLESORT
.A/
. To prove that B
UBBLESORT
is
correct, we need to prove that it terminates and that
A
0
Œ1
A
0
Œ2
A
0
Œn ;
(2.3)
where
n
D
A:
length
. In order to show that B
UBBLESORT
actually sorts, what
else do we need to prove?
The next two parts will prove inequality (2.3).
b.
State precisely a loop invariant for the
for
loop in lines 2–4, and prove that this
loop invariant holds. Your proof should use the structure of the loop invariant
proof presented in this chapter.
c.
Using the termination condition of the loop invariant proved in part (b), state
a loop invariant for the
for
loop in lines 1–4 that will allow you to prove in-
equality (2.3). Your proof should use the structure of the loop invariant proof
presented in this chapter.


Problems for Chapter 2
41
d.
What is the worst-case running time of bubblesort? How does it compare to the
running time of insertion sort?

Download 4,84 Mb.

Do'stlaringiz bilan baham:
1   ...   36   37   38   39   40   41   42   43   ...   618




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©hozir.org 2024
ma'muriyatiga murojaat qiling

kiriting | ro'yxatdan o'tish
    Bosh sahifa
юртда тантана
Боғда битган
Бугун юртда
Эшитганлар жилманглар
Эшитмадим деманглар
битган бодомлар
Yangiariq tumani
qitish marakazi
Raqamli texnologiyalar
ilishida muhokamadan
tasdiqqa tavsiya
tavsiya etilgan
iqtisodiyot kafedrasi
steiermarkischen landesregierung
asarlaringizni yuboring
o'zingizning asarlaringizni
Iltimos faqat
faqat o'zingizning
steierm rkischen
landesregierung fachabteilung
rkischen landesregierung
hamshira loyihasi
loyihasi mavsum
faolyatining oqibatlari
asosiy adabiyotlar
fakulteti ahborot
ahborot havfsizligi
havfsizligi kafedrasi
fanidan bo’yicha
fakulteti iqtisodiyot
boshqaruv fakulteti
chiqarishda boshqaruv
ishlab chiqarishda
iqtisodiyot fakultet
multiservis tarmoqlari
fanidan asosiy
Uzbek fanidan
mavzulari potok
asosidagi multiservis
'aliyyil a'ziym
billahil 'aliyyil
illaa billahil
quvvata illaa
falah' deganida
Kompyuter savodxonligi
bo’yicha mustaqil
'alal falah'
Hayya 'alal
'alas soloh
Hayya 'alas
mavsum boyicha


yuklab olish