Question

2. (10 pts) Recall Binary Search: Input: a list of n sorted integer values and a target value Output: True if target value ex
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Code:

#include <stdio.h>
#include <stdbool.h>
int binary_search(int arr[], int right, int target_value) //METHOD binary_search();
{
   int mid,left=1, target_index=-1; //set left =1, right =n, target_index =-1;
bool found = false; //set found =false;
while (found == false && left <= right) { //while found==false and left<=right;
       mid = left + (right - left) / 2; //calcultion of mid between left and right;
if (target_value == arr[mid]) //if target == item at mid the found = true and target_index=mid;
{
   found = true;
target_index= mid;
       }
if (target_value < arr[mid]) //if target<item then right=mid-1;
right = mid - 1;
if (target_value > arr[mid]) //if target>item then left=mid+1
left = mid + 1;
}
return target_index; //return target_index
}
int main()
{
int n, i,target_value,index;
printf("Enter Size of Sorted List of integers : \n");
scanf("%d",&n);
int arr[n];
printf("Enter %d integer in sorted way : \n", n);
for(i=0;i<n; i++){
   scanf("%d",&arr[i]);
   }
   printf("Enter a integer number to search in the list :\n");
   scanf("%d", &target_value);
index = binary_search(arr, n, target_value);
if (index == -1)
       printf("Element is not present in list");
else
   printf("Element is present at index %d", index);
return 0;
}

Note:

1. Input list should be in ascending(sorted list) order to satisfy the while loop condition(left<=right; as mentioned in the question)

2. Should not enter any negative number or zero, as minimum value in the list is 1(left=1 ;as mentioned in the question);

OutPut1:

Enter Size of Sorted List of integers :
5
Enter 5 integer in sorted way :
4
6
8
13
94
Enter a integer number to search in the list :
13
Element is present at index 3
--------------------------------
Process exited after 21.52 seconds with return value 0
Press any key to continue . . .

OutPut2:

Enter Size of Sorted List of integers :
6
Enter 6 integer in sorted way :
4
6
8
10
21
56
Enter a integer number to search in the list :
7
Element is not present in list
--------------------------------
Process exited after 25.19 seconds with return value 0
Press any key to continue . . .

Add a comment
Know the answer?
Add Answer to:
2. (10 pts) Recall Binary Search: Input: a list of n sorted integer values and a...
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
  • Java The following questions ask about tracing a binary search. To trace the binary search, list...

    Java The following questions ask about tracing a binary search. To trace the binary search, list the value of first, last, and mid for each pass through the loop or method. Use one of these methods for your trace. public static int binarySearchIterative(int[] numbers, int target) { boolean found = false; int first = 0; int last = numbers.length - 1; while (first <= last && !found) { int mid = (first + last) / 2; if (numbers[mid] == target)...

  • Given a sorted (in ascending order) integer array nums of n elements and a target value,...

    Given a sorted (in ascending order) integer array nums of n elements and a target value, write a method to perform a binary search for a target value in nums. If target exists, then return its index, otherwise return -1. Analyze the running time, T(n). public int search(int[] nums, int target) { // YOUR CODE GOES HERE } // end search

  • #2Trace These JAVA searches by hand on the following dataset. Sorted Data Set: 3, 5, 8,...

    #2Trace These JAVA searches by hand on the following dataset. Sorted Data Set: 3, 5, 8, 9, 12, 14, 18, 19, 21, 23, 24, 26 #Trace the sorted data set using a binary search. Search target: 23 #Trace the sorted data set using a binary search. Search target: 15 (To trace a binary search, list the value of first, last, and mid for each pass through the method.) Use this method: private static <T extends Comparable<? super T>>    boolean...

  • In the PowerPoint slides for lecture 15, you will find a function that performs binary search...

    In the PowerPoint slides for lecture 15, you will find a function that performs binary search on a Python list using recursion. Modify that binarySearchRec(alist, item) function so that it performs ternary search on a Python list. Your new function should find two midpoints that divide the list into three approximately equal size segments. If the item being searched for is found at either of the midpoints, the function should return True. If the item being searched for is less...

  • C++ Time the sequential search and the binary search methods several times each for randomly generated...

    C++ Time the sequential search and the binary search methods several times each for randomly generated values, then record the results in a table. Do not time individual searches, but groups of them. For example, time 100 searches together or 1,000 searches together. Compare the running times of these two search methods that are obtained during the experiment. Regarding the efficiency of both search methods, what conclusion can be reached from this experiment? Both the table and your conclusions should...

  • Recall that in a binary search tree, at every node, all elements to the left of...

    Recall that in a binary search tree, at every node, all elements to the left of the node have a smaller key, and all elements to the right of a node have a larger key. Write a program called that takes two parameters: a pointer to a binary search tree node, and an int parameter called min which will print all the elements bigger than the specified value, min. Your program should allow the user to enter data (integer) from...

  • 3. Write the function find sorted elt whose input is a vector sorted in increasing order...

    3. Write the function find sorted elt whose input is a vector sorted in increasing order and an element x. The output is 0 if the element x does not occur in v, 1 if the element x does occur in v. Below is the algorithm you should implement, known as the binary search algorithm. Note that the code below returns the index, but we want to return true or false, not the index, so adjust your code accordingly. Algorithm....

  • Suppose that binary search is called with input list: (3, 7, 13, 18, 24, 29, 31, 33, 41) and target item x = 19. What is the sequence of items in the list that x is compared to? Include the compariso...

    Suppose that binary search is called with input list: (3, 7, 13, 18, 24, 29, 31, 33, 41) and target item x = 19. What is the sequence of items in the list that x is compared to? Include the comparison made irn the base case in testing whether aowx Suppose that binary search is called with input list: (3, 7, 13, 18, 24, 29, 31, 33, 41) and target item x = 19. What is the sequence of items...

  • 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...

  • I am currently using eclipse to write in java. A snapshot of the output would be...

    I am currently using eclipse to write in java. A snapshot of the output would be greatly appreciated to verify that the program is indeed working. Thanks in advance for both your time and effort. Here is the previous exercise code: /////////////////////////////////////////////////////Main /******************************************* * Week 5 lab - exercise 1 and exercise 2: * * ArrayList class with search algorithms * ********************************************/ import java.util.*; /** * Class to test sequential search, sorted search, and binary search algorithms * implemented in...

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
ADVERTISEMENT