teach a level compu/ng: algorithms and data …...week 3: sor*ng and searching 14/03/2017 1 teach a...
TRANSCRIPT
Week3:Sor*ngandSearching 14/03/2017
1
Teach A level Compu/ng: Algorithms and Data
Structures
EliotWilliams
@MrEliotWilliams
Course Outline
1 Representa+onsofdatastructures:Arrays,tuples,Stacks,Queues,Lists
2 RecursiveAlgorithms
3 SearchingandSor*ng-EWwillbelate!
4 HashingandDic*onaries,GraphsandTrees
5 Depthandbreadthfirstsearching;treetraversals
Week3:Sor*ngandSearching 14/03/2017
2
Searching, sor/ng and algorithm analysis
Algorithm Analysis
• Analgorithmisaself-containedsequenceofac*onstobeperformed
• Wecananalysisanalgorithmsefficiencyusingthefollowingmeasures
• The*meittakestofinish• Howmuchmemoryitneeds
Week3:Sor*ngandSearching 14/03/2017
3
Measuring /me
• FirsTme.py
• Runtheprogramhowlongdothesimpleexpressionstake• Changethenumbersandopera*onsinthefirstthreestatements
• Uncommentthefurtherstatements(eachblockata*me)
Measuring /me
• FirsTme.py
• Differentcomputerswillproducedifferent*mes• Timetakentoexecuteacommandisnotequalonthesamecomputer• Timetakentoexecuteasequenceofcommandsgrowswiththelengthofthesequence
Week3:Sor*ngandSearching 14/03/2017
4
We don’t need absolute /me measures • Wewanttounderstandfundamentalproper*esofouralgorithms,notthingsspecifictoapar*cularinputormachine.
• Wethereforeonlycareabouthowthe*meincreases• Maybethe*mestaysthesame• Maybedoublingthesize,doublesthe*me• Maybedoublingthesize,morethandoublesthe*me
AnyComputer
Problem(sizeN) Time:T1
Problem(size2*N) Time:T2
Big O Nota/on
• BigONota*onisaformalwayofsta*nghowthe*metakenbyanalgorithmrelatestoaninputsize
• Trysecond*me.py• andsecond*men.py
• O(1)inputsizedoesnoteffectthe*me• O(n)inputgrowslinearlywithinputsize
Week3:Sor*ngandSearching 14/03/2017
5
Big O Nota/on
• BigONota*onisaformalwayofsta*nghowthe*metakenbyanalgorithmrelatestoaninputsize–weonlyneedtoconsiderthelargestterm
• O(1)inputsizedoesnoteffectthe*me• O(logn)logarithmicgrowth• O(n)*megrowslinearlywithinputsize• O(n2)*meisdirectlypropor*onaltothesquareoftheinputsize• O(ny)Exponen*algrowth• O(2n)polynomialgrowth–intractableproblems….
O(n)
O(logn)
O(n2)
O(1)
Week3:Sor*ngandSearching 14/03/2017
6
Search: The Problem • Findatargetvalueinthelist• Isitthere?• Ifso,atwhatindex?
• Target=41,foundatindex4• Target=27,notfound
01234567891011
173152194134761128924461
Linear Search
• Idea:lookateachentryinturn• Steps• Startatindex=0• IsArray[Index]equaltothetarget?• Ifyes,stop;otherwiseincrementtheindex• Stopwhenindexisonelessthanthelength
0123456789
1731521941347611289234Target
Index
0
Week3:Sor*ngandSearching 14/03/2017
7
Exercise 1.1 and 1.2
Linear Search – Algorithm • Algorithminpseudocode• ArrayisA
index = 0 !while index < length of array! if A[index] equals target! return index! index = index + 1!return -1 to show not found !
for i is 0 to length of array-1! if A[i] equals target! return i!return -1 to show not found !
Week3:Sor*ngandSearching 14/03/2017
8
Binary Search Searchingasortedlist
Searching a Sorted List
• Ques*on:whyarebooksinthelibrarykeptinorder?
Week3:Sor*ngandSearching 14/03/2017
9
Searching a Sorted List
• Ques*on:whyarebooksinthelibrarykeptinorder?
• Inanorderedarray,wedonothavetolookateveryitem• “Beforethisone”• “Aperthisone”• …quicklyfindthecorrectloca*on
• Whatisthebestalgorithmforlooking?
Binary Search – Sorted Lists • Whichhalfisitin?Lookinthemiddle.
0123456789
1117192831344152769241Target
Index
4
7
6
Week3:Sor*ngandSearching 14/03/2017
10
Binary Search – Algorithm • Keyidea:inwhichpartofthearrayarewelooking?
lep right
Binary Search – 3 Cases
• Case1:Xequalstargetvalue• Case2:X<targetvalue
• Case3:X>targetvalue
lep rightmidX
rightlep
lepX
right
Week3:Sor*ngandSearching 14/03/2017
11
Binary Search – Algorithm left = 0!right = length of array!while right > left:! mid = average of left and right! if A[mid] equals target! found it at 'mid'! if A[mid] < target! search between mid+1 & right! otherwise ! search between left & mid!return not found!
Binary Search – Python def BSearch(A, target):! left = 0! right = len(A)! while right > left:! mid = (left + right) // 2! if A[mid] == target:! return mid! elif A[mid] < target:! left = mid+1! else:! right = mid! return -1!
Week3:Sor*ngandSearching 14/03/2017
12
Binary Search – Complexity
• Numberofsteps=numberofbinarydigittoindex• O(logN)
1117192831344152
000
001
010
011
100
101
110
111
Binary Search (Recursive) • Searchasortedarray–isEinthearray?
• Basecase:• emptyarrayorEfound
• Recursivecase:• CompareEwiththemiddleelement• IfEsmaller,searchthelephalf• IfElarger,searchtherighthalf
Week3:Sor*ngandSearching 14/03/2017
13
Exercise 1.3 • Completetheitera5veandrecursivebinarysearchproceduresinbinarysearch.py
Sor/ng
Week3:Sor*ngandSearching 14/03/2017
14
Sor/ng: The Problem
• Arrangearrayinorder• Sameentries;inorder–swapentries
• Proper*es• Speed,space,stable,
0123456789
17315219413476112892
11171928313441527692
Discussion
• Sortapackoftoptrumps• Describehowyoudoit
Week3:Sor*ngandSearching 14/03/2017
15
Bubble Sort – Insight
• Librarianfindstwobooksoutoforder• Swapthemover!• Repeatedly
0123456789
17315219413476112892
Bubble Sort – Descrip/on
• Passthroughthearray(star*ngonthelep)• Swapanyentriesthatareoutoforder• Repeatun*lnoswapsneeded
0123456789
17315219413476112892
Quiz:showarrayaperfirstpass
Week3:Sor*ngandSearching 14/03/2017
16
Bubble Sort – Algorithm • Sor*ngArrayA• Assumeindices0tolength-1
while swaps happen! index = 1! while index < length! if A[index-1] > A[index]! swap A[index-1] and A[index]! index = index + 1!
Exercise 2.1 Bubble Sort
• Completethetabletoshowthesuccessivepassesofabubblesort
Week3:Sor*ngandSearching 14/03/2017
17
Demo sor*ngDemo.py
Bubble Sort – Proper/es
• Stable• Inefficient• O(N2)• Doublelength–*meincreases4-fold
hvp://www.sor*ng-algorithms.com/bubble-sort
Week3:Sor*ngandSearching 14/03/2017
18
Inser/on Sort – Insight
• Imaginepartofthearrayisordered• Insertthenextitemintothecorrectplace
17315219413476112892
orderednotyetordered
17315219413411287692
orderednotyetordered
Inser/on Sort – Descrip/on
• Startwithoneentry–ordered• Takeeachentryinturn• Insertintoorderedpartbyswappingwithlowervalues• Stopwhenallentriesinserted
17315219413476112892
orderednotyetordered
Week3:Sor*ngandSearching 14/03/2017
19
Inser/on Sort – Algorithm • Sor*ngArrayA• Assumeindices0tolength-1
index = 1 !while index < length of array! ix = index! while A[ix] < A[ix-1] and ix > 0! swap A[ix] and A[ix-1]! ix = ix – 1! index = index + 1!
• A[0:index]ordered• Samevalues
Innerloop:insertintoorderedlist
Exercises 3.x
Week3:Sor*ngandSearching 14/03/2017
20
Quicksort – Insight
• Howcouldweshareoutsor*ngbetweentwopeople?• ChooseavalueV• Givefirstpersonallvalues<V• Givesecondpersonallvalues>V
• Whenthereisonlyasingleentry–itissorted
Quicksort Example 173152194134761128
281719113134765241
all<31 all>=31
11171928
1928
41345276
3441
Week3:Sor*ngandSearching 14/03/2017
21
Quicksort Descrip/on
• Chooseapivotvalue• Par**onthearray• Valueslessthanthepivottothelep• Thepivot• Valuesgreaterthanthepivottotheright
• Repeatprocessoneachpar**on• …un*lpar**onhasnomorethanonevalue
• Workdoneinpar++on
Tasks
• Quickfirststep.py–goingthroughonlyonce• quickS1
• Separateintotwonewlistsandputbacktogetherwithpivot• Checksbasicunderstanding
Week3:Sor*ngandSearching 14/03/2017
22
Recursive Sor/ng • Concept• Splitarrayintwohalves,sorteachhalf• Combinetwosortedarrays• Singleitemsorted(basecase)
• Twoalgorithms• Mergesort• Halvearray–mergetwosortedlists
• Quicksort• Par**onarray–combineeasy
Merge Sort – Insight
• Howcouldweshareoutsor*ngbetweentwopeople?• Halfitandsorteachhalf• Mergethetwosortedlists
• Whenthereisonlyasingleentry–itissorted
Week3:Sor*ngandSearching 14/03/2017
23
Merge Sort
1731521941347611
1731521941347611
1719315211344176
Decompose
1117193134415276
Merge
MAGICRecursion
Merging • Workdoneinthemerge
• Repeatedlyselectsmallest
17193152
11344176
171931 5211 344176
Week3:Sor*ngandSearching 14/03/2017
24
Proper/es
• Inser*onsort• O(N2)–sameasbubblesort• Stable
• Quicksort
• Moreefficient:O(NlogN)• Notstable
• Mergesort• O(NlogN)–sameasquicksortbutextraspace• Stable
hvp://www.sor*ng-algorithms.com/inser*on-sort
hvp://www.sor*ng-algorithms.com/quick-sort
hvp://www.sor*ng-algorithms.com/merge-sort