Abstract
Searching and sorting are two basic problems that contain many valuable elements for algorithms. They are still being investigated in many related areas. The series of books entitled “The Art of Computer Programming” by D. E. Knuth is said to be “the Bible” of algorithmic theory, and the third volume has the subtitle “Sorting and Searching.” We will enjoy and understand these deep problems in this chapter.
Donald Ervin Knuth (1938–):
He is a professor emeritus of computer science at Stanford University. The series of books mentioned in the text is called “TAOCP.” It was planned to publish the series in seven volumes, starting in the 1960s. Now, some preliminary drafts of the fourth volume are public on the Web. It is surprising that, to write this series, Knuth first started with designing and programming the well-known TeX system for typesetting, and the METAFONT system for designing typesetting fonts. See Chap. 7 for more details.
Access this chapter
Tax calculation will be finalised at checkout
Purchases are for personal use only
Notes
- 1.
Implementation of sentinel:
In the real implementation, we cannot use “the value” \(\infty \), and hence, we have to allow it to be a sufficiently large integer. Several programming languages have such a special value. If your language does not have such a value, you can set the maximum number of the system. We sometimes use the same idea in this book.
- 2.
Results of a comparison:
Here, to simplify, we assume that \(x<y\) or \(x>y\) holds in a comparison. We omit the other possible case, \(x=y\), in this book. Another issue is that \(\log n\) is not an integer in general. To be precise, we have to take \(\left\lceil \log n\right\rceil \) or \(\left\lfloor \log n\right\rfloor \) according to the context, which is also omitted in this book.
- 3.
Flag:
As introduced in the text, this is a common technique in programming where a variable that indicates that the status is changed from, say, 0 to 1, is used. This variable is called a flag . We sometimes use the expression that a flag “stands” when the value of the flag is updated. Maybe it comes from the image of setting or positioning small flags. The idea of a flag is useful, but it is not very smart in general since it tends to make the structure of the program untidy. If you can use some standard control statements, such as while, repeat, for, and others, it is better to do so. It might be better to reconsider the structure or logic in the program if you cannot avoid it.
- 4.
Assertion of a program:
A property that always holds in a computer program is called an assertion . By maintaining reasonable assertions, we can avoid introducing bugs into our programs.
- 5.
Changing the base of log:
It is known that \(\log _a b= \log _c b/\log _c a\) for any positive real numbers a, b, and c. This is called the change-of-base formula. Under the O notation, we can say that \(\ln n=\Theta (\log n)\) and \(\log n=\Theta (\ln n)\). That is, every logarithmic function \(\log _c\) is the same as the other logarithmic function \(\log _{c'}\) up to a constant factor.
- 6.
Stirling’s formula :
The precise Stirling’s formula is \(n!\sim \sqrt{2\pi n}{\left( \frac{n}{e}\right) }^n\). The error of this approximation is small, and this formula is useful for the analyses of algorithms. For the present, in a rough-and-ready manner, the approximation \(n!\sim n^n\) is worth remembering for estimating computational complexity roughly in order to analyze an algorithm.
Author information
Authors and Affiliations
Corresponding author
Rights and permissions
Copyright information
© 2019 Springer Nature Singapore Pte Ltd.
About this chapter
Cite this chapter
Uehara, R. (2019). Algorithms for Searching and Sorting. In: First Course in Algorithms Through Puzzles. Springer, Singapore. https://doi.org/10.1007/978-981-13-3188-6_3
Download citation
DOI: https://doi.org/10.1007/978-981-13-3188-6_3
Published:
Publisher Name: Springer, Singapore
Print ISBN: 978-981-13-3187-9
Online ISBN: 978-981-13-3188-6
eBook Packages: Computer ScienceComputer Science (R0)