Question: I need a C sharp code to do this: I have the code but it does bonary search, I need the binary search repkaced with

I need a C sharp code to do this: "I have the code but it does bonary search, I need the binary search repkaced with INSERTION CODE"

  • Use the insertion sort algorithm
  • Use an array of 100 random integers between (1-100) as the base for the sort
  • Run the tests 10 times with a new set of random values
  • Calculate the average "loop count" (you have to figure out what to instrument)
  • Determine the average execution time in milliseconds

    using System;

    using System.Diagnostics;

    class Program

    {

    //I START TO DO BINARY SEARCH//RETURN THE INDEX IF FOUND & -1 IF NOT FOUND//UPDATE REFERENCE PARAMETER LEVELS & THE NUMBER OF LEVELS IT TOOK

    static int binarySearch(int[] array, int target, ref int levels)

    {

    //THE LEVELS SHOULD RESETTING

    levels = 0;

    //THE VARIABLES SHOULD DECLARING AS LOW, MIDDLE, HIGH

    int low = 0, high = array.Length - 1, mid;

    //LOOPING & LOOPING UNTIL MY LOW REACH NEAR TO MY HIGH

    while (low <= high)

    {

    //LEVELS SHOULD UPDATING

    levels++;

    //FINDING, START TO FIND MY MIDDLE INDEX

    mid = (low + high) / 2;

    //CHECKING, MAKING SURE IF MY ELEMENT AT THE MIDDLE IS MY TARGET ELEMENT OR NOT

    if (array[mid] == target)

    {

    //YEEEEEY, FOUND IT HERE

    return mid;

    }

    else if (array[mid] < target)

    {

    //NOPE, SEARCHING RIGHT-SIDE-HALF OF ITERATION

    low = mid + 1;

    }

    else

    {

    //NOPE, SEARCHING LEFT-SIDE-HALF OF ITERATION

    high = mid - 1;

    }

    }

    //AH, NOPE, NOT FOUND, SHOUT UP

    return -1;

    }

    //CONTINUE

    static void Main(string[] args)

    {

    //CREATING, A GENERATOR OF RANDOM NUMBERS

    Random rand = new Random();

    //100 ELEMENTS ARRAY, POUR RANDOM NUMBERS BETWEEN 0 TO 99

    int[] array = new int[100];

    for (int i = 0; i < array.Length; i++)

    {

    array[i] = rand.Next(100);

    }

    //SORTING, "NOT DOING THINGS WITHOUT SORTING."

    Array.Sort(array);

    //DISPLAYING SORTED ARRAY

    Console.WriteLine("Sorted:");

    for (int i = 0; i < array.Length; i++)

    {

    Console.Write(array[i] + " ");

    }

    Console.WriteLine();

    //The second part of the code, right in the middle

    double sumTimes = 0, avgTimes, avgLevels;

    int sumLevels = 0, level = 0, maxLevel = 0, index = 0, target = 0;

    //STOPWATCH, RECORD RUNING TIME AS I DRIVE FROM HERE TO SAN ANTONIO I SHOULD NOT STOP FOR A BURGER

    Stopwatch watch;

    //10 TIMES LOOPING

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

    {

    //GENERATING A SINGLE RANDOM-VALUE FOR THE SEARCH PURPOSES

    target = rand.Next(100);

    //READY, SIT, BEGIN: STARTING STOPWATCH, INITIALIZING STOPWATCH FIRST

    watch = new Stopwatch();

    watch.Start();

    //PERFORMING THE GREAT DEAL OF BINARY SEARCH, "MAKE THE BINARY SEARCH GREAT AGAIN!"

    index = binarySearch(array, target, ref level);

    //STOPPING MY TIMER, I GOT IT

    watch.Stop();

    //Believe me, i could "ElapsedMilliseconds" easy as probably the others are using as the value,

    //but with using that the value will be too low and the system may consider as extreme close to 0,

    //therefore, I ADDING TICKS TAKEN IT TO MY SUM TIMES

    sumTimes += watch.ElapsedTicks;

    //MAXIMUM LEVEL SHOULD BE UPDATING IN THIS STEP, CONDITIONAL

    if (level > maxLevel)

    {

    maxLevel = level;

    }

    //LEVELS ADDING TO SUM LEVELS

    sumLevels += level;

    //DISPLAYING CURRENT SEARCH IN THE MIDDLE OF MY OUTPUT

    Console.WriteLine("target={0}, index={1}, level={2}", target, index, level);

    }

    //AVERAGE TIME DETERMINATION, FINDING MY AVERAGE TIME TOOK FOR MY BINARY SEARCH

    avgTimes = (double)sumTimes / 10;

    //I FOUND THIS SPECIFIC PART TO CONVERT AVERAGE TIME TO MILLISECOND AS REQUIREMENT

    avgTimes = avgTimes / TimeSpan.TicksPerMillisecond;

    //FINDING AVERAGE LEVEL

    avgLevels = (double)sumLevels / 10;

    //DISPLAYING, DISPLAYING, DISPLAYING

    Console.WriteLine("Binary search: {0} ms, average level={1}, max level={2}", avgTimes, avgLevels, maxLevel);

    //REMOVE IF YOU ARE DONE! PRESS ANY KEY TO QUITE

    Console.ReadKey();

    }

    }

- Use the insertion sort algorithm
- Use an array of 100 random integers between (1-100) as the base for the sort
Run the tests 10 times with a new set of random values
- Calculate the average "loop count" (you have to figure out what to instrument)
- Determine the average execution time in milliseconds
******I need to use insertion sort to SORT 100 random numbers between 0 to 100 and determine how long it takes to sort those 100 numbers; i need to run 10 times to calculate the average sort.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!