What would be time Complexity of binary search algorithm?

Jhon Smith

I am studying about binary search tree and I have written a method to perform binary search in list of array java. However, I am trying to work out the time complexity of this algorithm. but i am new this topic and I am not sure how to work out the time complexity using BIG O notation.

public <T extends Comparable<T int binarySearch(T[] array,T target) {
    int first = 0, last = array.length-1; // initially search in the whole array
    while (first <= last) {// still at least one element in search space
                           // calculate median index, halfway between first and last indices. . .
        int median = (first+last)/2;
        // . . . and get the value there
        int comparison = array[median].compareTo[target];
        if (comparison == 0) {// target value found
            return median;
        } else if (comparison > 0) {// median value is larger than target
            last = median-1; // search further in lower half of search space
        } else {// median value is smaller than target
            first = median+1; // search further in upper half of search space
        }
    }
    return -1; // ran out of search 
Nikolay Bonev

The complexity is O(logn).

Wikipedia

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

What would be the time complexity of the pascal triangle algorithm

From Java

What is the time-complexity in a binary search?

From Dev

What's time complexity of algorithm for finding all Unique Binary Search Trees?

From Dev

Time Complexity of Binary Search?

From Dev

Why does assignment in a binary search algorithm not add to the time complexity?

From Java

What will be the time complexity of this algorithm

From Dev

What is the time complexity for this algorithm?

From Dev

What is the time complexity for this Algorithm?

From Dev

What is the time complexity for this Algorithm?

From Dev

What is the Time complexity of this Algorithm using AVL tree and Binary tree

From Dev

What is the time complexity of this generation algorithm?

From Dev

What is complexity of simplex algorithm for binary integer programming?

From Dev

Running time complexity for binary search tree

From Dev

Time complexity of deletion in binary search tree

From Dev

GATE 2008: Time Complexity of Binary Search Tree

From Dev

Time complexity of deletion in binary search tree

From Dev

What's tight time complexity of this algorithm for Binary Tree Zigzag Level Order Traversal?

From Dev

What's tight time complexity of this algorithm for Binary Tree Zigzag Level Order Traversal?

From Dev

Execution time for successful search Binary Search algorithm

From Dev

What should be the time complexity of my sorting algorithm?

From Dev

What's time complexity of this algorithm for solving Sudoku?

From Dev

What's the time complexity of this algorithm for Palindrome Partitioning?

From Dev

What's time complexity of this algorithm for Wildcard Matching?

From Dev

What is the time complexity of a modified quick sort algorithm?

From Dev

What is the time complexity of the Hill Climbing Algorithm?

From Dev

What is the time complexity of this algorithm (Big-O)?

From Dev

What is the time complexity of a modified quick sort algorithm?

From Dev

What's the time complexity of this algorithm (pseudo code)?

From Dev

What is the Time Complexity, Space complexity and Algorithm for strstr() function in C++?

Related Related

  1. 1

    What would be the time complexity of the pascal triangle algorithm

  2. 2

    What is the time-complexity in a binary search?

  3. 3

    What's time complexity of algorithm for finding all Unique Binary Search Trees?

  4. 4

    Time Complexity of Binary Search?

  5. 5

    Why does assignment in a binary search algorithm not add to the time complexity?

  6. 6

    What will be the time complexity of this algorithm

  7. 7

    What is the time complexity for this algorithm?

  8. 8

    What is the time complexity for this Algorithm?

  9. 9

    What is the time complexity for this Algorithm?

  10. 10

    What is the Time complexity of this Algorithm using AVL tree and Binary tree

  11. 11

    What is the time complexity of this generation algorithm?

  12. 12

    What is complexity of simplex algorithm for binary integer programming?

  13. 13

    Running time complexity for binary search tree

  14. 14

    Time complexity of deletion in binary search tree

  15. 15

    GATE 2008: Time Complexity of Binary Search Tree

  16. 16

    Time complexity of deletion in binary search tree

  17. 17

    What's tight time complexity of this algorithm for Binary Tree Zigzag Level Order Traversal?

  18. 18

    What's tight time complexity of this algorithm for Binary Tree Zigzag Level Order Traversal?

  19. 19

    Execution time for successful search Binary Search algorithm

  20. 20

    What should be the time complexity of my sorting algorithm?

  21. 21

    What's time complexity of this algorithm for solving Sudoku?

  22. 22

    What's the time complexity of this algorithm for Palindrome Partitioning?

  23. 23

    What's time complexity of this algorithm for Wildcard Matching?

  24. 24

    What is the time complexity of a modified quick sort algorithm?

  25. 25

    What is the time complexity of the Hill Climbing Algorithm?

  26. 26

    What is the time complexity of this algorithm (Big-O)?

  27. 27

    What is the time complexity of a modified quick sort algorithm?

  28. 28

    What's the time complexity of this algorithm (pseudo code)?

  29. 29

    What is the Time Complexity, Space complexity and Algorithm for strstr() function in C++?

HotTag

Archive