3. List all the steps used to search for 9 in the sequence 1, 3, 4, 5, 6, 8, 9, 11 usinga) a linear search b) a binary search.
Question
3. List all the steps used to search for 9 in the sequence 1, 3, 4, 5, 6, 8, 9, 11 using
a) a linear search
b) a binary search.
🧐 Not the exact question you are looking for?Go ask a question
Solution
a) Linear Search:
- Start at the first element of the sequence.
- Compare the first element (1) with the target value (9). They are not equal.
- Move to the next element (3) and compare it with the target value (9). They are not equal.
- Move to the next element (4) and compare it with the target value (9). They are not equal.
- Move to the next element (5) and compare it with the target value (9). They are not equal.
- Move to the next element (6) and compare it with the target value (9). They are not equal.
- Move to the next element (8) and compare it with the target value (9). They are not equal.
- Move to the next element (9) and compare it with the target value (9). They are equal.
- The target value (9) is found at the 7th position in the sequence.
b) Binary Search:
- Start with the entire sequence: 1, 3, 4, 5, 6, 8, 9, 11.
- Find the middle element. The middle index is (0+7)/2 = 3.5, rounded down to 3. The middle element is 5.
- Compare the middle element (5) with the target value (9). Since 9 is greater than 5, focus on the right half of the sequence: 6, 8, 9, 11.
- Find the middle element of the right half. The new middle index is (4+7)/2 = 5.5, rounded down to 5. The middle element is 8.
- Compare the middle element (8) with the target value (9). Since 9 is greater than 8, focus on the right half of the new sequence: 9, 11.
- Find the middle element of the new right half. The new middle index is (6+7)/2 = 6.5, rounded down to 6. The middle element is 9.
- Compare the middle element (9) with the target value (9). They are equal.
- The target value (9) is found at the 7th position in the sequence.
Similar Questions
How many steps are there in the information search process? A. 5 B. 9 C. 4 D. 2
1. List all the steps used by Algorithm "max" to find the maximum of the list 1, 8, 12, 9, 11,2, 14, 5, 10, 4.
The sequence 5, 7, ______, 11 ...... n is an arithmetic progressionQuestion 9Answera.6b.9c.3d.10
Assuming the given list follows a pattern, find the next three terms in the list 8, 9, 7, 10,6, ...
Question 10Which of the following sequences cannot occur from a postorder traversal of a binary search tree with the keys 1, 2, 3, 4, 5?
1/1
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.