Problem Trying Implement Binary Search Function Visual Studio 2019 Using C First Program Q43894907
Problem:
I am trying to implement a binary search function in VisualStudio 2019 using C#. This is my first program in C# and Itranslated it from Java.
In this program, I am to carry out the same 10,000,000unsuccessful searches for eight different-sized arrays, namelyarrays of sizes, 128, 512, 2048, 8192, 32768, 131072, 524288, and2,097,152. I need to measure each of the three programs and thetime it takes to do the 10,000,000 searches for each of the eightarrays. Compare the timings to the theoretical timings thealgorithm binary search provides.
Please make corrections to my programs below so that it maysuccessfully debug/run.
Code:
using System;
class BinarySearch
{
int BinarySearch(int arr[], int 1, int r, int x)
{
if (r >= 1)
{
int mid = 1 + (r- 1) / 2;
if (arr[mid] ==x)
return mid;
if (arr[mid]> x)
return BinarySearch(arr, 1, mid – 1, x);
returnBinarySearch(arr, mid + 1, r, x);
}
return -1;
}
public class Problem5InC#
{
public static void main(String[]args)
{
BinarySearchobj = new BinarySearch();
int size =2097152;
int arr[] = newint[size];
for (int i = 0;i < size; i++)
{
arr[i] = 0;
}
int x = 1;
int result =0;
long start =System.currentTimeMillis();
for (int i = 0;i < 10000000; i++)
{
result = obj.BinarySearch(arr, 0, size – 1,x);
}
long end =System.currentTimeMillis();
longelapsed_time = end – start;
Console.write(“elapsed_time”);
}
}
Expert Answer
Answer to Problem: I am trying to implement a binary search function in Visual Studio 2019 using C#. This is my first program in C…
OR