Chapter 10: Problem 1
Mark the following statements as true or false. a. \(A\) sequential search of a list assumes that the list elements are sorted in ascending order. b. \(A\) binary search of a list assumes that the list is sorted. c. A binary search is faster on ordered lists and slower on unordered lists. d. \(A\) binary search is faster on large lists, but a sequential search is faster on small lists. e. When you declare a vector object and specify its size as \(10,\) then only 10 elements can be stored in the object.
Short Answer
Step by step solution
Evaluate statement (a)
Evaluate statement (b)
Evaluate statement (c)
Evaluate statement (d)
Evaluate statement (e)
Unlock Step-by-Step Solutions & Ace Your Exams!
-
Full Textbook Solutions
Get detailed explanations and key concepts
-
Unlimited Al creation
Al flashcards, explanations, exams and more...
-
Ads-free access
To over 500 millions flashcards
-
Money-back guarantee
We refund you if you fail your exam.
Over 30 million students worldwide already upgrade their learning with Vaia!
Key Concepts
These are the key concepts you need to understand to accurately answer the question.
Sequential Search
This method is simple and easy to implement, but it can be inefficient for large lists because it might check every single element. The time complexity of a sequential search is linear, denoted as \(O(n)\), where \(n\) is the number of elements in the list.
While a sequential search can be slower than other algorithms, it is effective for small lists or when the list is unordered, as no pre-sorting is needed.
- Checks each element one-by-one
- No need for a sorted list
- Time complexity: \(O(n)\)
Binary Search
If the target value is equal to the middle element, the search is complete. If the target is less, the search continues on the left half, otherwise on the right. This halving process continues, narrowing down the possible positions of the target until it is found or the interval is reduced to zero.
- Requires a sorted list
- Time complexity: \(O(\log n)\)
- Efficient for large datasets
Vector in C++
When a vector is declared with an initial size, it does not strictly limit the number of elements it can contain. If elements are added beyond its current capacity, the vector re-allocates its storage to accommodate the new elements. This dynamic resizing makes vectors quite flexible and useful in situations where the size of the dataset can change.
- Dynamic resizing capability
- Part of C++ STL (Standard Template Library)
- Initial size is not strict; vectors can grow as needed
Algorithm Efficiency
For instance, a sequential search has a time complexity of \(O(n)\), meaning the time taken is proportional to the number of elements. On the other hand, a binary search has a time complexity of \(O(\log n)\), which is much faster, especially for large lists, because it reduces the search space exponentially by halving the list in each step.
- Time complexity: measures execution time in relation to input size
- Space complexity: considers memory usage
- Crucial for comparing the practicality of algorithms