Câu hỏi phỏng vấn Searching
Câu hỏi

Is Sentinel Linear Search bet...

Câu trả lời

Yes, Sentinel Linear Search is better than normal Linear Search in certain scenarios. Here's why:

In normal Linear Search, the loop checks two conditions in each iteration: 1) if the current element matches the search key, and 2) if the loop index is within the array bounds[1][4]. This results in 2*N+1 comparisons in the worst case, where N is the size of the array[1][5].

However, in Sentinel Linear Search, the last element of the array is replaced with the search key before the loop starts[1][2]. This eliminates the need to check if the loop index is within bounds, as the search key is guaranteed to be found before reaching the end of the array[1][4].

The loop in Sentinel Linear Search only needs to check if the current element matches the search key. This reduces the number of comparisons in the worst case to N+2, where N is the size of the array[1][2].

For example, consider an ...

expert

expert

Gợi ý câu hỏi phỏng vấn

senior

Explain what is Ternary Search?

middle

Compare Binary Search vs Linear Search

middle

Recursive and Iterative Binary Search: Which one is more efficient and why?

Bình luận

Chưa có bình luận nào

Chưa có bình luận nào