r/computerscience • • Sep 24 '25

does sequential search compare every element even if there is an absence?

like for example we have this list (1,5,17,40,92,100) and i want to see how many comparisons will it do to understand that the number 35 for example is missing. will it stop at 40 or will it go till the end of the list?

1 Upvotes

9 comments sorted by

View all comments

Show parent comment

4

u/O_xD Sep 24 '25

you can sort it once then search multiple times in the already sorted list, so its a tradeoff that you have to take in to account.

sometimes its better to sort first so you can binary search, other times its not.

alternatively, you may be able to sort your data when there is low or no demand for processing power, then reap the benefits later.

e.g. an index in a database, it will get sorted during downtime so that when a request comes in, you get to search in logarithmic time