Searching algorithms: linear and binary search
Học bằng cách chơi
Trả lời những câu hỏi này để kiếm năng lượng, rồi câu cá và khám phá. Không cần tài khoản.
Ghi chú bài học
Introduction to Search Algorithms
- A search algorithm finds the position of a target value within a list of items.
- Two common search algorithms are linear search and binary search.
- Linear search checks each item in order until the target is found or the list ends.
- Binary search repeatedly divides a sorted list in half to locate the target.
- The choice of algorithm affects speed and efficiency, especially with large data sets.
Linear Search
- Linear search starts at the first element and compares each element with the target.
- It works on unsorted or sorted lists.
- In the worst case, it checks every element, so it takes n comparisons for a list of n items.
- Its time complexity is O(n) – linear time.
- It is simple to implement but slow for large lists.
- Best used when the list is small or unsorted.
Binary Search
- Binary search requires the list to be sorted in ascending order.
- It compares the target with the middle element of the list.
- If the target is less than the middle, the search continues in the left half; if greater, in the right half.
- This process repeats, halving the search area each time.
- If the search area becomes empty, the target is not present.
- It runs in O(log n) time – logarithmic time.
- For a list of 1,000,000 items, binary search needs at most about 20 comparisons, while linear search may need up to 1,000,000.
Binary Search Step-by-Step
- Set low to 0 and high to n-1 (the first and last indices).
- While low ≤ high, calculate mid = (low + high) / 2 (rounded down).
- If the middle element equals the target, return mid.
- If the middle element is less than the target, set low = mid + 1.
- If the middle element is greater than the target, set high = mid - 1.
- If low > high, the target is not found.
Comparing Efficiency
- Linear search time grows proportionally with list size (O(n)).
- Binary search time grows logarithmically (O(log n)).
- For small lists, linear search may be faster due to less overhead.
- For large sorted lists, binary search is dramatically faster.
- Binary search requires the list to be sorted, which adds an initial cost if not already sorted.
When to Use Each Algorithm
- Use linear search when the list is unsorted or small.
- Use linear search when you need to find all occurrences of a value.
- Use binary search when the list is sorted and large.
- Use binary search when you need the fastest search time.
- If the list is frequently updated, sorting overhead may make linear search more practical.
Key Terms
- Array – a collection of items stored at contiguous memory locations.
- Sorted – arranged in ascending or descending order.
- Target – the value being searched for.
- Index – the position of an element in an array (starting at 0).
- Time complexity – a measure of how the running time grows with input size.
- O(n) – linear time; O(log n) – logarithmic time.
Common Mistakes
- Forgetting to sort the list before using binary search.
- Using mid = (low + high) / 2 without rounding down (integer division).
- Not updating low or high correctly, causing infinite loops.
- Assuming binary search works on unsorted data.
- Confusing index with value when comparing.
Slide
Sign up free to view the lesson slides
Step through every slide for this topic — plus flashcards and revision notes — with a free account.
Câu hỏi luyện tập
Xem trước miễn phí — 8 trên 59 câu hỏi. Đăng ký để xem tất cả.
1.What is the main requirement for binary search to work correctly?
Easy- AThe data must be sorted
- BThe data must be unsorted
- CThe data must be stored in a hash table
- DThe data must be small
2.Linear search requires the data to be sorted.
EasyTrue or false?
3.Which search algorithm is generally faster on a large sorted list?
Easy- ABinary search
- BLinear search
- CBoth are equally fast
- DIt depends on the programming language
4.Arrange the steps of a binary search on a sorted array in the correct order:
Easy- Compare the target with the middle element.
- If the target is less than the middle, search the lower half.
- If the target is greater than the middle, search the upper half.
- If the target equals the middle, return its position.
5.Which of the following are true about linear search? (Select all that apply)
Easy- AIt checks each element in order.
- BIt requires the data to be sorted.
- CIt works on unsorted data.
- DIt is usually slower than binary search on large lists.
- EIt always starts from the middle of the list.
6.Given the sorted list [2, 5, 8, 12, 16, 23, 38], what is the first element compared to when searching for 23 using binary search?
Medium- A12
- B16
- C23
- D38
7.In binary search, after comparing the target to the middle element and finding it is greater, what happens next?
Medium- AThe lower half is eliminated and the search continues in the upper half.
- BThe upper half is eliminated and the search continues in the lower half.
- CThe search stops immediately.
- DThe entire list is searched again.
8.Binary search can be used on an unsorted list if you first sort it.
EasyTrue or false?
Unlock all 59 questions, flashcards & more
Tạo tài khoản miễn phí để xem mọi câu hỏi, slide, thẻ ghi nhớ và ghi chú ôn tập cho chủ đề này.