Question

Problem: I am trying to implement a binary search function in Visual Studio 2019 using C#....

Problem:

I am trying to implement a binary search function in Visual Studio 2019 using C#. This is my first program in C# and I translated it from Java.

In this program, I am to carry out the same 10,000,000 unsuccessful searches for eight different-sized arrays, namely arrays of sizes, 128, 512, 2048, 8192, 32768, 131072, 524288, and 2,097,152. I need to measure each of the three programs and the time it takes to do the 10,000,000 searches for each of the eight arrays. Compare the timings to the theoretical timings the algorithm binary search provides.

Please make corrections to my programs below so that it may successfully 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);
           return BinarySearch(arr, mid + 1, r, x);
       }
       return -1;
   }

   public class Problem5InC#
   {

       public static void main(String[] args)
       {

           BinarySearch obj = new BinarySearch();
           int size = 2097152;
           int arr[] = new int[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();
           long elapsed_time = end - start;
           Console.write("elapsed_time");
       }
   }

0 0
Add a comment Improve this question Transcribed image text
Answer #1

using System;

using System.Diagnostics;

class MainClass

{

static int search(int[] arr, int l, int r, int x)

{

if (r >= 1)

{

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

if (arr[mid] == x)

return mid;

if (arr[mid] > x)

return search(arr, 1, mid - 1, x);

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

}

return -1;

}

public static void Main (string[] args) {

{

int size = 2097152;

int[] arr = new int[size];

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

{

arr[i] = 0;

}

int x = 1;

int result = 0;

Stopwatch stopwatch = new Stopwatch();

stopwatch.Start();

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

{

result = search(arr, 0, size - 1, x);

}

stopwatch.Stop();

TimeSpan stopwatchElapsed = stopwatch.Elapsed;

Console.WriteLine(Convert.ToInt32(stopwatchElapsed.TotalMilliseconds));

}

}

}


Add a comment
Know the answer?
Add Answer to:
Problem: I am trying to implement a binary search function in Visual Studio 2019 using C#....
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Problem: The code I have below compiled/debugged using C#, however I need to measure the time...

    Problem: The code I have below compiled/debugged using C#, however I need to measure the time it takes to do the 10,000,000 searches for each of the eight arrays. Could you compare the timings to the theoretical timings the algorithm binary search provides. Instructions prior to this: Implement a binary search function in C#. In this program we are to carry out the same 10,000,000 unsuccessful searches for eight different-sized arays, namely arrays of sizes 128, 512, 2,048, 8,192, 32,768,...

  • implement a binary search function in 3 programming languages. In each program (identical, except for the...

    implement a binary search function in 3 programming languages. In each program (identical, except for the programming language), carry out the same 20,000,000 unsuccessful searches for eight different-sized arrays, namely arrays of sizes 128, 512, 2048, 8192, 32768, 131072, 524288, and 2,097,152. Measure in each of the three programs the time it takes to do the 20,000,000 searches for each of the eight arrays. Compare these timings to the theoretical timings the algorithm binary search provides. Are there differences between...

  • In C++, make the following binary search function work on an array of strings to give...

    In C++, make the following binary search function work on an array of strings to give you the index location of the string you want in the array. It currently works on integers only. int binarySearch(int arr[], int firstIndex, int lastIndex, int target){ int index; if (firstIndex>lastIndex) index = -1; else { int mid = firstIndex + (lastIndex - firstIndex) / 2; if (target == arr[mid]) index = mid; else if (target < arr[mid]) index = binarySearch(arr, firstIndex, mid -...

  • Write a Java application that implements the recursive Binary Search alogrithm below: Write a Java application...

    Write a Java application that implements the recursive Binary Search alogrithm below: Write a Java application that implements the recursive Binary Search alogrithm below:/** * Performs the standard binary search using two comparisons * per level. This is a driver that calls the recursive method * @return index where item is found or NOT FOUND if not found */public static <AnyType extends Comparable<? super AnyType>> int binarySearch(AnyType [] a, AnyType x) {return binarySearch(a, x, 0, a.length -1);}/** * Hidden recursive...

  • Given the following code for a binary search, how many times this method have to be...

    Given the following code for a binary search, how many times this method have to be executed in order to find the number 5? A) 1 time B) 2 times C) 3 times D) 4 times E) more than 5 times public class BinarySearch{ public static void main(String []args){ if (right >= left) { int middle = left + (right - left) / 2; if (arr[middle] == num) return middle; if (arr[mid] > num) return binarySearch(arr, left, middle - 1,...

  • Having issues using binary search on a pointer array. 1. Write 1000 random ints to file...

    Having issues using binary search on a pointer array. 1. Write 1000 random ints to file 2. Binary Search to find if number exists in element from file. int main() { ArrayActions action;   int count; int userInput; int num = 1000; int array[num]; ofstream myFile ("/Users/chan/Desktop/LANEY_CIS27/Assignemtn2_CIS27/Assignemtn2_CIS27/File.txt");   //Set srand with time to generate unique random numbers srand((unsigned)time(0));   //Format random num up to 999 for(count = 0; count < num; count++) { array[count] = rand() % 1000; }          //Condition...

  • An easier way to do the Binary Search part or a simplistic way for beginners. Not...

    An easier way to do the Binary Search part or a simplistic way for beginners. Not using such high code for the assignment in general also commenting on what each line does. COMMENT ON the code below .what does each part do for the program? and a better and more simple and basic way to do binary search and get same results. #include <iostream> #include <fstream> using namespace std; void swap(int *xp, int *yp) { int temp = *xp; *xp...

  • without coding Give the Big O run-time of the following algorithms. Binary Search: def binary-search (arr,...

    without coding Give the Big O run-time of the following algorithms. Binary Search: def binary-search (arr, low, high, x): # Check base case if low > high : return None else: mid = (high + low) // 2 element arr[mid] == X: if element return mid elif element > X: return binary-search(arr, low, mid 1, x) else: return binary_search(arr, mid + 1, high, x) Selection Sort: def selection_sort (arr): for i in range (len(arr)): smallest index = i smallest value...

  • Given the binary search function, answer following question: int binarySearch(int a[], int size, int target, int...

    Given the binary search function, answer following question: int binarySearch(int a[], int size, int target, int low, int high) { while (low <= high) { int mid = (low + high) / 2; if (a[mid] == target) return mid; else if (a[mid] < target) low = mid + 1; else high = mid 1: } return -1; } 1) If array a[] = {4, 5, 18, 25, 66, 70, 78}, size = 7, target = 71, low = 0, high...

  • 6.3.1 [10] <§6.2> Consider the following binary search algorithm (a classic divide and conquer algorithm) that...

    6.3.1 [10] <§6.2> Consider the following binary search algorithm (a classic divide and conquer algorithm) that searches for a value X in a sorted N-element array A and returns the index of matched entry: BinarySearch(A[0..N−1], X) { low = 0 high = N −1 while (low <= high) { mid = (low + high) / 2 if (A[mid] >X) high = mid −1 else if (A[mid] <X) low = mid + 1 else return mid // found } return −1...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT