Question

In: Computer Science

Given the following list of values, perform a binary search for the indicated search item. Use...

Given the following list of values, perform a binary search for the indicated search item. Use the binary search algorithm on pg. 1026 of our textbook. Show the values of first, last and middle, and the number of comparisons made after each iteration of the loop. Your should create a table like the following to show your work, where first and last are the values of the variables at the beginning of the loop, and mid is the midpoint used during that iteration, list[mid] is the value in the list at the midpoint, and No. of key comparisons should be the number of comparisons made during this iteration by the algorithm:

Iteration  first   last    mid  list[mid]   No. of key comparisons
1          0       12      ?    ?           ?

List:
[ 2, 3, 4, 4, 5, 7, 10, 14, 15, 17, 22, 23, 24 ]

Searching for value: 3

Solutions

Expert Solution

#include <stdio.h>

int main()
{
int f,l,mid,i,comp=0,key,n;
int list[20];
printf("enter no.of elements");
scanf("%d",&n);
for(i=0;i<n;i++)
scanf("%d",&list[i]);
printf("enter the element to search");
scanf("%d",&key);
f= 0;
l=n-1;
i=0;
mid=(f+l)/2;//caluculate mid
printf("Iteration\tfirst\tlast\tmid\tno.of comparisions\n");
while (f<= l) {//while the first value becomes greater than last then element not found
printf("%8d%12d%8d%8d%10d\n",i+1,list[f],list[l],list[mid],comp);//print values
i++;
if (list[mid] < key){//if mid is less than key it is in second half
f = mid+1;   
comp++;//increase comparisions
}
else if (list[mid] == key) {
comp++;//check if equals and increment comparisions
printf("%d found at location %d.\n", key, mid+1);
break;
}
else//if mid greater than key it is in first half
{
l= mid-1;
comp++;//increment comparisions
}
  
mid = (f+ l)/2;//change the mid value
}
if(f>l)
{
printf("The element not found");
}

return 0;
}


Related Solutions

Design a program which uses functions to sort a list and perform a binary search. Your...
Design a program which uses functions to sort a list and perform a binary search. Your program should: Iinitialize an unsorted list (using the list provided) Display the unsorted list Sort the list Display the sorted list. Set up a loop to ask the user for a name, perform a binary search, and then report if the name is in the list. Use a sentinel value to end the loop. Do not use the Python built in sort function to...
Perform the following arithmetic in Binary assuming 16 bit registers (67)10 + (-89)10 List the values...
Perform the following arithmetic in Binary assuming 16 bit registers (67)10 + (-89)10 List the values of the status bits: C, V, N and Z
A binary search tree can be built with a traditional insertion method given a list of...
A binary search tree can be built with a traditional insertion method given a list of integers. Binary search trees (BSTs) are binary trees where the data is ordered such that nodes in the subtree to the left of a given node are smaller than or equal to the node, and the right subtree will contain nodes with values greater than the given node. With a built binary search tree, one can traverse the tree to print each node’s data...
A binary search tree can be built with a traditional insertion method given a list of...
A binary search tree can be built with a traditional insertion method given a list of integers. Binary search trees (BSTs) are binary trees where the data is ordered such that nodes in the subtree to the left of a given node are smaller than or equal to the node, and the right subtree will contain nodes with values greater than the given node. With a built binary search tree, one can traverse the tree to print each node’s data...
Binary Search. Write a MIPS assembly program to perform a binary search on A[10], which is an array of 10 positive integers.
Binary Search. Write a MIPS assembly program to perform a binary search on A[10], which is an array of 10 positive integers. Your program should have a main routine that does the following:(a) Prompt the user to enter all the 10 integers in the array.(b) Prompt the user to enter the number to be searched.(c) Reads the integer values and makes sure it is a positive integer.(d) Prints the index of the integer. If the input is not available in...
Consider the recursive formulation of the Binary Search algorithm. Given a sorted and non-decreasing list of...
Consider the recursive formulation of the Binary Search algorithm. Given a sorted and non-decreasing list of comparable objects L, and a new item x, find the location (index) of x or indicated that x is not in L. 5a) Give a recursive algorithm for binary search. No, while, for, repeat etc statements are allowed. Only if, if else and assignment statements are allowed. 5b) Write a difference equation for the running time of your Binary Search algorithm. Solve your equation...
Create a List object that uses the binary search algorithm to search for the string "A"....
Create a List object that uses the binary search algorithm to search for the string "A". Display a message box indicating whether the value was found. Language: C#
A search for “car” on a shopping website gave a list of the following items: Item...
A search for “car” on a shopping website gave a list of the following items: Item Department Unit Price Average Reviews Hot Wheels 9-Car Gift Pack (Styles May Vary) Toy $9 5 Graco Nautilus 3-in-1 Car Seat, Matrix Baby $140 4 ... ... ... ... You can use the following example problem as a model. What are the “individuals” for this data set? 1-Kids 2-Customers 3-Items for sale on this website 4-Shopping website Which of the following statements is NOT...
Suppose I have a list of 128 unsorted numbers. If I use the binary search to...
Suppose I have a list of 128 unsorted numbers. If I use the binary search to search it, how many comparisons will I have to do in the worst case, assuming I use a quadratic algorithm to sort the list first.
Given the array a and the binary search function below, list all ACTIVATIONS IN FINDING t=19....
Given the array a and the binary search function below, list all ACTIVATIONS IN FINDING t=19. (15 Points) -9 -5 -2 0 1 3 7 11 17 19 21 25 27 31 37 41 a    index 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 int search(int a[], int t, int l, int r){ if(l<=r){ int m=(l+r)/2; if(t==a[m]) return m; else if (t<a[m]) return search(a, t, l,m-1); else return search(a, t, m+1,...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT