Question

In: Computer Science

[C++ Language] Look at the following pseudo code: Binary_search(int a[], int size) { ……….// binary search...

  1. [C++ Language] Look at the following pseudo code:

Binary_search(int a[], int size)

{

……….// binary search and return

}

Selection_Sort(int a[], int z)

{

…..// do the selection sort

}

main()

{

    Selection_Sort(array, size);

    Binary_Search(array, item);

}

Solutions

Expert Solution

#include <iostream>
using namespace std;
void Selection_Sort(int a[], int z)
{
int i,j,min_pos,temp;
for (i=0;i<z-1;i++)
{
//find the minimum element of next unsorted array   
min_pos=i;
for (j=i+1;j<z;j++)
{
if (a[j]<a[min_pos]) //if it is minimum take position
min_pos=j;
}
//swap the found minimum element with the first element
temp=a[min_pos];
a[min_pos]=a[i];
a[i]=temp;
}
}

int Binary_Search(int a[],int item,int size)
{
int mid,l,r;
l=0;
r=size;
while (l<=r) {
mid=l+(r-l)/2;
//check if the middle value is our element
if(a[mid]==item)
return mid;
//If x greater than mid check in right half
if (a[mid]<item)
l=mid+1;
//If x is smaller than mid check in left half
else
r=mid-1;
}
return -1;//if the element is not present
}
int main()
{
int a[]={5,7,1,4,9,10,3};
Selection_Sort(a,7);
int pos=Binary_Search(a,5,7);
if(pos!=-1)
cout<<"The element found at "<<pos;
else
cout<<"The element is not found";
  
return 0;
}

The Binary search function shold pass the size of array to function because it is not possible to calculate the size inside a function other than main.

comment if any doubts


Related Solutions

Write a program in C language that uses a binary search algorithm to guess a number...
Write a program in C language that uses a binary search algorithm to guess a number from 1 to 100. The computer will keep guessing until they get the users number correct.
Convert the following C++ statements to an ARM assembly language program: const int size = 10;...
Convert the following C++ statements to an ARM assembly language program: const int size = 10; int x[size] = {8, 2, 9, 6, 7, 0, 1, 3, 5, 4}; int y[size] = {399, -87, 12, 0, 42, -367, 57, 92, -1000, 25}; for i = 0; i < size; i++) if (x([ i ] > y[ i ]) z[ i ] = 0 else z[ i ] = 1;
In c++ A binary search member function of SortedType has the following prototype: void.SortedType::BinarySearch(int value, bool&...
In c++ A binary search member function of SortedType has the following prototype: void.SortedType::BinarySearch(int value, bool& found); Write the function definition as a recursive search, assuming an array-based implementation. Add a main function as a driver to test the BinarySearch. Test the edge cases.
Consider the following program: 1 #define Size 64 int A[Size; Size], B[Size; Size], C[Size; Size]; int...
Consider the following program: 1 #define Size 64 int A[Size; Size], B[Size; Size], C[Size; Size]; int register i, j; for (j = 0; j< Size; j ++) { { for (i = 0; i< Size; i++) C[i; j] = A[i; j] + B[i; j]; } } Assume that the program is running on a system using demand paging and the page size is 1 Kilobyte. Each integer is 4 bytes long. It is clear that each array requires a 16-page...
implement this search function using CPP Coding language bool binarySearch(vector<int>vec,int low,int high, int search) Test your...
implement this search function using CPP Coding language bool binarySearch(vector<int>vec,int low,int high, int search) Test your code in main
Code in C-language programming description about convert binary number to decimal number.
Code in C-language programming description about convert binary number to decimal number.
Write a MIPS assembly language program that implements the following pseudo-code operation: result = x +...
Write a MIPS assembly language program that implements the following pseudo-code operation: result = x + y – z + A[j] x and y should be in reserved memory words using the .word directive and labeled as x and y. Initialize x=10 and y=200. Read in z from the console. Input the value -8. This is the value for z, not for –z. Store this value in memory with the label z. To begin, you could just initialize z to...
c++ language Create a file program that reads an int type Array size 10; the array...
c++ language Create a file program that reads an int type Array size 10; the array has already 10 numbers, but your job is to resize the array, copy old elements of array to the new one and make it user input and add an additional 5 slots in the array, and lastly do binary search based on user input. close the file.
Please code in C language. Server program: The server program provides a search to check for...
Please code in C language. Server program: The server program provides a search to check for a specific value in an integer array. The client writes a struct containing 3 elements: an integer value to search for, the size of an integer array which is 10 or less, and an integer array to the server through a FIFO. The server reads the struct from the FIFO and checks the array for the search value. If the search value appears in...
Code using C++ A binary search tree is a data structure designed for efficient item insertion,...
Code using C++ A binary search tree is a data structure designed for efficient item insertion, deletion, and retrieval. These 3 operations share an average run time complexity of O(log(n)). Such time complexities are guaranteed whenever you are working with a balanced binary search tree. However, if you have a tree that begins leaning heavily to either its left or right side, then you can expect the performances of the insertion, deletion, and retrieval operations to degrade. As an example,...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT