Write a recursive binary search algorithm in pseudocode.

A binary search algorithm is one of the most important searching algorithm, user on sorted arrays. This algorithm searches for a given number (value) in an array A. How does the algorithm works ?1) check if the left is lower than right (if it is, the value is not in the array)2) compute the middle of the array3) check if the element in the middle is the number we are looking for (if true, return the position)4) compare the element in the middle with the value we are looking forif the value is lower, we are going to search in the left part of the arrayif the value is bigger, we are going to search in the right part of the array5) back to step 1, until left > right or a value is found.
BinarySearch(array A, value, left, right) { if(left > right) return "not found" mid = (left + right)/2; if(A[mid] == value) return value; if(A[min] > value) return BinarySearch(array A, value, left, mid - 1) else return BinarySearch(Array A, value, mid + 1, right)}

Related Computing University answers

All answers ▸

What is polymorphism in regards to Object Oriented Programming (OOP)? Provide an example.


Given two strings s1 and s2 return the location of each permutation of the s1 string within the s2 string.


What is inheritance in object-oriented programming?


What is the purpose of RAM in a computer?


We're here to help

contact us iconContact usWhatsapp logoMessage us on Whatsapptelephone icon+44 (0) 203 773 6020
Facebook logoInstagram logoLinkedIn logo

© MyTutorWeb Ltd 2013–2024

Terms & Conditions|Privacy Policy