Sequential search code9/13/2023 ![]() ![]() In particular, when the list items are arranged in order of decreasing probability, and these probabilities are geometrically distributed, the cost of linear search is only O(1). Therefore, if some values are much more likely to be searched than others, it is desirable to place them at the beginning of the list. The performance of linear search improves if the desired value is more likely to be near the beginning of the list than to its end. (for example, for n = 2 this is 1, corresponding to a single if-then-else construct).Įither way, asymptotically the worst-case cost and the expected cost of linear search are both O( n). If the value being sought occurs k times in the list, and all orderings of the list are equally likely, the expected number of comparisons is The worst case is when the value is not in the list (or occurs only once at the end of the list), in which case n comparisons are needed. Else, the search terminates unsuccessfully.įor a list with n items, the best case is when the value is equal to the first element of the list, in which case only one comparison is needed.
0 Comments
Leave a Reply.AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |