Algorithms for searching Flashcards

1
Q

Explain the steps taken to perform a linear search, using a worked example as part of your explanation.

A

Linear search involves checking every element in a list in order, one-by-one until the desired element is found.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Explain the steps taken to perform a binary search,

A

Binary search operates on a sorted list, choosing the middle element and comparing it with the target. If they match, the search ends. If not, it narrows the search to the left or right, repeating until the item is found or no more elements remain.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Roland performs both linear and binary searches on a large dataset for the same item. Surprisingly, the linear search proves much faster than the binary search. How is this discrepancy possible?

A

Binary search is generally more efficient in average and worst-case scenarios compared to linear search.

However, if the item is at the beginning of the list, linear search outperforms binary search. For instance, in a list of 1000 items, linear search checks 1 item, while binary search checks at most 10, thanks to its ability to halve the search space with each comparison.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly