11/23/2023 0 Comments Algorithm for sequential search(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). However, if it is known that it occurs once, then at most n - 1 comparisons are needed, and the expected number of comparisons is 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įor example, if the value being sought occurs once 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 |