1answer.
Ask question
Login Signup
Ask question
All categories
  • English
  • Mathematics
  • Social Studies
  • Business
  • History
  • Health
  • Geography
  • Biology
  • Physics
  • Chemistry
  • Computers and Technology
  • Arts
  • World Languages
  • Spanish
  • French
  • German
  • Advanced Placement (AP)
  • SAT
  • Medicine
  • Law
  • Engineering
fgiga [73]
3 years ago
12

You are asked to simulate a binary search algorithm on an array of random values.An array is the list of similar type of element

s. For the sake of comparison use an integer array for implementing the search.
Hint: For a binary search you need the array as sorted and so apply the selection sort first and then apply the binary search.
Syntax of an array:
int numbers[10];
int numbers[] = { 10, 20, 30, 40 };
int numbers[4] = { 10, 20, 30, 40 };
int numbers[3] = { }; int numbers[3] = {0};
To read a sequence of numbers in an array:
for (int i =0; i> arr[i];}
Similarly to print an array:
for( int i = 0; i {cout<<"The array element at position " < A report should include the following information
Problem statement
Should have a short statement of the problem, object or purpose
Input/output description
Algorithm and Flowchart
Program listing
Sample test run of the program
Conclusions
All C++ programming project

Computers and Technology
1 answer:
Alex Ar [27]3 years ago
8 0

Answer:

Explanation:

Problem statement:

to simulate a binary search algorithm on an array of random values.

Binary Search: Search a sorted array by repeatedly dividing the search interval in half. Begin with an interval covering the whole array. If the value of the search key is less than the item in the middle of the interval, narrow the interval to the lower half. Otherwise narrow it to the upper half. Repeatedly check until the value is found or the interval is empty.

Input/output description

Input:

Size of array: 4

Enter array:10  20 30 40

Enter element to be searched:40

The Output will look like this:

Element is present at index 3

Algorithm and Flowchart:

We basically ignore half of the elements just after one comparison.

Compare x with the middle element.

If x matches with middle element, we return the mid index.

Else If x is greater than the mid element, then x can only lie in right half subarray after the mid element. So we recur for right half.

Else (x is smaller) recur for the left half.

The Flowchart can be seen in the first attached image below:

Program listing:

// C++ program to implement recursive Binary Search

#include <bits/stdc++.h>

using namespace std;

// A recursive binary search function. It returns

// location of x in given array arr[l..r] is present,

// otherwise -1

int binarySearch(int arr[], int l, int r, int x)

{

   if (r >= l) {

       int mid = l + (r - l) / 2;

       // If the element is present at the middle

       // itself

       if (arr[mid] == x)

           return mid;

       // If element is smaller than mid, then

       // it can only be present in left subarray

       if (arr[mid] > x)

           return binarySearch(arr, l, mid - 1, x);

       // Else the element can only be present

       // in right subarray

       return binarySearch(arr, mid + 1, r, x);

   }

   // We reach here when element is not

   // present in array

   return -1;

}

int main(void)

{ int n,x;

cout<<"Size of array:\n";

cin >> n;

int arr[n];

cout<<"Enter array:\n";

for (int i = 0; i < n; ++i)

{ cin >> arr[i]; }

cout<<"Enter element to be searched:\n";

cin>>x;

int result = binarySearch(arr, 0, n - 1, x);

   (result == -1) ? cout << "Element is not present in array"

                  : cout << "Element is present at index " << result;

   return 0;

}

The Sample test run of the program can be seen in the second attached image below.

Time(sec) :

0

Memory(MB) :

3.3752604177856

The Output:

Size of array:4

Enter array:10  20 30 40

Enter element to be searched:40

Element is present at index 3

Conclusions:

Time Complexity:

The time complexity of Binary Search can be written as

T(n) = T(n/2) + c  

The above recurrence can be solved either using Recurrence T ree method or Master method. It falls in case II of Master Method and solution of the recurrence is Theta(Logn).

Auxiliary Space: O(1) in case of iterative implementation. In case of recursive implementation, O(Logn) recursion call stack space.

You might be interested in
Alexa it is olewi.............................................
Sladkaya [172]

Answer:

jvksvksdvkbvkjsdkcn

Explanation:

6 0
3 years ago
Read 2 more answers
In Python, which comparison operator means "less than or equal to"?
Komok [63]

Answer:

<=

Explanation:

Pretty much the same as in maths

3 0
3 years ago
Read 2 more answers
What time is it in Ohio?
skad [1K]
Geographically, the state of Ohio falls within the lines of latitude of 75 °W and 90 °W. The timezone corresponding to this portion of the globe is eastern standard time (EST). The prime meridian at 0 °, has the timezone greenwich mean time (GMT) from which all other timezones are referenced. And eastern standard time, local time in Ohio, will be 5 hours behind GMT.
5 0
3 years ago
Read 2 more answers
How to delete audio from powerpoint​
Marta_Voda [28]

Answer:

To delete an audio clip, select the audio icon on the slide and press Delete.

Hope it helps!!!!

4 0
3 years ago
John is analyzing strange behavior on computers in his network. He believes there is malware on the machines. The symptoms inclu
enot [183]

Answer:

Boot Sector Virus

Explanation:

A malicious software or malware is an executable line of code, programmed by a cybercriminal for ill intentions. There are many types of malware namely; viruses, rootkit, keylogger, trojan horse etc.

A boot sector virus is a kind of malware that runs before the operating system, affecting the boot sector of the hard disk, so even when a linux live cd is running as the operating system, the virus is still active.

5 0
3 years ago
Other questions:
  • I need a idea of a origami for my coding class and it needs to be easy to make
    15·2 answers
  • A layer of control in each communicating device that provides functions such as flow control, error detection, and error control
    13·1 answer
  • 6. A distribution consists of three components with frequencies 200, 250 and 300 having means
    14·1 answer
  • How to get the absolute value in coding begginners?
    13·2 answers
  • Which type of image uses lossy compression to reduce file size?
    14·1 answer
  • What is work flexibility?
    15·1 answer
  • Ports that are generally used to establish outbound connections are known as ___ ports.
    14·1 answer
  • Choose the correct term to complete the sentence.
    12·1 answer
  • In _____, a program running on a Web server creates a Web page in response to a request for specific information from a Web clie
    7·1 answer
  • Cyber vulnerabilities to dod systems may include
    12·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!