-1

I am trying to put in practice an algorithm for binary searching and compare it with linear searching:

  • linear search:
public static int linearSearch(ArrayList<Book> books, int searchedId) {
    for (int i = 0; i < books.size(); i++) {
        int currId = books.get(i).getId();
        if (currId == searchedId) {
            return i;
        }
    }
    return -1;
}
  • and binary search
public static int binarySearch(ArrayList<Book> books, long searchedId) {
    if (!isSorted(books)) {
        System.err.println("ArrayList must be sorted!");
        return -1;
    }

    int ix_st = 0;
    int ix_en = books.size() - 1;
    int middle = 0;
    while (ix_st <= ix_en) {
        middle = (ix_en + ix_st)/2;
        int currId = books.get(middle).getId();
        if (currId == searchedId) {
            return middle;
        }
        if (searchedId > currId) {
            ix_st = middle + 1;
        }
        if (searchedId < currId) {
            ix_en = middle - 1;
        }
    }
    return -1;
}

the whole code

but not only is the binary searching algorithm similar but sometimes even slower than a trivial linear searching. I can't understan why. Binary search is supposed to run the code only for ~log2(100,000) = 17 iterations or ~log2(1,000,000) = 20 iterations, whilst the linear searching is full looping thorugh for 1,000,000 iterations in the worst scenario. Obviously, the lists are sorted. I've got these timings:

How many books to create?
[Me] 100000
Id of the book to search for?
[Me] 987665

Searching with linear search:
The search took 9 milliseconds.
Book not found


Seaching with binary search:
The search took 13 milliseconds.
Book not found

Process finished with exit code 0

the whole code.

I am not sure if the compiler is optimising something in background. Then it would make sense. Any idea? Thanks.

PS. The code is from the MOOC.fi website as I am taking an online course from the University of Helsinki called Java Programming I, particularly from this section.

3
  • 2
    Also, if the 1 book you're looking for happens to be among the first, the linear search will outperform. To get a better sense of the time, you would have to search for thousands of books, and average the times. Commented Jun 3, 2021 at 19:05
  • 1
    FYI, This binary search code has the classic binary search overflow bug Commented Jun 3, 2021 at 19:09
  • Using a debugger to step through your code would have shown you immediately what’s going on. Commented Jun 3, 2021 at 19:39

1 Answer 1

12

Your actual binary search implementation includes a test isSorted() which scans through the whole list and is therefore slower than the linear search. Re-test without this and you will see different results.

2
  • 2
    Something like isSorted should be a property that gets invalidated on any modification of the array and checked only when invalid. Commented Jun 3, 2021 at 19:34
  • 1
    @gnasher729 or you drop it entirely and accept that binarySearch only has defined behaviour for sorted lists Commented Jun 4, 2021 at 8:19

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.