Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8636371
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T10:13:05+00:00 2026-06-12T10:13:05+00:00

I have the task of creating two seperate programs, one linear search program, which

  • 0

I have the task of creating two seperate programs, one linear search program, which I have already completed, and a binary search program. These programs must also count the number of comparisons made during the search process. My linear search program already counts the number of comparisons made while my binary search program cannot. The code for the binary search looks like this:

using System;
using System.Collections.Generic;

public class Example
{
public static void Main()
{

    Console.WriteLine("Input number you would like to search for");

    String Look_for = Console.ReadLine();

    int Lookfor;

    int.TryParse(Look_for, out Lookfor);

    {
        List<int> numbers = new List<int>();

        numbers.Add(1); 
        numbers.Add(2); 
        numbers.Add(3); 
        numbers.Add(4); 
        numbers.Add(5); 
        numbers.Add(6); 
        numbers.Add(7); 
        numbers.Add(8); 

        Console.WriteLine();
        foreach (int number in numbers)
        {
            Console.WriteLine(number);
        }

        int answer = numbers.BinarySearch(Lookfor);

        Console.WriteLine("The numbers was found at:");

        Console.WriteLine(answer);

    }
 }
}

If anybody can tell me how to modify it to count comparisons it would be greatly appreciated.

Many thanks, Matthew.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T10:13:05+00:00Added an answer on June 12, 2026 at 10:13 am

    You can use this extension method (code based on this answer)

    public static class ListEx
    {
        public static Tuple<int, int> BinarySearchWithCount<T>(
            this IList<T> list, T key)
        {
            int min = 0;
            int max = list.Count - 1;
            int counter = 0;
    
            while (min <= max)
            {
                counter++;
                int mid = (min + max) / 2;
                int comparison = Comparer<T>.Default.Compare(list[mid], key);
                if (comparison == 0)
                {
                    return new Tuple<int, int>(mid, counter);
                }
                if (comparison < 0)
                {
                    min = mid + 1;
                }
                else
                {
                    max = mid - 1;
                }
            }
    
            return new Tuple<int, int>(~min, counter);
        }
    }
    
    //Which returns a tuple with the item and a number of comparisons. 
    //Here is how you can use it:
    
    class Program
    {
        static void Main(string[] args)
        {
            var numbers = new List<int>();
            numbers.AddRange(Enumerable.Range(0, 100000));
    
            var answer = numbers.BinarySearchWithCount(2);
            Console.WriteLine("item: " + answer.Item1);   // item: 2
            Console.WriteLine("count: " + answer.Item2);  // count: 15
    
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have task to prepare two windows with swing. One contains grid of squares,
I have two app domains, one parent creating the child domain. In the child
I have the following task: Create one website which is accessible with 20 different
I have the task of creating implementations for a large number of metric data
I have found that creating a zip file using the Zip task provided by
i have task which takes a parameter and has three modes of results Example
I'm using dhtmlx Gantt Chart UI component which have task list and graphical chart.
I have a task to develop an electronic phone book in which i have
I have been task with the mission of creating a phone solution where clients
I am creating a form which updates two related models, for this example a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.