A simpler way to right what you have described is below. The code should be very similar to binary search. Many unnecessary elif conditions but I think that was only used for printing.The leftmost range would always start from 0 in your case.The length of the list should be more than the right-most element.Some of the things that I found incorrect were: Yes, you are right in that there were many things wrong with the code above. Print("Checking if " + str(key) + " is equal to " + str(L)) Print("Checking if " + str(key) + " is less than " + str(L)) Print("A total of " + str(n) + " comparisons were made") It is similar to binary search, except that the search region is divided into three smaller regions (having lengths as equal as possible) at each iteration by picking two indexes ind1 and ind2 (ind1 L: I'm trying to write a ternary search algorithm function that consumes a sorted list of integers and a value.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |