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 9265487
In Process

The Archive Base Latest Questions

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

public int BinarySearch(int x) { //if (Attendees.Length == 0) // return -1; int mid

  • 0
public int BinarySearch(int x)
{
    //if (Attendees.Length == 0)
    //    return -1;
    int mid =  (Count/ 2) -1;
    Student cur = new Student(x);

    while (cur.CompareTo(Attendees[mid]) != 0)
    {
        int sCount = Count;
        if (cur.CompareTo(Attendees[mid]) < 0)
        {
            int NCount = sCount / 2;
            mid = NCount / 2 - 1;    
        }
        if (cur.CompareTo(Attendees[mid]) > 0)
        {
            int Start = mid +1;
            mid = (Start + sCount) / 2;
        }
        else
            break;

        cur = Attendees[mid];                
    }
    if (cur.CompareTo(Attendees[x]) == 0)
        return mid;
    else 
        return -1;
}

Can anyone help me find out why my binary search isn’t working? I’m very new to programming so any help would be much appreciated. Thank you.

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

    I think you didn’t really grasp what binary search is about. In your code you are searching at which position element x is in an array – guess what? It’s at position x!

    What binary search is about is to find out the index of an element. Soooo you need to search for a given Student:

    public int BinarySearch(Student student)
    {
        // Search the entire range
        int min = 0;
        int max = Attendees.Length;
        int mid = 0;
    
        do
        {
            // Find the pivot element
            mid = min + ((max - min) / 2);
    
            // Compare
            int comparison = student.CompareTo(Attendees[mid]);
            if (comparison < 0)
            {
                max = mid;
            }
            if (comparison > 0)
            {
                min = mid;
            }
        }
        while (min < max && comparison != 0);
    
        if (comparison != 0)
            return -1;
        return mid;
    }
    

    This code may not work 100% as I haven’t tried it and wrote it off my head, but it will point you in the right direction. Now, use the debugger on this code and single step through it to see whether it works as expected.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This compiles: class Ex1 { public int show() { try { int a=10/10; return
Let's start with this overload of List BinarySearch: public int BinarySearch(T item, IComparer<T> comparer);
For example check method in java.util.Collections public static <T> int binarySearch(List<? extends T> list,
According to the documentation: public static <T> int binarySearch(T[] a, T key, Comparator<? super
[DataMember] public int? NumberOfPages; //////////// Is this supported???? [DataMember] public bool? Color; //////////// Is
public class JsonCategoriesDisplay { public JsonCategoriesDisplay() { } public int CategoryID { set; get;
Assuming public class MyClass { public int ID {get; set; } public string Name
sealed public class HMethod { public int Calc(string Method, int X1, int X2, int
Keynotfound exception public int getLastUniqueID() { int lastID = 0; IsolatedStorageSettings uc = IsolatedStorageSettings.ApplicationSettings;
class test { public: int data; test(int var = 0):data(var){cout<<constructor<<endl;} ~test(){ cout<<destructor<<endl; } test(const

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.