Given an array and an element, search the element x in the array using a minimum number of comparisons.
This algorithm is developed to return the element index in 2n+1 comparison in the worst case.
To avoid the comparison that is taking place in the loop.
1. Check that the last element in the array is equal to searched element.
a. YES, element index is found, return the index.
b. NO, Take back up of the last element and put the searched element on the last index of the array.
2. Search for element with Infinite loop (without termination condition) and break when the element is found
a. If the element is found in the last index, this is the element put by us to terminate the search and this signals that element not found in the array.
b. If element found other than the last index, set the backup to the last index and return the index.
This algorithm is developed to return the element index in n+1 comparison in the worst case.