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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T09:24:09+00:00 2026-06-12T09:24:09+00:00

I have some code that isn’t making much sense to me. I have an

  • 0

I have some code that isn’t making much sense to me. I have an array of strings and I’m using a binary search to count them in a foreach() loop. The code is exactly the same both times I attempt output, other than sorting. I’m not sure why I’m getting the results I’m getting. I assume it should just count the array values the same way both time. Any help?

Code:

using System;
public class Driver {
    public static void Main(string [] args) {
        String [] s = {"Bob", "Jane", "Will", "Bill", "Liz"};

        Console.WriteLine("Before Sorting:\n----------");
        foreach(string item in s) {
            Console.WriteLine("{0}. {1}", Array.BinarySearch(s, item) + 1, item);
        }
        Console.WriteLine("Will is at position {0}", Array.BinarySearch(s, "Will") + 1);

        Console.WriteLine("\n\nAfter Sorting:\n----------");
        Array.Sort(s);
        foreach(string item in s) {
            Console.WriteLine("{0}. {1}", Array.BinarySearch(s, item) + 1, item);
        }
        Console.WriteLine("Will is at position {0}", Array.BinarySearch(s, "Will") + 1);
    }
}

Output:

Before Sorting:
----------
1. Bob
2. Jane
3. Will
0. Bill
-2. Liz
Will is at position 3

After Sorting:
----------
1. Bill
2. Bob
3. Jane
4. Liz
5. Will
Will is at position 5

I’m sure it’s something entirely stupid, but I can’t figure it out.

  • 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-12T09:24:09+00:00Added an answer on June 12, 2026 at 9:24 am

    Binary search only works on sorted arrays. It is not finding the value:

    If value is not found and value is less than one or more elements in array, a negative number which is the bitwise complement of the index of the first element that is larger than value. If value is not found and value is greater than any of the elements in array, a negative number which is the bitwise complement of (the index of the last element plus 1).

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

Sidebar

Related Questions

I have to work with some code that isn't truly MVC (i.e., it doesn't
I have inherited some code that isn't quite working ... I have jsp which
I have a project that isn't using USING anywhere with their ADO.NET code. I
I have some mouse-tracking code that isn't working as expected in IE that basically
I have some code that reads from an ini file in Javascript, using activex
I am using Eclipse Indigo. I already have some code that is stored in
I have some code that parses an xml file using XDocument . It retrieves
I have some code that will change the background color of a specific label
I have some code that is supposed to return an NSString. Instead it is
I have some code that generates Visio masters for me, and some masters have

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.