You are on page 1of 12
Week 1. Lecture Notes Topics: Insertion Sort Aralysis of Insertion Sort Recurrence of Merge Sort Substitution Method The problem of eo Input: @ sequence £Q\ 05, ---, On? of numbers Output : a permutation Jala... an’) Such that a, ga,’ g.c. Sian Example: Input: 13 5 0 4 F Output: O Oe Oy total Pseudo Code: Insertion Sort Twsertion Sort (A,n) DALt... nT] jor ee lton Seude - do Key 0 : Asymptotic Analysis . © notation Moth: 9) (qu) . j je) id positive constants 15 ond thet o£ exgens gure NM such ¥ n7No { Engineering, Drop low - order terwne; ignore leading constants Example Bn Fon?+ Sn-yorw = Ole*) O notation 0 (gos 5 ld: positive cons that yw 4 cq tants ¢ ard Mo ouch wy nr vo } Q notation (@) (ge») : f gta: such that 4b" € nBNno d posite couctants © and Yo > cgl") A Syme protic Perfor mance When n aets longe enough o lr’) algori thm ‘always’ beats a O(n) algori thn «We shouldn't ignore Gs maptotically, slower algori toms . Real world designs Too Situation often calls qr careful bolareing of engineerin objectives - As mnptotic analysis is o°useful tool to help to structure our thinki “4 Trsertion Sot Analysis Worst Cases Jnput inverse sosted The) © & OCj) = Blat) [orithmetic series] Average Cose: All permutations equally likely Toe 2 Alife) = Aln*) i ¢ \nsertion sovt ao vrodlerately fost for small ‘n’ + Jt a> notatall fast Sos loge vn’ Merge Sovt Merge- Sort ALt---0] Te sort w numbers - Monel, done 2. Reeursively Beet Ale Toi and AL rvte,... wn] 3 “Merge” the 2 sorted lists Kew subroutine : MERGE Enomple: Merging twe sorted arrays zo 12 20 12 20 12 | Jo 12 | Ro 12 |20 WD Is) ion fie ul Ou ory? | spree a 1 z Qol® 1 2 F 9 mW 12 18 20 Time = O(n) to merge @ tota) of 7m elements (near Hme ) Analyzing Merge Sost Merge- Sort (Ayn) bd Afiy....n] Tl) | To sort nm numbers OU) } 4. Ub wet done 27(%) 2 Recursively sost ALi... F%1] ord AL IIH, ..07] Olv)} 3. Merge the 2 sorted lists Should be T(F%1) +7 (U%41) bet it tums out wot to matter asymptotically Recurrence fox Merge Sost T(x) : 6) ie ne) AT (%) + OW) if n>) * We shall usually omit the bese cace when Th): 6U) po “oufPiciently swall w and when it has uo effect on the solutions to the vecurrence . Re cursion “Tree SoWe Tu): 2TlWle)+ CN, where C>0 is constant cH / ee CM OM ~~ = een he J\ / yn ch oy —n ach dys nfm a se © Tetal = @lnlgn) Conclusions + Olnlgn) grows more slowly then Bln") There fore, merge sort asymp totically beets insertHor sort in the worst cose ow practice, merge sort loeats insertion Sort for n> 30 ov so Solving Racunverte 6 : Substitution Method is the most general method : 1 Guess Ma fee SF oh 2 Neri fy by seduction 3. Seve for constants Eoowple Tiny = 4T (a) +0 » [Asoume that Tie 609] + Guess olw’) Assume thot Tlk) £ck® jor Ken + Prove The) ¢ ev? by induction . E mam ple of Substitution Tle 4T (Me) +9 he (hy +n (e/2) ween SNe ( (Ayreon) + desived -1esidua) Nn “ £ cn® + desired Whenevey ((q)n?-n) 20, ter enample f if cre, ne) residual Erom ple ( Continued ) * We must also handle the initia) condittons that is, ground the induction wit, bace caces, Base m opin) © Ou) for al! Nene, where ve isa suitable constant + For LEnene, we hove OU)" £ on™ 5p we pick c big enough But this teund is net tight A Tig blew upper beund Ke shall Prove thet Tin) = Olnt) Assume thet Tle) eer for Ken: Tin) = AT (2) +n £ Gewan = Om wrong! we wust prove the J.H. > ev'— (-n) I desived - residual ] ¢ ent yer mo choice of Cr0 We lose A Fighter upper bound Trea: Strengthen the inductive hgpethesis * Subtract o low-osder term. Jnduction hypothesi¢: TU 2 ge ck pr KS ti) = aT) +" 2m (GUaye ol) +9 = cw - 2anrn 2 qnt- Gene Len) £ Cwr- nr to hendle this situeton. if ¢97) Wel eich Ce Dia Sood

You might also like