Data Structures and Algorithms Annotated Reference With Examples Granville Barnett Instant Download
Data Structures and Algorithms Annotated Reference With Examples Granville Barnett Instant Download
https://ebookbell.com/product/data-structures-and-algorithms-
annotated-reference-with-examples-granville-barnett-54633802
https://ebookbell.com/product/data-structures-and-algorithms-with-
javascript-michael-mcmillan-47308410
https://ebookbell.com/product/data-structures-and-algorithms-in-c-4th-
edition-adam-drozdek-48951104
https://ebookbell.com/product/data-structures-and-algorithms-in-swift-
implement-stacks-queues-dictionaries-and-lists-in-your-apps-1st-
edition-elshad-karimov-50195754
https://ebookbell.com/product/data-structures-and-algorithms-analysis-
data-structures-based-on-linear-relations-xingni-zhou-zhiyuan-ren-
yanzhuo-ma-kai-fan-xiang-ji-51127738
Data Structures And Algorithms In Java Michael T Goodrich Roberto
Tamassia
https://ebookbell.com/product/data-structures-and-algorithms-in-java-
michael-t-goodrich-roberto-tamassia-53645826
https://ebookbell.com/product/data-structures-and-algorithms-in-c-
michael-t-goodrich-roberto-tamassia-53683882
https://ebookbell.com/product/data-structures-and-algorithms-
implementation-through-c-dr-brijesh-bakariya-54459320
Data Structures And Algorithms With Python 100 Coding Qa Code Of Code
Converted Yasin Cakal
https://ebookbell.com/product/data-structures-and-algorithms-with-
python-100-coding-qa-code-of-code-converted-yasin-cakal-54845986
Data Structures And Algorithms With The C Stl A Guide For Modern C
Practitioners 1 Converted John Farrier
https://ebookbell.com/product/data-structures-and-algorithms-with-the-
c-stl-a-guide-for-modern-c-practitioners-1-converted-john-
farrier-55889290
Data Structures and Algorithms
DSA
Annotated Reference with Examples
1 Introduction 1
1.1 What this book is, and what it isn’t . . . . . . . . . . . . . . . . 1
1.2 Assumed knowledge . . . . . . . . . . . . . . . . . . . . . . . . . 1
1.2.1 Big Oh notation . . . . . . . . . . . . . . . . . . . . . . . 1
1.2.2 Imperative programming language . . . . . . . . . . . . . 3
1.2.3 Object oriented concepts . . . . . . . . . . . . . . . . . . 4
1.3 Pseudocode . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
1.4 Tips for working through the examples . . . . . . . . . . . . . . . 6
1.5 Book outline . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
1.6 Testing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
1.7 Where can I get the code? . . . . . . . . . . . . . . . . . . . . . . 7
1.8 Final messages . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
I Data Structures 8
2 Linked Lists 9
2.1 Singly Linked List . . . . . . . . . . . . . . . . . . . . . . . . . . 9
2.1.1 Insertion . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
2.1.2 Searching . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
2.1.3 Deletion . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
2.1.4 Traversing the list . . . . . . . . . . . . . . . . . . . . . . 12
2.1.5 Traversing the list in reverse order . . . . . . . . . . . . . 13
2.2 Doubly Linked List . . . . . . . . . . . . . . . . . . . . . . . . . . 13
2.2.1 Insertion . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
2.2.2 Deletion . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
2.2.3 Reverse Traversal . . . . . . . . . . . . . . . . . . . . . . . 16
2.3 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17
www.dbooks.org
3.7.2 Postorder . . . . . . . . . . . . . . . . . . . . . . . . . . . 26
3.7.3 Inorder . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29
3.7.4 Breadth First . . . . . . . . . . . . . . . . . . . . . . . . . 30
3.8 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31
4 Heap 32
4.1 Insertion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33
4.2 Deletion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37
4.3 Searching . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 38
4.4 Traversal . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41
4.5 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42
5 Sets 44
5.1 Unordered . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 46
5.1.1 Insertion . . . . . . . . . . . . . . . . . . . . . . . . . . . . 46
5.2 Ordered . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 47
5.3 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 47
6 Queues 48
6.1 A standard queue . . . . . . . . . . . . . . . . . . . . . . . . . . . 49
6.2 Priority Queue . . . . . . . . . . . . . . . . . . . . . . . . . . . . 49
6.3 Double Ended Queue . . . . . . . . . . . . . . . . . . . . . . . . . 49
6.4 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 53
7 AVL Tree 54
7.1 Tree Rotations . . . . . . . . . . . . . . . . . . . . . . . . . . . . 56
7.2 Tree Rebalancing . . . . . . . . . . . . . . . . . . . . . . . . . . . 57
7.3 Insertion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 58
7.4 Deletion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 59
7.5 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61
II Algorithms 62
8 Sorting 63
8.1 Bubble Sort . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 63
8.2 Merge Sort . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 63
8.3 Quick Sort . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 65
8.4 Insertion Sort . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 67
8.5 Shell Sort . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 68
8.6 Radix Sort . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 68
8.7 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 70
9 Numeric 72
9.1 Primality Test . . . . . . . . . . . . . . . . . . . . . . . . . . . . 72
9.2 Base conversions . . . . . . . . . . . . . . . . . . . . . . . . . . . 72
9.3 Attaining the greatest common denominator of two numbers . . 73
9.4 Computing the maximum value for a number of a specific base
consisting of N digits . . . . . . . . . . . . . . . . . . . . . . . . . 74
9.5 Factorial of a number . . . . . . . . . . . . . . . . . . . . . . . . 74
9.6 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 75
II
10 Searching 76
10.1 Sequential Search . . . . . . . . . . . . . . . . . . . . . . . . . . . 76
10.2 Probability Search . . . . . . . . . . . . . . . . . . . . . . . . . . 76
10.3 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 77
11 Strings 79
11.1 Reversing the order of words in a sentence . . . . . . . . . . . . . 79
11.2 Detecting a palindrome . . . . . . . . . . . . . . . . . . . . . . . 80
11.3 Counting the number of words in a string . . . . . . . . . . . . . 81
11.4 Determining the number of repeated words within a string . . . . 83
11.5 Determining the first matching character between two strings . . 84
11.6 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 85
A Algorithm Walkthrough 86
A.1 Iterative algorithms . . . . . . . . . . . . . . . . . . . . . . . . . 86
A.2 Recursive Algorithms . . . . . . . . . . . . . . . . . . . . . . . . . 88
A.3 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 90
B Translation Walkthrough 91
B.1 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 92
D Testing 97
D.1 What constitutes a unit test? . . . . . . . . . . . . . . . . . . . . 97
D.2 When should I write my tests? . . . . . . . . . . . . . . . . . . . 98
D.3 How seriously should I view my test suite? . . . . . . . . . . . . . 99
D.4 The three A’s . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 99
D.5 The structuring of tests . . . . . . . . . . . . . . . . . . . . . . . 99
D.6 Code Coverage . . . . . . . . . . . . . . . . . . . . . . . . . . . . 100
D.7 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 100
III
www.dbooks.org
Preface
Every book has a story as to how it came about and this one is no different,
although we would be lying if we said its development had not been somewhat
impromptu. Put simply this book is the result of a series of emails sent back
and forth between the two authors during the development of a library for
the .NET framework of the same name (with the omission of the subtitle of
course!). The conversation started off something like, “Why don’t we create
a more aesthetically pleasing way to present our pseudocode?” After a few
weeks this new presentation style had in fact grown into pseudocode listings
with chunks of text describing how the data structure or algorithm in question
works and various other things about it. At this point we thought, “What the
heck, let’s make this thing into a book!” And so, in the summer of 2008 we
began work on this book side by side with the actual library implementation.
When we started writing this book the only things that we were sure about
with respect to how the book should be structured were:
1. always make explanations as simple as possible while maintaining a moder-
ately fine degree of precision to keep the more eager minded reader happy;
and
IV
V
Therefore it is absolutely key that you think about the run time complexity and
space requirements of your selected approach. In this book we only explain the
theoretical implications to consider, but this is for a good reason: compilers are
very different in how they work. One C++ compiler may have some amazing
optimisation phases specifically targeted at recursion, another may not, for ex-
ample. Of course this is just an example but you would be surprised by how
many subtle differences there are between compilers. These differences which
may make a fast algorithm slow, and vice versa. We could also factor in the
same concerns about languages that target virtual machines, leaving all the
actual various implementation issues to you given that you will know your lan-
guage’s compiler much better than us...well in most cases. This has resulted in
a more concise book that focuses on what we think are the key issues.
One final note: never take the words of others as gospel; verify all that can
be feasibly verified and make up your own mind.
We hope you enjoy reading this book as much as we have enjoyed writing it.
Granville Barnett
Luca Del Tongo
www.dbooks.org
Acknowledgements
Writing this short book has been a fun and rewarding experience. We would
like to thank, in no particular order the following people who have helped us
during the writing of this book.
Sonu Kapoor generously hosted our book which when we released the first
draft received over thirteen thousand downloads, without his generosity this
book would not have been able to reach so many people. Jon Skeet provided us
with an alarming number of suggestions throughout for which we are eternally
grateful. Jon also edited this book as well.
We would also like to thank those who provided the odd suggestion via email
to us. All feedback was listened to and you will no doubt see some content
influenced by your suggestions.
A special thank you also goes out to those who helped publicise this book
from Microsoft’s Channel 9 weekly show (thanks Dan!) to the many bloggers
who helped spread the word. You gave us an audience and for that we are
extremely grateful.
Thank you to all who contributed in some way to this book. The program-
ming community never ceases to amaze us in how willing its constituents are to
give time to projects such as this one. Thank you.
VI
About the Authors
Granville Barnett
Granville is currently a Ph.D candidate at Queensland University of Technology
(QUT) working on parallelism at the Microsoft QUT eResearch Centre1 . He also
holds a degree in Computer Science, and is a Microsoft MVP. His main interests
are in programming languages and compilers. Granville can be contacted via
one of two places: either his personal website (http://gbarnett.org) or his
blog (http://msmvps.com/blogs/gbarnett).
1 http://www.mquter.qut.edu.au/
VII
www.dbooks.org
Page intentionally left blank.
Chapter 1
Introduction
1. Big Oh notation
www.dbooks.org
CHAPTER 1. INTRODUCTION 2
Figure 1.1 shows some of the run times to demonstrate how important it is to
choose an efficient algorithm. For the sanity of our graph we have omitted cubic
O(n3 ), and exponential O(2n ) run times. Cubic and exponential algorithms
should only ever be used for very small problems (if ever!); avoid them if feasibly
possible.
The following list explains some of the most common big Oh notations:
O(1) constant: the operation doesn’t depend on the size of its input, e.g. adding
a node to the tail of a linked list where we always maintain a pointer to
the tail node.
O(n) linear: the run time complexity is proportionate to the size of n.
O(log n) logarithmic: normally associated with algorithms that break the problem
into smaller chunks per each invocation, e.g. searching a binary search
tree.
O(n log n) just n log n: usually associated with an algorithm that breaks the problem
into smaller chunks per each invocation, and then takes the results of these
smaller chunks and stitches them back together, e.g. quick sort.
O(n2 ) quadratic: e.g. bubble sort.
O(n3 ) cubic: very rare.
O(2n ) exponential: incredibly rare.
If you encounter either of the latter two items (cubic and exponential) this is
really a signal for you to review the design of your algorithm. While prototyp-
ing algorithm designs you may just have the intention of solving the problem
irrespective of how fast it works. We would strongly advise that you always
review your algorithm design and optimise where possible—particularly loops
Random documents with unrelated
content Scribd suggests to you:
The Project Gutenberg eBook of Tietoja
maailman kansoista, heidän tavoista,
uskonnoista ja vaiheista
This ebook is for the use of anyone anywhere in the United States
and most other parts of the world at no cost and with almost no
restrictions whatsoever. You may copy it, give it away or re-use it
under the terms of the Project Gutenberg License included with this
ebook or online at www.gutenberg.org. If you are not located in the
United States, you will have to check the laws of the country where
you are located before using this eBook.
Language: Finnish
TIETOJA MAAILMAN
KANSOISTA, HEIDÄN
TAVOISTA, USKONNOISTA JA
VAIHEISTA
Saksasta mukailtu
[J. O. Forsman]
Imprimatur: L. Heimbürger.
SISÄLLYS:
Ihmiskunta yleensä
Germanilaiset (Saksalaiset)
Frankilaisten valta
Britannian saarten asukkaat
Normannilaiset ei Skandinavilaiset
Slavilaiset
Suomalaiset
Arapilaiset
Japanesit
Neekerit
Amerikan alkukansat
Ihmiskunta yleensä.
Ensimäinen Osake.
Näin oli tuo onneton hajotus tapahtunut, joka oli syy Juudan
kansan häviöön. Siis oli jälkeen Salomonin kuoleman kaksi
valtakuntaa: 1 Juudan valtakunta, jolla oli Salomonin poika
Rehabeam kuninkaana; ja 2 Israelin valtakunta, Jerobeam
kuninkaanansa. Molempien valtakuntien historia ei sisällä muuta kuin
jumalattomuutta ja kurjuutta.
Jerobeam, Israelin valtakunnan kuningas, teki Sichem'in
pääkaupungiksensa. Kun tahtoi peräti hävittää sen arvon, joka
Jerusalemilla templinsä vuoksi oli, kielsi hän alamaisensa templiin
menemästä ja asetti maassansa kaksi epäjumalan-kuvaa. Nyt kansa
lankesi epäjumalien palvelukseen, vaikka ennostuneet Propheetat
voimiensa mukaan varoittivatkin sitä tämmöiseen ilkeyteen
rupeemasta. Pakanalliset kuninkaat sotivat voimakkaasti Israelin
valtaa vastaan ja sen viimeisen kuninkaan, Hosean hallitessa tuli
Assyrialainen kuningas Salmanassar ja valloitti koko valtakunnan.
Itse Hosea kuningas ynnä iso osa asukkaita vietiin Assyriaan
vankeuteen, v. 722 e.Kr. Maahan jääneet kansoittivat sittemmin
Samarian ja kutsuttiin Samariiteiksi. Mutta Juutalaiset eivät koskaan
lukeneet näitä kansalaisiksensa.
2. Intialaiset.
4. Babylonilaiset ja Assyrialaiset.
ebookbell.com