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

  • SEARCH
  • Home
  • 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 8857949
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T14:42:49+00:00 2026-06-14T14:42:49+00:00

I’m am trying to determine how many comparisons are made for both the linear

  • 0

I’m am trying to determine how many comparisons are made for both the linear and binary searching techniques. can someone tell me how to print out the number of times a loop happened in each case? for example, to find 5 in the first array the loop happens only one time.

       public static void main(String[] args) {
    // TODO code application logic here
    int[] values  = {5, 8, 6, 2, 1, 7, 9, 3, 0, 4, 20, 50, 11, 22, 32, 120};
    int[] valuesSorted  = {1, 2, 3, 4, 5, 8, 16, 32, 51, 57, 59, 83, 90, 104};
    DisplayArray(values);
    DisplayArray(valuesSorted);

    int index;
    index = IndexOf(1, values);
    System.out.println("1 is at values location " + index);
    index = IndexOf(120, values);

    System.out.println("120 is at values location " + index);

    index = BinaryIndexOf(104, valuesSorted);
    System.out.println("104 is at values Sorted location " + index);

    index = BinaryIndexOf(90, valuesSorted);
    System.out.println("90 is at values Sorted location " + index);       

}


public static int IndexOf(int value, int[] array)
{

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

        if(array[i] == value)
            return i;

    }

    return -1;


}
public static int BinaryIndexOf(int value, int [] array)
{
    int start = 0;
    int end = array.length -1;
    int middle;

    while (end >= start)
    {
        middle = (start + end ) /2;
        if (array[middle]== value)
            return middle;
        if (array[middle]< value)
            start = middle + 1;
        else 
            end = middle - 1;
    }
    return -1;

}

public static void DisplayArray(int[] array)
{
    for (int a : array)
    {
        System.out.print(a + " ");
    }
    System.out.println();
}

}

  • 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-14T14:42:50+00:00Added an answer on June 14, 2026 at 2:42 pm

    For the linear search, you can do something like this:

    public static int IndexOf(int value, int[] array)
    {
        for (int i=0; i < array.length; i++)
        {
    
            if(array[i] == value)
            {            
                System.out.println("Linear search: Number of comparisons = " + (i + 1));
                return i;
            }
        }
    
        return -1;
    }
    

    For the Binary Search, do this:

    public static int BinaryIndexOf(int value, int [] array)
    {
        int start = 0;
        int end = array.length -1;
        int middle;
        int loopCount = 0;
        while (end >= start)
        {
            loopCount++;
            middle = (start + end ) /2;
            if (array[middle]== value)
            {
                System.out.println("Binary search: Number of times looped = " + loopCount); 
                return middle;
            }
            if (array[middle]< value)
                start = middle + 1;
            else 
                end = middle - 1;
        }
        System.out.println("Binary search: Number of times looped = " + loopCount);
        return -1;
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
I am trying to render a haml file in a javascript response like so:
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm trying to select an H1 element which is the second-child in its group

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.