You are on page 1of 20
PRACTICE PROBLEM SHEC Tem NAME ® AAROR! MISHRA REG. NO» % IBCES303 QuesTION | (Z) INPUT s Sequence of w elements ay, a21--7~ 1 any OUTPUT § Ineveasing order 0 elements NRE CAD: Lan CD) DATA STRUCTURE : A[a,larlazy ~~~ Jan | an] f) LOGIC AND SAMPLE INPUT « WV Let A= |-3 [17] -s] i100 a 1s | 4% smallest element :~-S D> let -3 be mintmum: ATIT > mn = |} D min ts compared with remoming all elements Pia RG) No) chonae. Pelee esi) Se 3) vs greater than -S um betomes -S, by Swapptng (ALt) ond A(Z7 > Ee fiz} 3 [109] % smallest element ts placed at ALI) spre th te known as election Sort gmoallest dhement ia placed at 24 BJ 1S (V) Aicorimmm Selechon — Sort (A) L for j <1 40 n-1 2 min =] # for i (min +1) YO Z if A{min} » ATCT Zi temp = AL] 2 ALT] = A Lenin] if Aen 1 = +P (UE) PROOF OF CORRECTNESS Trwartant # At step k, first K elements are sorted and the omen elements ave ungorted- loop Thvoyvant & Jnitalizatfon 3 A step 0, O elements ave Sorted. 3% The invartant \s true prior fe +ne 1 Geratton of loop Macntenonce : Agsume the fnvaviant is true for kM step . o, ALII213)---» K ave sorted and the Vemosneng elements ave unsorted. Assume min =K, using Sor loop minimum = ALK] f compaved with the Tremaine elements and we ave finding the kh smallest element among those elements and placing the kth Smallest element at- A tmind oN image; ce Kf ore sovted D0 @) ALcor\ITHM es Bubbte Sort (A) i Ve2}))- Vf, (n) DATA STRUCTURE s Array (ML) LOGIC and (WZ) SAMPLE INPUT 2 let words = hat, cat, mat ; pat > Value of eoch word ts Calewlated by putting de bao yun. Then +hese values ave stored Mm On Bay and gorted inte decveasing order, , yr } } ' } Vicor Pyare eae lt 20 ue) Vin) = Cromer S! Poor y (3) = mrark da to 34 Vy MO) = gee Sh ae After sorting » Clements well be desplayed ag pat, mat, hat) cae (Z) ALGORITHM 2 caluuatelalue (word) sum <-O i =O 40 word: Jeng tC) {[ word Ei)-'a an tle) e Sum <> Se wesem +( velumn su Sort_words (m) for (<0 ton vowue Ci Caloulosevelue (worelsf1) ie 10 ton ei 5 & ttl Crater) Cvane lJ) Sway ata twords £)) Swap ( value LU» value Cj) (WE) PROOF OF coRRECTNESS “ee INVARIANT = At siep Ky value a wovds are foun and kK elements [words] are © vied in descending ordey LOOP INVARIANT s Trihowzations AY step words ave sored , Ss The invertant O values are ee and O tg tue prior to ISt uevation - Maintenances Agsume wnvavant ts true for kth ieration ye Value 0. kK wovds owe calcul ated and Sovteds using bubble eort - Catentate vouure a hoe teu ae ap QW ithe letters bn the word usung colelate lalue unctton . The function @ovt_words So7t the wos y bubple Sort an bye eration, Stt onty + words are Sorted and n-e words Gre ungovied . art, the value ts ialicieds Geer s ee Ae mange im descending ordoy cyyorth and av Termination = The ievahton Jerminates where K=nrl)i-e 1 words ave sored deereasingly J based on thety values, TIME COMPLEXITY 5 Time complenmty = © (nlogn) > Time. commplentty vs @(mlogn) ag sorkeng the values Bay, wake (Tig) keme and fends the word Boe a eoUg oie Om) tiene tn word ery QUESTION 7 (ar ) INPoTs Sequence of mp elements <0, 142 1> 1 n> OUVMUT?2 Tnoveasing orley of n Gemen', Key) reg ht mae) else left ae md +4 eee! 5 nile Cy 7 left) rojyrid& AG) ja acta ec Key

You might also like