100% found this document useful (1 vote)
515 views26 pages

Binary Seach Aditya Verma

Adiya verma notes binary search not writeen by me
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
100% found this document useful (1 vote)
515 views26 pages

Binary Seach Aditya Verma

Adiya verma notes binary search not writeen by me
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
You are on page 1/ 26
Binary Seayven + Aroay 's Sovted avy C) ot 2 3 ie See pee 5 108 8 findele © 2% CRamcet). oe ae Ses Return Tndexa 2 [aise Obs mes) Smalley Ahan Greterv T i mid ve Hhan mid end am mid= Stexttend = pase, a = Our target lies in Left of mid eoil now Check Left part nO: @nd = mid -1 [2 ene? fis Stont 4 Er! Mids SnGeaeay Oca Se yes! =) | “3 az Wwe Found answer: Prog: Us; : a Using Binary Search We eliminate Bat Fin tt SCS) of Ber] Teenof™. Comp = '0q.9| , peeudeC one — Teper co int end = Size-1; While Cs tart Ze end) < int wid = starvt tend 2 if Cele == ary Cmidj) Yeturn mid ; else if Cele < axv Genre end = mid-i5 else Stavt = mid +15 yeturn -1- Ii% we donot gef elem a Note : Fancy Way to waite mid- Int | mid = Sbevt + end-start a Why 7D if start G end atave too bige Sey 109 then anint mid = Geen - 7a will oveyflew the yange of int. Sy Descending Sovted Arevasy Mec): 20 17 15 i4 13 12 10 9) some target 14 Be Sen Intuition - Artereuy is Sovted in cvevevse ovdey — Se the change thet we voll domes move to right if [mid] < > tovget move to left iF [mi] < target A eee 9 1T 1s wy 13 ie 3° 3) 3 oa Cl : ood * i Start mid = § ee a arx Lmid] 7 target | $0 moveto Tight - Start = mid +1 & ‘ 7 3 zyion® 12.8 8 t , ) S mid e = WS + 3-5 z 2 ave Cmid) 7tav get Start = mid+\ Teevation 2 3 3 e I ry Lrnid] 7tayge ! 1 S)mid e Stovt = mid+] mid = 3 + 49-8 Pseudo Code: int Start =O int end = Size -) while C stayt <= end) int mid = start + i Carry [mid] == tag teturn mid; else if Cary Crd > tora Start = mid +35 else, ‘ end = md -} i return —\ 5 Figrst and Last 4» O " of Element. lo 1 oA luc, i Nowe: 2 Te! Pied ~ Payee Sicc eee 7 We will Cismaroerr le, 7? When in (Sam We will get the el We will stove it in and Chonge, = errlaee mid - > This change WNL Find the the element mid aa IS present Wele= anaes not: And 9 Element is Found change Value of Variable lueey index: ey ? To Find Last Occuvance | 7 Rest all “things will be sa as finst Occuvance , just on thing will change, * instead of “end we will 4 ' Change. tstervt -omid +1 | “80 We will have to make 2 While Loop ™e for Pivst ocurance 6 other fy eee a O(log ny Pseudo Useugo Code \- int start =o int end = Size-| int First Occ = - While (gtavb <= ee 5, eae Ctorget == avy[Emid]) First Oce = mid @nd = mid-i elseif ( target < avyEmid}) end = mid-J Ee Stayt = mid+) 3 int stavt =O int €nd = size-| int Last Oc = —1 While ( Stayt <= end) t iF (target = ory CmidJ) LastO« = mid Stayt = mid +1 else if Charge? < avatmid Dy} end = mid-\ else stark found Pivst Oce & Last Oco in Previous Problem. So, Count = LastOcc - Fist Oce +1 Loe Gy How many times A Sorted Array Is Rotated - ove a leis 1 7 Sie quee Sntuition : Novmal 9 t Ce i ie ‘a 7 aap fa]}sle 3 ]u]i2|is ['8) min ele index 20 Ree esta Set on: SS Pee in, [pe yisfiete[s c[2 | J min ele index = 4 menos time — Trdex of Pee) rin cle | ae) ele So if we find Min, Index de We will Fed th get the Ans %® How tocheck ele is min pay net in te 61S) = 5 (ae 7 ele is a Smalley than @ Both Neighbour ary [mid-1] > arvfmid] < aryv{mid+] | —@ x min cle wit ba always present Ue part of array « ex = (a 2: | ee 1 ; : Start mid- ele Sill Sorted. Also Note: start ele IS greater [| —— © than mid ele- Algo: © Appy normal binovy search ® Find mid @ check mid is min oy not Oar Emmis 1e<: 5 40% Comid +) 2 ae Next avy Emidl] < avy [mid +N-1) XN] oNEaed 1€ true veturn Teakex mid - @ rf false: Move to ungonted part of ary igi @ors (store 5 cal CmidJ ) Stort = mid +1 3 elae if ( ov7> (risa) ae cend3) end = pale eae «ah Pseudo Code :— int gtavt = 0 int end. izes while Cstayvt <= mid) t int mid = start + end start a2 int Next =(mid+!) 7N int Paev = (mid+ N-!) JN if ( ary Cmid]} jo 2°e Scum Tntution:— sorted word is used so we will Use. Binary searh for sure: BS Modified Bs (Sosted ) (Nearly Sorted ) er - end. mid stet end Start | Se i a end =m es =mid+! onl 4 ont = Ne ete cmid-2 Mele tm index i an 141 index if (ele == ary [mia]) Jif Cele == ony mid) elseif +__— = aa) (ele == ary (mid -) (ele == ary Cmid +i) To move left 2 ae . end =mideal Teo move Right Tomove Rignt Start = mid +} Start = mid +2 Teeude Code , int Left <0 Int Right= Size-\ while (Left <2 Rignt)_, fecois ioe if Cele == a(midJ) dua mid FC mid r= stort $4 a[mil-i] == ele) creturn mid-! if Crmid é= end 4 ov [mid+i]= = ele) qeturn mid+) if ( ele < ary Cmid7) end = mid -| else stavt =mid+) By Find Flooy OF Element In Sorted Paes Bagel: 0) 36" ge jo) fon ce mie = 5 — Fleoy of 5 = 5S ov greatest element less than S. Battion:-O.2¢ will opoty BG. fow the ele- @¢ if the ee mid is less than ele Se we will stove it In @ variable- @ When We will be out of while loop. Vaviable will have our ans. G Sie toe Soa s ‘6 eS Example : eae | yo | dean mn O°” Oech i tT iS mid én} OMS ari{mid] 7 ele. : Move left. Saet li : | 1 3 4 5 mid 3 any (mid J < ele. ie potential Candidate Foy Sion GNSS ]T2\q - Move Tight - avy C3 2) avy Cmid) D 3 Orer Oey : S35€, mid - avy Cmid] ele) Ons = arva{mid]; Stayt= mid +15 else 6nd = mid-1; Vetuan SNS Qy Find Cell oF Clement in Sorted Array av (J: peta aghe iS 16 Ne bai iS ee. 5 _ [ext of 5 = Smallest cle Gredeqi nanmee (ola Equal to £ Heve the potertal candidates ave 8 1010 1219 Srnaliest among them ““.e. 8 coll teauencreee Entuitien ; Just one change From ta Lloow problem. we will Stove the ele gteatey than 5 in ans variable. feadatode .- While Ci start <= end) Fe iF Cary emid) -= ele) Yeturn anid. Ova Emid]: Ase F C awmCmidy < key) Start = mid +1 ; else Ans = ava Cit d ] end = mid-1; aeturn Ans, LO: Gi Next Alphabetical Element OnE]: «a Wy omen et q ib, key =f, We need to-~veturn | next element ia ooray) This que is Similay §° find Ceil discussed previeushy- Bik Key So when we will find ef in 8S te then web will metumm next index of Prd if ee cle is greeter than Key stor Pseudo Code, a, it in © Variable - while ( stort <= end) iy cy oe = Lif Cary fmid] == key) Meturn mid+i; else 1f (any Cmidd < Key) Stayt = mid +15 else Ans end mid mid -| i, <4 3 cretury Ans; 4) Find Position OF Ele in Infinite } Sovted Bomrays BicJ° '? 34s 6 7 § ae te Sa 2? Diffevence bet” nogmal 8S and ether this pvoblem iS we dont have end ele. “ We cannot do- int end 5 see 6S Modified 8S. Ooo 0 Oo OOS oe r oon ¢ stort end Start end 99 We dont Know end- x How to find Wend index 2 — > tets mark end index at start tl position. PPO Oo... it Stat end Example : Key =7 % ' a 3 4 s c 7 s 4 Re me 345 6 7 8 9 jou im ft Start ond Now Compare, “key? and ‘are Cend J if Key 7 avy Cend J Le Tap ge Start=end g end = end** ¢ Sree!” 3 Algo :- key > brent) S=6 Quer > jeomae ys co we 3 T fe Ss eC Key 7 are Cend] Gae aes Mee 4 5G Ju te i ie e key < aaa Cend] Apply RBS on (, e) @ initicde ~ stavt =o “end =| @iké (orn (end) < Key) Start = Bend @nd = end #2 @ Now qany fend] > key Apply BS on ( Start > endl) of aro) « Pseudo Code :- int start =0 ine endel While ( Key > ave [end] ) & Start = end; end = Crndi * 255 BS (Oc, sche y end); ESI Sy Find Index of ‘1’ in Binary Sorted Array. Avaay i§ 0. Sl : @his “ausin. ombined” of Sexrch acon and Find st occurance. Pseudo code: int gtart <0 intend =1 While C Key 7 avy Cend3) () Canes) & start send; @nd = End #2 int Fixst -0C6 =-15 Remen@sen: <= end) _miaeStPE) Be Cerget = od) Piyst oc = ravd fais end = mid—15 Occuran ce else # (target < avy Emd]) end =mid-1} else i stavt =midt) | 5 aduen Finest Oce 5 MM. Tyee ect enone Aen yy Mimi Difhewence Element im Sorted Arvay oy Minimum mei at Ge AO) key: 7 _— 4) | 6 io | er gee aL aa 3 (aes mae a Min diff if Key iS present LF Key is not present Key = \2 fa [712 1/3 8 | to] sig J 7 a jo: {12 TOS Sa ose ce Ss ae BO): 3) Mere 2 3 il niin min diff dH is giver oy ct Apply Simple BS neorest elernern § Return ele. of a: a * How to fing them 7 Tf we apply BS and key is not present gtaxt | erd will point to neavest elements: . 1 Q 3 4 \ 3 3 10 15 Key = 412 t r i S m fe « 5 4 % Oo aas (gz @) Pseudo Code :- Apply BS qeturn er (a key m SI Peak Element amt): § [ORO Ome 11S) Bee Preak element Is one greater thon its Neighbours - Ex. 16 wm I6S5 10 < a6 XK not possible - Ex: 20 > 15 = Qo § Pear Element Door N10 Breas! 10:1 9-0 Ay. AO. So So > lo Yit last element i “+ Peak. Sienilanly Gay tJ: SO 4o 20 30 40 i Peak ele Antuition --9 while performing BS check mid with mid +1 & midi jg mid is qreadey than both mid is peak @ IP mid is not peak Then theve iS charce thot ele nearby an greater than mad can be Pear - ICME Grolcl ees el es Ne chance & mide-l voll be pear. mid < mid+1 e _ Chance of mid +1 Can be peas - Move Right - —— * otart = mid a Sroid < noel end =mid -1; Basically Jo bhi sbades Pr 0 Code: bow = O grease while ( low <=high) t int rod = low + [ughleo) pe jaa 2 (mid zoo ee Sree) element a if Ce mid] 7 avy{mid-1] gery Timid] > avy Emid +i] yetumn mid; Clse if Cary Emid] < ary [mid-)) high = mid -| else low = mid +15 3 } else if (mid = =0) Lif Cory fe] > avy £19) return 0; else aeturn 13 3 elge if (mid =< size-!) rk iF Coma Csize- i) 7 av (size-2)) yeturn Size =1; e\se aeturn gite-2 5 Gy Search In Bitonic amv]: “| 9S) sane aa Key 2 4 ——~ Intuition !- | 93% ¢ 3) Osman =. a Gers peak This Q is combinasion of Zep © Finding Pecuc ® Seay ch in Sorted array ® Search in Descending 5 Res (4 Sovted Avvay Apply these 3 problems 'e S while lox Te= o(legn)

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy