Question

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, 131,072, 524,288, and 2,097,152.

Code:

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));

}

}

}

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

Theoretical time depends upon number of operations cpu can handle.

Assuming CPU frequency = 1Ghz = 10^9

Binary search require O(logn) operations

Total searches = 10,000,000

total time = 10,000,000*logn in nanoseconds = 10*logn milliseconds

n=128

logn = 7

time = 70ms

n=512

logn = 9

time = 90ms

n=2,048

logn = 11

time = 110ms

n=8,192

logn = 13

time = 130ms

n=32,768

logn = 15

time = 150ms

n=131,072

logn = 17

time = 170ms

n=524,288

logn = 19

time = 190ms

n=2,097,152

logn = 21

time = 210ms

Program (Similar to yours)

using System;
using System.Diagnostics;
class Program{

static int search(int[] arr, int l, int r, int x){
if (r >= l){
int mid = (l+r) / 2;
if (arr[mid] == x)
return mid;
if (arr[mid] > x)
return search(arr, l, 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("Time:");
Console.WriteLine(Convert.ToInt32(stopwatchElapsed.TotalMilliseconds));
}
}

Add a comment
Know the answer?
Add Answer to:
Problem: The code I have below compiled/debugged using C#, however I need to measure the time...
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
  • need help editing or rewriting java code, I have this program running that creates random numbers...

    need help editing or rewriting java code, I have this program running that creates random numbers and finds min, max, median ect. from a group of numbers,array. I need to use a data class and a constructor to run the code instead of how I have it written right now. this is an example of what i'm being asked for. This is my code: import java.util.Random; import java.util.Scanner; public class RandomArray { // method to find the minimum number in...

  • 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,...

  • I need to program 3 and add to program 2 bellows: Add the merge sort and...

    I need to program 3 and add to program 2 bellows: Add the merge sort and quick sort to program 2 and do the same timings, now with all 5 sorts and a 100,000 element array. Display the timing results from the sorts. DO NOT display the array. ____________________>>>>>>>>>>>>>>>>>>>>___________________________ (This is program 2 code that I did : ) ---->>>>>> code bellow from program 2 java program - Algorithms Write a program that randomly generates 100,000 integers into an array....

  • 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...

  • [C#] I need to convert this if/else statements into Switch I have the code for the...

    [C#] I need to convert this if/else statements into Switch I have the code for the if/else but now need to turn it into a switch. The Console.WriteLine and all that is fine. I just need to convert the if/else.      int x1, x2, y1, y2;                 Console.WriteLine("What is smallest value of x:");                 x1 = Convert.ToInt32(Console.ReadLine());                 Console.WriteLine("What is largest value of x:");                 x2 = Convert.ToInt32(Console.ReadLine());                 Console.WriteLine("What is smallest value of y:");                 y1 = Convert.ToInt32(Console.ReadLine());                 Console.WriteLine("What is largest value of y:");                 y2 =...

  • Hello, I need help with my code. The code needs to display random number from 1...

    Hello, I need help with my code. The code needs to display random number from 1 to 50 every time the program runs but the program displays the same random numbers every time. Thanks #include #include using namespace std; void dynAlloc(int size, int *&arr); void displayArray(int *arr, int n); void insertionSort(int *arr, int n, int *temp); void linear_search(int *arr, int n, int key); void binary_search(int *arr, int n, int key); int main() {   const int n = 50; int *arr,...

  • I need a java flowchart for the following code: import java.util.*; public class Main {   ...

    I need a java flowchart for the following code: import java.util.*; public class Main {    public static void main(String[] args) {    Scanner sc=new Scanner(System.in);           System.out.print("Enter the input size: ");        int n=sc.nextInt();        int arr[]=new int[n];        System.out.print("Enter the sequence: ");        for(int i=0;i<n;i++)        arr[i]=sc.nextInt();        if(isConsecutiveFour(arr))        {        System.out.print("yes the array contain consecutive number:");        for(int i=0;i<n;i++)        System.out.print(arr[i]+" ");       ...

  • I need this program converted to c++ Please help if you can import java.util.*; // Add...

    I need this program converted to c++ Please help if you can import java.util.*; // Add two arrays of the same size (size) // Each array is a representation of a natural number // The returned array will have the size of (size + 1) elements public class Fibonacci { private static String arrToString(int[] arr) {           String s = "";           for (int i = 0; i < arr.length; i++) {               s = s + arr[i];           }...

  • How can I convert the following C code to MIPS Assembly? +++++++++++++++++++++++++++++++++ MIPS main program ++++++++++++++++++++++++++++++++...

    How can I convert the following C code to MIPS Assembly? +++++++++++++++++++++++++++++++++ MIPS main program ++++++++++++++++++++++++++++++++ .data # Defines variable section of an assembly routine. array: .word x, x, x, x, x, x, x, x, x, x # Define a variable named array as a word (integer) array # with 10 unsorted integer numbers of your own. # After your program has run, the integers in this array # should be sorted. .text # Defines the start of the code...

  • (10 pts)3-1. Given the following piece of code #define SIZE 10 include <iostream> using namespace std;...

    (10 pts)3-1. Given the following piece of code #define SIZE 10 include <iostream> using namespace std; // sorted array in descending order int list[SIZE] (23, 19, 17, 13, 11, 7, 5, 3, 1, 0); // recursively binary search the array list for key // return the index to list if key is found. else return -1 int recBinarySearch (int key) // Please implement the recursive function.. Please implement the C++ function recBinarySearch that recursively binary searches the value key in...

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
ADVERTISEMENT