Abstract
Many efficient algorithms are based on sorting the input data, because sorting often makes solving the problem easier. This chapter discusses the theory and practice of sorting as an algorithm design tool. Section 4.1 first discusses three important sorting algorithms: bubble sort, merge sort, and counting sort. After this, we will learn how to use the sorting algorithm available in the C++ standard library. Section 4.2 shows how sorting can be used as a subroutine to create efficient algorithms. For example, to quickly determine if all array elements are unique, we can first sort the array and then simply check all pairs of consecutive elements. Section 4.3 presents the binary search algorithm, which is another important building block of efficient algorithms.
This is a preview of subscription content, log in via an institution.
Buying options
Tax calculation will be finalised at checkout
Purchases are for personal use only
Learn about institutional subscriptionsNotes
- 1.
The C++11 standard requires that the sort function works in \(O(n \log n)\) time; the exact implementation depends on the compiler.
- 2.
Note that in some older compilers, the function make_tuple has to be used to create a tuple instead of braces (for example, make_tuple(2,1,4) instead of {2,1,4}).
- 3.
Some people, including the author of this book, still use printed dictionaries. Another example is finding a phone number in a printed phone book, which is even more obsolete.
Author information
Authors and Affiliations
Corresponding author
Rights and permissions
Copyright information
© 2017 Springer International Publishing AG
About this chapter
Cite this chapter
Laaksonen, A. (2017). Sorting and Searching. In: Guide to Competitive Programming. Undergraduate Topics in Computer Science. Springer, Cham. https://doi.org/10.1007/978-3-319-72547-5_4
Download citation
DOI: https://doi.org/10.1007/978-3-319-72547-5_4
Published:
Publisher Name: Springer, Cham
Print ISBN: 978-3-319-72546-8
Online ISBN: 978-3-319-72547-5
eBook Packages: Computer ScienceComputer Science (R0)