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 binary search algorithm in C language by performing following steps: Prompt the user to...
Write a binary search algorithm in C language by performing following steps: Prompt the user to enter the number of array elements (say, N).   Read the N different values (define these values to be of type integer).   Read the element (integer value) which you want to search. Invoke a function to display the values in the array. The function should take the array reference and number of elements as arguments and should have the return type as void. The function...
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...
Complete ArrayCollection.java with following methods (Please complete code in java language): public ArrayCollection(); public ArrayCollection(int size);...
Complete ArrayCollection.java with following methods (Please complete code in java language): public ArrayCollection(); public ArrayCollection(int size); public boolean isEmpty(); public boolean isFull(); public int size(); // Return number of elements in the Collection. public String toString(); public boolean add(T element); // Add an element into the Collection, return true if successful public boolean remove(T target); // Remove the target from Collection, return true if successful public boolean removeAll(T target); // Remove all occurrences of Target, return if successful public void...
Assignment #1: Sorting with Binary Search Tree (IN C LANGUAGE) Through this programming assignment, the students...
Assignment #1: Sorting with Binary Search Tree (IN C LANGUAGE) Through this programming assignment, the students will learn to do the following: 1. Know how to process command line arguments. 2. Perform basic file I/O. 3. Use structs, pointers, and strings. 4. Use dynamic memory. This assignment asks you to sort the lines of an input file (or from standard input) and print the sorted lines to an output file (or standard output). Your program, called bstsort (binary search tree...
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.
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.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT