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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T08:05:13+00:00 2026-06-03T08:05:13+00:00

Here is the class on gist https://gist.github.com/2605302 I have tested it multiple times with

  • 0

Here is the class on gist
https://gist.github.com/2605302

I have tested it multiple times with different files and even when there is less comparisons done for binary search the time taken is ALWAYS more. What’s going wrong?

public static int linerSearch ( String array [], String word, long resultsArray [])
{
    int comparisons = 0;
    int pos = -1;
    //i have started the timer where the search actualy starts
    long start = System.nanoTime ();
    for (int i = 0; i < array.length; i++){
        comparisons = comparisons + 1;
        if (array [i].equals (word)){
            pos = i;
            break;
        }
    }
    long stop = System.nanoTime ();
    long total = stop - start;
    resultsArray [0] = total;
    resultsArray [1] = (long) (long) array.length;
    resultsArray [2]= (long) (long) comparisons;
    return pos;
}

Here is the next binarySearch class

public  static int binarySearch (String [] array, String word, resultsArray []) {
    int start = 0;
    int end = array.length - 1;;
    int midPt;
    int pos = -1;
    int comparisons2 = 0;
    long start2 = System.nanoTime ();
    Arrays.sort (array);
    while (start <= end) {
        midPt = (start + end) / 2;
        comparisons2 = comparisons2 + 1;
        if (array [midPt].equalsIgnoreCase (word)) {
            pos = midPt;
            break;
        }
        else if (array [midPt].compareToIgnoreCase (word) < 0) {
            start = midPt + 1;
            comparisons2 = comparisons2 + 1;
            //camparisons2 addition was added inside this elseif and other elseif as a work around for not breaking the elseif statement tree, if it has made it two the last elseif then two camparisons after the first one will have been done
        } else if (array [midPt].compareToIgnoreCase (word) > 0)  {
            comparisons2 = comparisons2 + 2;
            end = midPt - 1;
        }
    }
    long stop2 = System.nanoTime ();
    long total2 = stop2 - start2;
    resultsArray [0] = total2;
    resultsArray [1] = (long) (long) array.length;
    resultsArray [2]= (long) (long) comparisons2;
    return pos;
}

edit: I should also add that i tried it on an already previously sorted array without that line of code and it was still a longer time when it shouldn’t have been

  • 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-03T08:05:14+00:00Added an answer on June 3, 2026 at 8:05 am

    Okay, I’ve got this worked out for you once and for all. First, here’s the binary search method as I used it:

    public static int binarySearch(String[] array, String word, long resultsArray[]) {
        int start = 0;
        int end = array.length - 1;
        int midPt;
        int pos = -1;
        int comparisons2 = 0;
    
        //Arrays.sort(array);
    
        long start2 = System.nanoTime();
        while (start <= end) {
            midPt = (start + end) / 2;
            int comparisonResult = array[midPt].compareToIgnoreCase(word);
            comparisons2++;
            if (comparisonResult == 0) {
                pos = midPt;
                break;
            } else if (comparisonResult < 0) {
                start = midPt + 1;
            } else { // comparisonResult > 0
                end = midPt - 1;
            }
        }
        long stop2 = System.nanoTime();
        long total2 = stop2 - start2;
    
        resultsArray[0] = total2;
        resultsArray[1] = (long) array.length;
        resultsArray[2] = (long) comparisons2;
        return pos;
    }
    

    You’ll notice that I reduced the number of comparisons by saving the comparison result and using that.

    Next, I downloaded this list of 235882 words. It is already sorted ignoring the case. Then, I built a test method that loads the contents of that file into an array and then uses both of those searching methods to find every word of that list. It then averages the times and numbers of comparisons for each method separately.

    I found out that you must be careful in choosing which comparison methods to use: if you Arrays.sort(...) a list and you use compareToIgnoreCase in binary search, it fails! By failing I mean that it cannot find the word from the given list even though the word actually exists there. That is because Arrays.sort(...) is a case-sensitive sorter for Strings. If you use that, you must use the compareTo(...) method with it.

    So, we have two cases

    1. a case-insensitively sorted list and the use of compareToIgnoreCase
    2. a case-sensitively sorted list and the use of compareTo

    In addition to these options in the binary search, you also have options in the linear search: whether to use equals or equalsIgnoreCase. I ran my test for all of these cases and compared them. Average results:

    • Linear search with equals: time: 725536 ns; comparisons: 117941; time / comparison: 6.15 ns
    • Linear search with equalsIgnoreCase: time: 1064334 ns; comparisons: 117940; time / comparison: 9.02 ns
    • Binary search with compareToIgnoreCase: time: 1619 ns; comparisons: 16; time / comparison: 101.19 ns
    • Binary search with compareTo: time: 763 ns; comparisons: 16; time / comparison: 47.69 ns

    So, now we can clearly see your problem: the compareToIgnoreCase method takes some 16 times as much time as the equals method! Because, on average, it takes the binary search 16 comparisons to find the given word, you can perform 124 linear comparisons in that time. So if you test with word lists shorter than that, the linear search is, indeed, always faster than the binary search due to the different methods they are using.

    I actually also counted the number of words that the linear search was able to find faster than the binary search: 164 when using the compareTo method and 614 when using the compareToIgnoreCase method. Of the the list of 235882 words, that’s about 0.3 percent. So all in all I think it’s still safe to say that the binary search is faster than the linear search. 🙂

    One last point before you ask: I removed the sorting code from the binarySearch method, because that’s actually an entirely different thing. Since you are comparing two searching algorithms, it’s not fair for the other if you add the cost of a sorting algorithm to its figures. I posted the following as a comment in another answer already, but I’ll copy it here for completeness:

    Binary search has the added overhead cost of sorting. So if you only need to find one element from an array, linear search is always faster, because sorting takes at least O(n log n) time and then a binary search takes O(log n) time, dominated by the O(n log n) operation. A linear search performs in O(n) time, which is better than O(n log n). But once you have the array sorted, O(log n) is way better than O(n).

    If you insist on having the sorting command in the binarySearch method, you should be aware that with my setup sorting that long list of words from an initially random order takes more than 140000000 ns, or 0.14 seconds, on average. In that time you could perform some 23000000 comparisons using the equals method, so you really should not use binary search if a) your array is in a random order and b) if you only ever need to find just one or a couple of elements from there.

    And one more thing. In this example, where you are searching for words in a String array, the cost of accessing an item in the array is negligible because the array is saved in the fast main memory of the computer. But if you had, say, a huge bunch of ordered files and you tried to find something from them, then the cost of accessing a single file would make the cost of every other calculation negligible instead. So binary search would totally rock in that scenario (too).

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

Sidebar

Related Questions

GIST is here: https://gist.github.com/1553371 These are the models I have: class Character < ActiveRecord::Base
Here is the stack trace which appears on resque-web interface: http://gist.github.com/396893 There you can
UPDATE : The full site code is here - https://github.com/eWizardII/PeerInstruction I have the following
I'm using Caliburn.Micro bootstrapper here: https://gist.github.com/1127914 Everything works if I keep all views and
I have a few classes as shown here public class TrueFalseQuestion implements Question{ static{
This problem has been driving me mad. Here's the general gist: I have two
Here's the gist of what I'm going to do. Heres' my hero class: namespace
Here is the basic gist of my problem: My main Window class instantiates Class
I have a user input form here: http://www.7bks.com/create (Google login required) When you first
I'm reading somebody else's code. Here's the gist of it. A class compresses and

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.