ebook img

Data Structures and Algorithms with Scala: A Practitioner's Approach with Emphasis on Functional Programming PDF

164 Pages·2019·1.36 MB·English
Save to my drive
Quick download
Download
Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.

Preview Data Structures and Algorithms with Scala: A Practitioner's Approach with Emphasis on Functional Programming

Undergraduate Topics in Computer Science Bhim P. Upadhyaya Data Structures and Algorithms with Scala A Practitioner’s Approach with Emphasis on Functional Programming Undergraduate Topics in Computer Science Series editor Ian Mackie Advisory Board Samson Abramsky, University of Oxford, Oxford, UK Chris Hankin, Imperial College London, London, UK Mike Hinchey, University of Limerick, Limerick, Ireland Dexter C. Kozen, Cornell University, Ithaca, USA Andrew Pitts, University of Cambridge, Cambridge, UK HanneRiisNielson,TechnicalUniversityofDenmark,KongensLyngby,Denmark Steven S. Skiena, Stony Brook University, Stony Brook, USA Iain Stewart, University of Durham, Durham, UK Undergraduate Topics in Computer Science (UTiCS) delivers high-quality instructional content for undergraduates studying in all areas of computing and information science. From core foundational and theoretical material to final-year topics and applications, UTiCS books take a fresh, concise, and modern approach and are ideal for self-study or for a one- or two-semester course. The texts are all authoredbyestablishedexpertsintheirfields,reviewedbyaninternationaladvisory board, and contain numerous examples and problems. Many include fully worked solutions. More information about this series at http://www.springer.com/series/7592 Bhim P. Upadhyaya Data Structures and Algorithms with Scala ’ A Practitioner s Approach with Emphasis on Functional Programming 123 Bhim P.Upadhyaya EqualInformation, LLC Sunnyvale,CA,USA ISSN 1863-7310 ISSN 2197-1781 (electronic) Undergraduate Topics inComputer Science ISBN978-3-030-12560-8 ISBN978-3-030-12561-5 (eBook) https://doi.org/10.1007/978-3-030-12561-5 LibraryofCongressControlNumber:2019930577 ©SpringerNatureSwitzerlandAG2019 Thisworkissubjecttocopyright.AllrightsarereservedbythePublisher,whetherthewholeorpart of the material is concerned, specifically the rights of translation, reprinting, reuse of illustrations, recitation, broadcasting, reproduction on microfilms or in any other physical way, and transmission orinformationstorageandretrieval,electronicadaptation,computersoftware,orbysimilarordissimilar methodologynowknownorhereafterdeveloped. The use of general descriptive names, registered names, trademarks, service marks, etc. in this publicationdoesnotimply,evenintheabsenceofaspecificstatement,thatsuchnamesareexemptfrom therelevantprotectivelawsandregulationsandthereforefreeforgeneraluse. The publisher, the authors and the editors are safe to assume that the advice and information in this book are believed to be true and accurate at the date of publication. Neither the publisher nor the authorsortheeditorsgiveawarranty,expressorimplied,withrespecttothematerialcontainedhereinor for any errors or omissions that may have been made. The publisher remains neutral with regard to jurisdictionalclaimsinpublishedmapsandinstitutionalaffiliations. ThisSpringerimprintispublishedbytheregisteredcompanySpringerNatureSwitzerlandAG Theregisteredcompanyaddressis:Gewerbestrasse11,6330Cham,Switzerland TotheReaders -Bhim Preface Professional software engineers find it difficult to make time to upgrade and rein- force their own knowledge; most of their time is spent on business responsibili- ties.Thisisalsotrueforengineeringstudents(amongothers),whohavenumerous coursestotakeandrigorousexercisestodo.So,timeisaconstraintforalmostev- erybody.Itisourobservationthatprofessionalsneedconciseinformationsourcein ordertobeabletoallocateandgetvalueoutoftheirtime.Inthiscontext,itishandy tohaveabookthatcanbereviewedinoneortwoweekends. A quick survey of books on Data Structures and Algorithms reveals that there are plenty of well-written books; however, they have multiple shortcomings. One that stands out is that most of those books were written a decade back. Computer scienceisoneofthefastest-ifnotthefastest-changingfields.Thereisasaying: everytimeyouturnyourheadyoumightseesomethingnew,whichistrue,specially withtheadvancesinautomatedbuilds.Theremightbethousandsofbuildsrunning orcompletingatagivenpointintime.Allthesuccessfulbuildscreatenewversions of existing software packages or new software packages. In this rapidly changing context,bookswrittenadecadeormoreagomaynotservethepurposebest. Anothershortcomingofwell-writtenclassicalbooksinthisfieldisthattheyare eithertoolongortootheoreticalforpractitioners.Findingweeksoffreetimetoread abookiscertainlychallengingforworkingsoftwareengineers,amidtheirstringent workresponsibilities.Also,too-theoreticalbooksmightbegoodforintellectualex- ercisesandresearchinthefield,butmaynotservewelltosolveappliedproblems quickly.Mostprofessionalengineerslookforterseandprecisepresentationofma- terial. ThethirdaspectisthattherearenotmanyDataStructuresandAlgorithmsbooks available for Scala. Scala is becoming popular in the big data space. Most senior Java-basedsoftwarejobs,thesedays,preferScalaproficiency.Inthiscontext,itis certainlyhelpfultobeequippedwithScalaimplementationsofpopulardatastruc- turesaswellasalgorithms.Mostoftheanalyticaltasksarebetterdonebythefunc- tional style of processing. As Scala is an object-functional language, we can do multi-paradigmprogramming.Scalaalsosupportspolyglotprogramming,Javabe- ingtheclosestsibling. vii viii Preface This book has been written more in a study-note or tutorial style and it covers ninepopulartopicsinDataStructuresandAlgorithms—arrays,lists,stacks,queues, hash tables, binary trees, sorting, searching, and graphs. Arrays are implemented in an imperative style with the famous matrix multiplication problem. Most of the othertopicsareimplementedinafunctionalstyle.So,ifyouareplanningtolearna functionalversionofDataStructuresandAlgorithmsinScala,thisistherightbook. I’vetriedtomakethebookasaccessibleaspossible. Mostoftheprogramsinthisbookarecompleteandrunningapplications.Also, each topic or subtopic has at least one complete and running example, which will save your time. You can try to come up with better implementations than in this book. Most of the challenge exercises are for this purpose. However, some are in- tendedtoreinforceyourunderstandingoftheexistingmaterial.Inadditiontocre- ativity,theabilitytotakesomebodyelse’ssolutionandimproveisdesirableinthe job market; I’ve tried to incorporate both in this book. Also the book includes so- lution to exercises so that you don’t struggle, specially when you are time-bound. HappyReading! Sunnyvale,California BhimP.Upadhyaya October2018 Contents 1 FoundationalComponents ...................................... 1 1.1 Arrays.................................................... 2 1.2 ListsandVectors ........................................... 6 1.3 AppliedTechniquesforEfficientComputation .................. 8 1.3.1 LazyEvaluation ..................................... 8 1.3.2 Memoization........................................ 10 1.4 Streams................................................... 11 1.5 SlidingWindows........................................... 16 2 FundamentalAlgorithms ....................................... 19 2.1 PrimeNumbers ............................................ 19 2.2 DecimaltoBinaryConversion................................ 21 2.3 DivideandConquer ........................................ 22 2.4 GreedyAlgorithms ......................................... 25 3 Arrays ........................................................ 27 3.1 Structure.................................................. 28 3.2 TypicalImplementation ..................................... 29 3.3 Analysis .................................................. 32 3.4 Application................................................ 32 4 Lists .......................................................... 35 4.1 Structure.................................................. 36 4.2 TypicalImplementation ..................................... 36 4.3 Analysis .................................................. 40 4.4 Application................................................ 41 5 Stacks ........................................................ 45 5.1 Structure.................................................. 45 5.2 TypicalImplementation ..................................... 45 5.3 Analysis .................................................. 48 ix x Contents 5.4 Application................................................ 48 6 Queues........................................................ 51 6.1 Structure.................................................. 51 6.2 TypicalImplementation ..................................... 52 6.3 Analysis .................................................. 55 6.4 Application................................................ 56 7 HashTables ................................................... 57 7.1 StructureandAlgorithm..................................... 57 7.2 TypicalImplementation ..................................... 58 7.3 Analysis .................................................. 66 7.4 Application................................................ 66 8 BinaryTrees................................................... 67 8.1 StructureandAlgorithms .................................... 67 8.1.1 Preorder............................................ 68 8.1.2 Inorder............................................. 68 8.1.3 Postorder ........................................... 68 8.2 TypicalImplementation ..................................... 68 8.3 Analysis .................................................. 73 8.4 Application................................................ 73 9 Sorting........................................................ 77 9.1 BubbleSort ............................................... 77 9.1.1 Algorithm .......................................... 77 9.1.2 TypicalImplementation............................... 78 9.2 SelectionSort.............................................. 79 9.2.1 Algorithm .......................................... 79 9.2.2 TypicalImplementation............................... 80 9.3 InsertionSort .............................................. 81 9.3.1 Algorithm .......................................... 81 9.3.2 TypicalImplementation............................... 82 9.4 MergeSort ................................................ 84 9.4.1 Algorithm .......................................... 84 9.4.2 TypicalImplementation............................... 84 9.5 QuickSort ................................................ 87 9.5.1 Algorithm .......................................... 87 9.5.2 TypicalImplementation............................... 87 9.6 ComparativeAnalysis....................................... 89 9.7 Application................................................ 90

Description:
This practically-focused textbook presents a concise tutorial on data structures and algorithms using the object-functional language Scala. The material builds upon the foundation established in the title Programming with Scala: Language Exploration by the same author, which can be treated as a comp
See more

The list of books you might like

Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.