Note: <br>abla O(\, ext{log} \, N)</p></li></ul></li></ul><h3id="4e1eb666−1a32−442d−bd9f−d6b935bc033e"data−toc−id="4e1eb666−1a32−442d−bd9f−d6b935bc033e"collapsed="false"seolevelmigrated="true">BinarySearch(Recursive)</h3><ul><li><p>Recursiveversion:BinarySearch(values,key,low,high)</p><ul><li><p>ifhigh<low:return−1</p></li><li><p>mid=(high−low)/2+low</p></li><li><p>ifvalues[mid]<key:returnBinarySearch(values,key,mid+1,high)</p></li><li><p>elseifvalues[mid]>key:returnBinarySearch(values,key,low,mid−1)</p></li><li><p>else:returnmid</p></li></ul></li><li><p>Call:BinarySearch(values,key,0,size(values)−1)</p></li></ul><h3id="4f8812be−1d33−497f−a269−963323d52e7f"data−toc−id="4f8812be−1d33−497f−a269−963323d52e7f"collapsed="false"seolevelmigrated="true">OtherSearchTechniques(aslistedintheslides)</h3><ul><li><p>InterpolationSearch:heuristicthatusesthevaluedistributiontoguesswherethekeymightbe(detailsnotspelledoutinthetranscript).</p></li><li><p>JumpSearch:jumpaheadbyfixed−sizeblocksandthenlinearsearchwithintheblock(detailsnotspecifiedinthetranscript).</p></li><li><p>ExponentialSearch:expandsarangeexponentiallytolocateablockwherethekeymayreside(detailsnotspecifiedinthetranscript).</p></li><li><p>Note:Thetranscriptexplicitlylabelsthesestrategiesbutprovideslittletonocomplexitydetailsbeyondnames.</p></li></ul><h3id="a9e126c0−30aa−4637−9141−212d1168389b"data−toc−id="a9e126c0−30aa−4637−9141−212d1168389b"collapsed="false"seolevelmigrated="true">Sorting:DivideandConquerandRecursionConcepts</h3><ul><li><p>Theme:manysortingalgorithmsusedivideandconquer,oftenviarecursion,thencombinesub−solutions.</p></li><li><p>Visualmetaphorontheslideshighlightsbuildingupfromsmallerpieces(e.g.,cutting,merging,orpartitioningtreasuremaps).</p></li><li><p>Coresteps(DivideandConquer):</p><ul><li><p>Dividetheproblemintosmallersubproblemsofthesametype(ideallyroughlyequalinsize).</p></li><li><p>Conquerthepiecesrecursively,downtoasimplebasecase.</p></li><li><p>Combinethesub−solutionstoformtheoverallsolution.</p></li></ul></li><li><p>Efficiencycomesfromreducingproblemsizeateachdividestep.</p></li></ul><h3id="e68c87ba−5d8f−4804−968c−acd36eaf76f3"data−toc−id="e68c87ba−5d8f−4804−968c−acd36eaf76f3"collapsed="false"seolevelmigrated="true">InsertionSort</h3><ul><li><p>Buildsasortedlistoneitematatime.</p></li><li><p>Itistheonlyquadraticruntimesortshownasapractical,non−educationalexampleinproductioncontexts(perslide).</p></li><li><p>Pseudocodeconcept:</p><ul><li><p>fori=1toarray.size−1:</p></li><li><p>j=i</p></li><li><p>whilej>0andarray[j]<array[j−1]:</p><ul><li><p>swaparray[j]witharray[j−1]</p></li><li><p>j=j−1</p></li></ul></li></ul></li><li><p>Complexity:</p><ul><li><p>Bestcase:O(N)</p></li><li><p>Averagecase:O(N^2)</p></li><li><p>Worstcase:O(N^2)</p></li></ul></li></ul><h3id="885e1b3f−c8b1−4264−9992−2eca4bff5aab"data−toc−id="885e1b3f−c8b1−4264−9992−2eca4bff5aab"collapsed="false"seolevelmigrated="true">MergeSort</h3><ul><li><p><strong>Concept:</strong>dividethearray,sorthalves,andmerge.</p></li><li><p><strong>Recurrence:</strong>T(n) = 2T(n/2) + O(n)</p></li><li><p><strong>Timecomplexity:</strong>O(n \, log\, n)inallcases</p></li><li><p><strong>Keyoperation:</strong>Mergetwosortedhalvesinlineartime.</p></li><li><p><strong>Mergeprocedure</strong>(conceptual):</p><ul><li><p>Merge(left,right):mergetwosortedsubarraysintoasinglesortedarray.</p></li><li><p>Examplestructure:whileleftPos<=leftEndandrightPos<=rightEnd:compareandcopythesmallerelement;thencopyanyremainingelementsfromeitherside.</p></li><li><p>Finallycopymergedelementsbackintotheoriginalarray.</p></li></ul></li><li><p><strong>MasterTheoremapplication</strong>(MergeSort):a=2,b=2,f(n)=Θ(n)⇒Case2⇒T(n)=Θ(nlogn).</p></li><li><p><strong>Recursiontreevisualization</strong>:costperleveldoublessubproblemswhiletotalworkperlevelremainsΘ(n),yieldingΘ(nlogn)total.</p></li></ul><h3id="5605d907−4c45−4746−a6aa−dc42a9a0575d"data−toc−id="5605d907−4c45−4746−a6aa−dc42a9a0575d"collapsed="false"seolevelmigrated="true">Quicksort</h3><ul><li><p>Coreidea:chooseapivot,partitionthearraysothatvalueslessthanthepivotgoleftandvaluesgreatergoright.</p></li><li><p>Recursivelysorttheleftandrightpartitions.</p></li><li><p>Noseparatemergestep;orderingemergesfrompartitioning.</p></li><li><p>Pseudocode(partition):</p><ul><li><p>Partition(array,low,high):</p></li><li><p>mid=low+(high−low)/2</p></li><li><p>pivot=array[mid]</p></li><li><p>done=false</p></li><li><p>whilenotdone:</p><ul><li><p>whilearray[low]<pivot:low=low+1</p></li><li><p>whilepivot<array[high]:high=high−1</p></li><li><p>iflow>=high:done=true</p></li><li><p>else:swaparray[low]witharray[high]</p></li><li><p>low=low+1</p></li><li><p>high=high−1</p></li></ul></li><li><p>returnhigh//lastindexoflowpartition</p></li></ul></li><li><p>Quicksortfunction:</p><ul><li><p>Quicksort(array,low,high):</p></li><li><p>iflow>=high:return</p></li><li><p>endLow=Partition(array,low,high)</p></li><li><p>Quicksort(array,low,endLow)</p></li><li><p>Quicksort(array,endLow+1,high)</p></li></ul></li><li><p>Complexity(average/bestvsworst):</p><ul><li><p>Best/Averagecase:O(n \, log n)</p></li><li><p>Worstcase:O(n^2)</p></li></ul></li><li><p>Recurrenceapproximation(viaMasterTheorem):best/average T(n) = O(n \, log n),worst T(n) = O(n^2)</p></li></ul><h3id="ca75f20a−856c−4c50−8864−74e61303ee76"data−toc−id="ca75f20a−856c−4c50−8864−74e61303ee76"collapsed="false"seolevelmigrated="true">MergeSortvsQuicksort:Comparison(high−level)</h3><ul><li><p>Algorithmtype:botharedivide−and−conquer.</p></li><li><p>Worst−casetime:MergeSortO(n \, log n);QuicksortO(n^2)(worstcase).</p></li><li><p>Average−casetime:bothapproxO(n \, log n)(Quicksortistypicallynearthisinpractice).</p></li><li><p>Best−casetime:bothO(n \, log n)(fortypicalwell−behavedinputs).</p></li><li><p>Spacecomplexity:MergeSortusesO(n)auxiliaryspace;QuicksortusesO(\,log n)stackspaceonaverage.</p></li><li><p>Stability:MergeSortisstable;Quicksortisnotstableunlessmodified.</p></li><li><p>In−place:MergeSortisnotin−placebydefault;Quicksortisin−place(intypicalimplementations).</p></li><li><p>Usecases:MergeSortforlargedatasetsandlinkedlists;Quicksortforin−memoryarraysandgeneral−purposesortinginlibraries.</p></li><li><p>Libraryusageandimplementationconsiderations:oftenusedinparallel/distributedcontexts(Merge)versusin−memory,general−purposesorting(Quicksort).</p></li></ul><h3id="57c8a428−4825−483d−90dd−380a43825c06"data−toc−id="57c8a428−4825−483d−90dd−380a43825c06"collapsed="false"seolevelmigrated="true">RadixSort</h3><ul><li><p>RadixSortprocessesdigitsfromleastsignificanttomostsignificant(LSDfirst).</p><ul><li><p><em>(LSDtheleastsignificantdigitisthenumberfarthesttotheright)</em></p></li><li><p><em>(MSDthemostsignificantdigitisthenumberfarthesttotheleft)</em></p></li></ul></li><li><p>Stablesub−sortingperdigitplace(e.g.,countingsort)isusedtopreservetheorderofdigitsalreadysorted.</p></li><li><p>Doesnotrelyoncomparisonsbetweenkeys;reliesondigitbucketing.</p></li><li><p>Bestforuniform,fixed−lengthkeys(e.g.,largelistsofintegersorstringswithconsistentkeylength).</p></li><li><p>Keycomponents:</p><ul><li><p>RadixGetLength(value):returnsnumberofdigitsinvalue(atleast1for0).</p></li><li><p>RadixGetMaxLength(array):maximumnumberofdigitsamongallelements.</p></li><li><p>RadixSort(array):uses10buckets(forbase−10)anditeratesoverdigitpositionsfromleasttomostsignificant,rebuildingthearrayaftereachpass.Alsohandlesnegativenumbersbyseparatingnegativesandnonnegatives,reversingnegatives,andconcatenating.</p></li></ul></li><li><p>High−levelpseudocode(outline):</p><ul><li><p>Buckets=array[0..9]ofemptylists</p></li><li><p>maxDigits=RadixGetMaxLength(array)</p></li><li><p>pow10=1</p></li><li><p>fordigitIndex=0tomaxDigits−1:</p></li><li><p>fori=0toarray.size−1:</p><ul><li><p>bucketIndex=abs(array[i]/pow10)O(n \log_{10}(k)).</p></li></ul></li><li><p>Practicaltakeaway:RadixSortcanoutperformcomparison−basedsortsonlargedatasetswithfixed−lengthkeysbutrequiresextraspaceandcarefulhandlingofnegatives.</p></li></ul><h3id="5328dd2f−a016−4cc5−a349−ac7a71704d37"data−toc−id="5328dd2f−a016−4cc5−a349−ac7a71704d37"collapsed="false"seolevelmigrated="true">SortingAlgorithmComparisonChart(summarypoints)</h3><ul><li><p>BubbleSort:BestO(n);AverageO(n^2);WorstO(n^2);Stable:Yes;In−place:Yes.Notes:educational;notusedinpractice.</p></li><li><p>SelectionSort:BestO(n^2);AverageO(n^2);WorstO(n^2);Stable:No;In−place:Yes.Notes:alwaysdoesn2comparisons.</p></li><li><p>InsertionSort:BestO(n);AverageO(n^2);WorstO(n^2);Stable:Yes;In−place:Yes.Notes:greatfornearly−sorteddata;conceptuallyabridgetoMergeSort.</p></li><li><p>ShellSort:BestO(n \, ext{log} n)^* roughlyO(n^{1.3});AverageO(n^2);Stable:No;In−place:Yes.Notes:fasterthaninsertionbutnotascommonlyused;notstable.</p></li><li><p>MergeSort:Best/Average/WorstO(n \, log n);Stable:Yes;In−place:No.Notes:recursive;requiresO(n)space;excellentperformance.</p></li><li><p>QuickSort:Best/AverageO(n \, log n);WorstO(n^2);Stable:No;In−place:Yes.Notes:veryfastinpractice;pivotstrategyiskey;notstablebydefault.</p></li><li><p>HeapSort:Best/Average/WorstO(n \, log n);Stable:No;In−place:Yes.Notes:basedonheap;goodperformancebutnotstable.</p></li></ul><h3id="ca0d0737−14a4−45d2−b9de−3e9331551db6"data−toc−id="ca0d0737−14a4−45d2−b9de−3e9331551db6"collapsed="false"seolevelmigrated="true">NotesonMasterTheoremandRecurrenceRelations</h3><ul><li><p>Recurrenceformfordivide−and−conqueralgorithms:</p><ul><li><p>T(n)=a⋅T(n/b)+f(n)</p></li><li><p>a:numberofsubproblems</p></li><li><p>b:factorbywhichtheproblemsizeisreduced</p></li><li><p>f(n):timetocombinethesub−solutions(theworkoutsiderecursivecalls)</p></li></ul></li><li><p>MasterTheoremapplicability:</p><ul><li><p>UsedtosolverecurrencesoftheformT(n)=aT(n/b)+f(n).</p></li><li><p>Computenlogbaandcomparewithf(n)todeterminethecase.</p></li></ul></li><li><p>Thethreeclassicalcases(informal):</p><ul><li><p>Case1:f(n)=O(nlogba−ε)forsomeε>0</p></li><li><p>ThenT(n)=Θ(nlogba)</p></li><li><p>Case2:f(n)=Θ(nlogbalogkn)forsomek≥0</p></li><li><p>ThenT(n)=Θ(nlogbalogk+1n)</p></li><li><p>Case3:f(n)=Ω(nlogba+ε)forsomeε>0,andregularityconditionholds(oftenwrittenasaf(n/b)≤cf(n)forsomec<1andsufficientlylargen)</p></li><li><p>ThenT(n)=Θ(f(n))</p></li></ul></li><li><p>HowtoapplyMasterTheorem(workflow):</p><ul><li><p>Identifya,b,f(n)intherecurrence.</p></li><li><p>Computenlogba.</p></li><li><p>Comparef(n)withnlogba(andanylogfactors)toselectthecorrectcase.</p></li><li><p>ConcludeT(n)fromthecase.</p></li></ul></li><li><p>Classicexamples:</p><ul><li><p>MergeSort:T(n)=2T(n/2)+Θ(n)→a=2,b=2,f(n)=Θ(n)→Case2→T(n)=Θ(nlogn).</p></li><li><p>BinarySearch:T(n)=T(n/2)+Θ(1)→a=1,b=2,f(n)=Θ(1)→Case2→T(n)=Θ(logn).</p></li></ul></li></ul><h3id="d9b1fe6a−652c−4bf6−b7ec−d9f4ac71301e"data−toc−id="d9b1fe6a−652c−4bf6−b7ec−d9f4ac71301e"collapsed="false"seolevelmigrated="true">RecurrenceExamplesandMasterTheoremFlow</h3><ul><li><p>MasterTheoremFlowchart(conceptual):</p><ul><li><p>Iff(n)=O(nlog<em>ba−ε)forsomeε>0→Case1→T(n)=Θ(nlog</em>ba)</p></li><li><p>Elseiff(n)=Θ(nlog<em>balogkn)→Case2→T(n)=Θ(nlog</em>balogk+1n)</p></li><li><p>Elseiff(n)=Ω(nlogba+ε)andregularityconditionholds→Case3→T(n)=Θ(f(n))</p></li><li><p>ElseMasterTheoremdoesnotapply</p></li></ul></li></ul><h3id="313e3b22−8c38−46be−98c2−2a16054d47ee"data−toc−id="313e3b22−8c38−46be−98c2−2a16054d47ee"collapsed="false"seolevelmigrated="true">MasterTheorem:WorkedCaseExamples(conceptual)</h3><ul><li><p>Case1exampleoutline:T(n)=2T(n/2)+Θ(1)</p><ul><li><p>a=2,b=2,f(n)=Θ(1)→nlog<em>ba=nlog</em>22=n1=n</p></li><li><p>Sincef(n)=O(n1−ε)withε=1,Case1applies?(Note:thestandardinterpretationyieldsT(n)=Θ(n),buttheslideframingiscontext−dependent;thekeytakeawayisusingthecomparisonwithnlogba.)</p></li></ul></li><li><p>Case2exampleoutline:T(n)=4T(n/2)+Θ(n2)</p><ul><li><p>a=4,b=2,f(n)=Θ(n2)→nlog<em>ba=nlog</em>24=n2</p></li><li><p>f(n)=Θ(nlog<em>ba)→Case2withk=0→T(n)=Θ(nlog</em>balogn)=Θ(n2logn)(asperthegeneralform).(Theslideusesavariant;theessentialpointisthatequalitywithalogfactorescalatestoCase2intheMasterTheoremframework.)</p></li></ul></li><li><p>Case3exampleoutline:T(n)=2T(n/2)+Θ(n2)</p><ul><li><p>a=2,b=2,f(n)=Θ(n2)→nlogba=n</p></li><li><p>Sincef(n)growsfasterthannlogba,Case3appliesgiventheregularitycondition,yieldingT(n)=Θ(f(n))=Θ(n2).</p></li></ul></li><li><p>Finalnotes:TheMasterTheoremprovidesastructuredwaytodeduceasymptoticgrowthfordivide−and−conquerrecurrences,withMergeSortandBinarySearchascanonicalexamples.</p></li></ul><h3id="08bcfd81−aae9−473c−bdd0−a695d2ff37f8"data−toc−id="08bcfd81−aae9−473c−bdd0−a695d2ff37f8"collapsed="false"seolevelmigrated="true">PracticalTakeaways</h3><ul><li><p>Whenchoosingasortingalgorithm,consider:</p><ul><li><p>Inputsizeandcharacteristics(random,nearlysorted,linkedlists,in−memoryarrays).</p></li><li><p>Stabilityrequirements.</p></li><li><p>Memoryconstraints(in−placevsextraspace).</p></li><li><p>Worst−casevsaverage−caseperformance.</p></li></ul></li><li><p>RadixSortoffersanon−comparison−basedalternativewithpredictablelinearpassesincertaincontexts,particularlywithfixed−lengthkeys,butrequiresextraspaceandhandlingofsigns.Overallcomplexityscaleswiththenumberofdigitsofthelargestkey:O(n \, d) = O(n \, ext{log}_{b}(k))forbasebandlargestkeyk.</p></li></ul><h3id="d68b5da7−e0e0−43fa−9496−aea8e3c6e566"data−toc−id="d68b5da7−e0e0−43fa−9496−aea8e3c6e566"collapsed="false"seolevelmigrated="true">QuickReference:KeyNotation</h3><ul><li><p>Timecomplexities:O(\,),
abla $$ (log base 2) denotation in context, and variations by base as needed.