Question

2) Write a recursive procedure in pseudocode to implement the binary search algorithm. 3) Explain, how the binary search algo

0 0
Add a comment Improve this question Transcribed image text
Answer #1

1.Recursive procedure in pseudocode to implement a binary search algorithm

// C program to implement recursive Binary Search
#include <stdio.h>
int binarySearch(int arr[], int l, int r, int x)
{
   if (r >= l) {
       int mid = l + (r - l) / 2;  
       if (arr[mid] == x)
           return mid;  
       if (arr[mid] > x)
           return binarySearch(arr, l, mid - 1, x);
       return binarySearch(arr, mid + 1, r, x);
   }
   return -1;
}

int main(void)
{
   int arr[] = { 1,2,4,5};
   int n = sizeof(arr) / sizeof(arr[0]);
   int x = 3;
   int result = binarySearch(arr, 0, n - 1, x);
   (result == -1) ? printf("Element is not present in array")
               : printf("Element is present at index %d",
                           result);
   return 0;
}

2. To insert an element into a sorted array using binary search

In order to insert a random number into a sorted array using binary search first, we compare the number with mid, if the mid is greater than that number then right=mid otherwise lenft=mid-1. After this step, we again call the same function as this is a recursive function. This loop is completed when left<right and then we add the number in the place of left.

 public void add(T e, int result) {
        int left, right, mid;


        left = 0;
        right = array.size();


        while(left< right)  {
            mid = (left + right)/2;
            


            if(result > 0) { //If e is lower
                right = mid;
            } else { //If e is higher
                left = mid + 1;
            }
        }

        array.add(left, e);

    }
Add a comment
Know the answer?
Add Answer to:
2) Write a recursive procedure in pseudocode to implement the binary search algorithm. 3) Explain, how...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • a. You need write a sequential search algorithm that is recursive format. b. Write a binary...

    a. You need write a sequential search algorithm that is recursive format. b. Write a binary search algorithm that is recursive, then write the program to implement it. C++

  • Write and implement a recursive version of the sequential search algorithm. Test the algorithm with an...

    Write and implement a recursive version of the sequential search algorithm. Test the algorithm with an array that have ten integer values, user can enter the values or assigned the values to the array in the main method. The search key should be a value in the array or not in the array. JAVA!!!

  • Write pseudocode for a recursive algorithm for inserting a key into a binary search tree. The following is the definition assumed. right? Write a) pto) You hadt written codes for for a algonithm for...

    Write pseudocode for a recursive algorithm for inserting a key into a binary search tree. The following is the definition assumed. right? Write a) pto) You hadt written codes for for a algonithm for inserting a key into a binary seareh tree. The public class BST E extends comparable V prot fected bstNodeE protected class bstNode E V E key V value; ,V> right); bstNode E vright bstNode (E key ngvalue , bstNode«E·V> left . bstNode«E publle void insert (E...

  • 3. [15 marks] Describe an algorithm in pseudocode that locates an element in a list of...

    3. [15 marks] Describe an algorithm in pseudocode that locates an element in a list of increasing integers by successively splitting the list into four sublists of equal size (or as close to equal size as possible) and restricting the search to the appropriate piece. procedure tetrary search(x: integer, a1, a2, ..., an: increasing integers) (location is the subscript of the term equal to x, or 0 if not found) 3. [15 marks] Describe an algorithm in pseudocode that locates...

  • Just Q3 and Q4 Q1] Write a C function to implement the binary search algorithm over...

    Just Q3 and Q4 Q1] Write a C function to implement the binary search algorithm over an array of integer numbers and size n. The function should return the index of the search key if the search key exists and return - 1 if the search key doesn't exist. [10 Points] Q2] Write a C function to implement the selection sort algorithm, to sort an array of float values and size n. The function should sort the array in ascending...

  • 1.Write in pseudocode a recursive algorithm for the operation deleteHighest (t), where t is the root...

    1.Write in pseudocode a recursive algorithm for the operation deleteHighest (t), where t is the root of the BST, to delete the largest element in a BST 2.Fill in the following table, giving the “worstcase”time complexity for each operation, ineach of the two implementations, assumingthe PQ contains n elements. insert deleteHighest Worst case time compexity Heap BST

  • Binary Search

    Suppose we want to check if a sorted sequence A contains an element v.  For this, we can use Binary Search.  Binary Search compares the value at the midpoint of the sequence A with v and eliminates half of the sequence from further consideration.  The Binary Search algorithm repeats this procedure, halving the size of the remaining portion of the sequence each time. Write a recurrence for the runningtime of Binary search and solve this recurrence.

  • (a) Write a program in Java to implement a recursive search function int terSearch(int A[], int...

    (a) Write a program in Java to implement a recursive search function int terSearch(int A[], int l, int r, int x) that returns the location of x in a given sorted array of n integers A if x is present, otherwise -1. The terSearch search function, unlike the binary search, must consider two dividing points int d1 = l + (r - l)/3 int d2 = d1 + (r - l)/3 For the first call of your recursive search function...

  • 2. The following (adapted from Wikipedia) gives pseudocode for searching a sorted vector A for a ...

    2. The following (adapted from Wikipedia) gives pseudocode for searching a sorted vector A for a specific element T. Given an array A of n elements with values or records A1 An and target value T, the following subroutine uses binary search to find the index of T in A. 1. Set L to 1 and R ton. 2. If L>R, the search terminates as unsuccessful Set m (the position of the middle element) to the floor of (L+R)/2. 3....

  • The binary search algorithm from Chapter 9 is a very efficient algorithm for searching an ordered...

    The binary search algorithm from Chapter 9 is a very efficient algorithm for searching an ordered list. The algorithm (in pseudocode) is as follows: highIndex - the maximum index of the part of the list being searched lowIndex - the minimum index of the part of the list being searched target -- the item being searched for //look in the middle middleIndex = (highIndex + lowIndex) / 2 if the list element at the middleIndex is the target return the...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT