Question

In: Computer Science

// This program demonstrates a Binary Search, which search for a value // in an array,...

// This program demonstrates a Binary Search, which search for a value

// in an array, assuming that the array is sorted in descending order.

// You have to modify the function binarySearch() to search for a value

// in an array that is sorted in ascending order.

// NOTES:

// Uncomment line 34 and comment line 32. You don't have to edit anything

// else in the main(), just in the binarySearch() function.

// EXAMPLES (using the array sorted in ascending order)

// Input: Enter an integer to search for: 12

// Output: The value 12 is in position number 8 of the array

// Input: Enter an integer to search for: 2

// Output: The value 2 is in position number 2 of the array

// Input: Enter an integer to search for: 19

// Output: The value 19 is in position number 14 of the array

#include<iostream>

using namespace std;

int binarySearch(int [], int, int); // function prototype

const int SIZE = 16;

int main()

{

        int found, value;

        // Array to be search, in descending order

        int array[] = {34,19,19,18,17,13,12,12,12,11,9,5,3,2,2,0}; // Comment this line

        // Array to be search, ascending order

        //int array[] = {0,2,2,3,5,9,11,12,12,12,13,17,18,19,19,34}; // Uncomment this line

        cout << "Enter an integer to search for: ";

        cin >> value;

        found = binarySearch(array, SIZE, value); //function call to perform the binary search

                                                  //on array looking for an occurrence of value

        if (found == -1)

               cout << "The value " << value << " is not in the array" << endl;

        else

        {

               cout << "The value " << value << " is in position number "

                    << found + 1 << " of the array" << endl;

        }

        return 0;

}

//*******************************************************************

//                     binarySearch

// task:               This searches an array for a particular value

// data in:       List of values in an orderd array, the number of

//                elements in the array, and the value searched for

//                in the array

// data returned: Position in the array of the value or -1 if value

//                not found

//*******************************************************************

int binarySearch(int array[],int numElems,int number)

{

        int first = 0;                            // Index of first element of array

        int last = numElems - 1;           // Index of last element of the array

        int middle;                                       // Index of current middle value of the array

        while (first <= last)

        {

               middle = first + (last - first) / 2;

               if (array[middle] == number)

                       return middle;                 // If value is in the middle, we are done

               else if (array[middle] < number)

                       last = middle - 1;             // Ignore second half of array and search the first   

               else // (array[middle] > number)

                       first = middle + 1;            // Ignore first half of array and search the second

        }   

        return -1;                                    // Indicates that value is not in the array

}

Solutions

Expert Solution

#include<iostream>
using namespace std;
int binarySearch(int [], int, int); // function prototype
const int SIZE = 16;
int main()
{
int found, value;
// Array to be search, in descending order
//int array[] = {34,19,19,18,17,13,12,12,12,11,9,5,3,2,2,0}; // Comment this line
// Array to be search, ascending order
int array[] = {0,2,2,3,5,9,11,12,12,12,13,17,18,19,19,34}; // Uncomment this line
cout << "Enter an integer to search for: ";
cin >> value;
found = binarySearch(array, SIZE, value); //function call to perform the binary search
//on array looking for an occurrence of value
if (found == -1)
cout << "The value " << value << " is not in the array" << endl;
else{
cout << "The value " << value << " is in position number "
<< found + 1 << " of the array" << endl;
}
return 0;
}
//*******************************************************************
// binarySearch
// task: This searches an array for a particular value
// data in: List of values in an orderd array, the number of
// elements in the array, and the value searched for
// in the array
// data returned: Position in the array of the value or -1 if value
// not found
//*******************************************************************
int binarySearch(int array[],int numElems,int number){
int first = 0; // Index of first element of array
int last = numElems - 1; // Index of last element of the array
int middle; // Index of current middle value of the array
while (first <= last){
middle = (first + last)/ 2;
if (array[middle] == number)
return middle; // If value is in the middle, we are done
else if (array[middle] < number)
first = middle + 1; // if the value is in second half then take the "first as middle+1"   
else // (array[middle] > number)
last = middle - 1; // If the value is in first hlaf then take the last index as middle-1"
}   
return -1; // Indicates that value is not in the array
}

--------------------------------------------------------------------------------------

explanation: if the values in array are ascending order then

while (first <= last){
middle = (first + last)/ 2; //mid value index
if (array[middle] == number)
return middle; // If value is in the middle, we are done
else if (array[middle] < number)
first = middle + 1; // if the value is in second half then take the "first index as middle+1"   
else // (array[middle] > number)
last = middle - 1; // If the value is in first half then take the "last index as middle-1"
}

if the value is matched with middle then done ,if the value is greater than array[middle] then value greater so value is in second half next time we will find mid as second half first index + second half last index SO first+middle+1. If the value is less than array[mid] then value is in first half so we will calculate mid as first half first index + first half last index .so last=middle-1


Related Solutions

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...
// This program demonstrates a Binary Search //PLACE YOUR NAME HERE #include<iostream> using namespace std; int...
// This program demonstrates a Binary Search //PLACE YOUR NAME HERE #include<iostream> using namespace std; int binarySearch(int [], int, int);  // function prototype const int SIZE = 16; int main() { int found, value; int array[] = {34,19,19,18,17,13,12,12,12,11,9,5,3,2,2,0}; // array to be searched cout << "Enter an integer to search for:" << endl; cin >> value; found = binarySearch(array, SIZE, value); //function call to perform the binary search   //on array looking for an occurrence of value if (found == -1) cout...
(a) Consider the general k-ary search algorithm (generalization of binary search) which splits a sorted array...
(a) Consider the general k-ary search algorithm (generalization of binary search) which splits a sorted array of size n into k subsets each of size n/k and recursively searches only one of these k subsets. Which one of these k subsets should be searched is decided by making (k − 1) comparisons, each of which can be done in constant time. Clearly, the recurrence relation for this k-ary search algorithm can be written as, T(n) = T(n/k) + (k −...
Given an array storing integers ordered by value, modify the binary search routine to return the...
Given an array storing integers ordered by value, modify the binary search routine to return the position of the integer with the greatest value less than K when K itself does not appear in the array. Return ERROR if the least value in the array is greater than K.
Implement a recursive binary search on an array of 8-byte integers in LEGV8
Implement a recursive binary search on an array of 8-byte integers in LEGV8
Given an array of foods, create a binary search tree. Then, make a copy of that...
Given an array of foods, create a binary search tree. Then, make a copy of that BST and balance it. Language is C++. Vectors are not allowed. The balance function definition: void balance(BST treeObj); and then when writing the function: void BST::balance(BST treeObj) where BST is a class. The function will be called in main like: originalTree.balance(balancedTree);
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...
Binary search for K=12 in the array A={2, 3, 5, 7,11,15, 16,18,19}
Binary search for K=12 in the array A={2, 3, 5, 7,11,15, 16,18,19}
Write a program to show the difference between linear search and binary search. Show the input...
Write a program to show the difference between linear search and binary search. Show the input test data for your program and the output produced by your program which clearly show that binary search is faster than linear search
Assume you need to write a Java program that uses a binary search algorithm to search...
Assume you need to write a Java program that uses a binary search algorithm to search a sorted array for a given value. 1. Write a Java pseudocode that uses recursion to accomplish the task. Here is a hint. When you are searching for a particular value in an array, there are two possible outcomes. 1) The value is found and the array index of that value is returned. 2) The value is not found and we return -1. (5...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT