Maximizing Real-Life Benefits from Computer Science: Sorting & Searching
Written on
Chapter 1: Introduction to Sorting and Searching
In my earlier discussion about leveraging computer science in everyday scenarios, I delved into the trade-offs between time, space, and resources. This piece will focus on the critical concepts of sorting and searching.
We will start by examining a classic real-world use case for sorting and searching algorithms. Next, we will explore the primary challenges these algorithms encounter and the contributions of computer science in overcoming them. Finally, we will examine one of the most efficient sorting and searching algorithms currently in use. Let’s dive in.
A Fundamental Application of Sorting and Searching
The emergence of books created a demand for efficient sorting and storage methods. Historically, books were valuable assets, and libraries became essential for knowledge enhancement.
As libraries expanded to house thousands of volumes, it became crucial to sort and store these texts in a manner that allowed for easy retrieval. Before we investigate how libraries addressed this issue, let's attempt to solve it ourselves with a simpler scenario.
How to Utilize Computer Science for Book Sorting and Searching
Imagine you have 15 books, each with a unique title beginning with a different letter of the English alphabet. You have a single shelf rack at your disposal. How can you arrange these books for efficient searching?
One logical approach is to organize the books alphabetically by their initial letters. This method would create a systematic way to arrange the books on the shelf.
Next, we must determine how to search for a specific book. Fortunately, there's a convenient algorithm from computer science that we can employ. For example, suppose you are searching for a title starting with the letter 'S.'
The algorithm works as follows: begin by examining the book in the center of the shelf. If it starts with a letter that precedes 'S,' you can eliminate the first half of the rack. Conversely, if it starts with a letter that follows 'S,' you can discard the second half.
Assuming the middle book begins with 'H,' which is before 'S,' you would reject the first half of the shelf. Next, you would check the middle book of the remaining half. Suppose this book starts with 'V,' which comes after 'S.' This time, you would discard the latter part of the original second half.
After repeating this process, you would eventually find the book you're looking for, located at position 10. Remarkably, you only had to check three titles out of 15 to find your desired book. However, what if there are more than 15 books?
The Search Algorithm for Any Number of Books
While this might seem anticlimactic, the same algorithm can be applied to any number of books, showcasing its scalability.
The key takeaway is that each search step effectively halves the search space. Therefore, for 15 books, it would take a maximum of four searches to find any specific title. For 31 books, it would take five searches; for 63, six searches; and so forth.
In its generalized form, this algorithm indicates that it would take 'n' searches to locate a title among a total of [(2^n)-1] books. This means you could find any book among 1,048,575 titles with just 20 searches—an impressive feat! But what if you needed to locate a book by the author's name?
Library Solutions for Sorting and Searching
The approach we have discussed is efficient but only allows searching by one criterion (the title). To search by additional criteria, like the author's name, we need to increase the sorting dimensions.
You might wonder how to achieve this with just one shelf. Historically, libraries devised a clever solution: they created a "catalogue card" for each book. This card contained essential details about the book, including the author's name, publication information, and its location.
These catalogue cards were sorted based on various criteria, allowing for flexible searching. Libraries simply created new stacks of cards for each unique search parameter.
With the advent of computers, this entire system underwent a transformation.
How Computers Revolutionized Sorting and Searching
As modern computers became common, they dramatically lowered the cost of indexing. The reliance on physical cards faded; any search criterion used more than twice was likely indexed. Searching indexed data is exponentially faster than scanning through non-indexed information.
Today, digital libraries offer users multiple search options. However, the story doesn’t end here. While digital cataloging transformed the landscape about forty years ago, new methods are further changing how we think about sorting.
Modern Large-Scale Storage (Random Stow)
Computers frequently use indexing for searches. Since moving data within physical storage incurs costs, engineers realized it wasn’t necessary to rearrange data constantly. Instead, merely updating index records with the latest data location suffices.
Surprisingly, the most advanced sort and search technology in large libraries has eliminated the need for sorting altogether. In this system, books are stored randomly. A prime example is the Hunt Library bookBot in North Carolina.
This method is so efficient that major retailers like Amazon employ similar strategies. Instead of sorting items by category, they store each product randomly in the nearest available space while maintaining a prompt indexing system for later retrieval.
Final Thoughts
We began by developing an algorithm to sort 15 books on a shelf, which we then scaled for larger collections. We expanded our search capabilities using the traditional library cataloging method.
Lastly, we examined how computerized indexing has enabled us to bypass sorting entirely, thereby enhancing search efficiency. However, this approach does come with a caveat: it makes us almost entirely dependent on computers. Such a system lacks sustainability in the event of a database failure or server crash.
If you wish to leverage computer science for physical sorting and searching without relying on technology, it may be wise to forgo some efficiency by physically sorting and storing items.
Further reading might include topics like "What Makes You Get Faster Search Results On Your Browser?" and "The Additive Palindrome Conjecture Is A Risky Venture."
If you would like to support me as an author, consider contributing on Patreon.
Chapter 2: Exploring Computer Science in Action
Explore the reasons behind choosing computer science as a field of study and how it can enrich your life and career.
Experience a day in the life of a computer science student, highlighting the challenges and rewards of pursuing this exciting field.