Search code examples
pythonalgorithmsortingsearchbinary-search

Difference between binary search and binary sort


  1. What is the difference between binary search and binary sort? (Or is there no such thing as binary sort.)

  2. Can I perform binary search on an unsorted list? (I am a little in clear on this can someone please explain it to me.)

  3. If I perform binary sort on an unsorted list and then binary search for an element in that (now) sorted list what will be the time complexity of the whole process?


Solution

    1. Can I perform binary search on an unsorted list?(I am a little in clear on this can someone please explain it to me)

    No you cannot,

    The core idea of Binary search is to reduce the search space by value comparison.

    for instance look at [1,2,3,4,5,6,7] and the element to be search for is 5. in binary search we look at the middle element in this case 4, since the target element is greater than 4 and we know the array is sorted we can look only at the right half [5,6,7]

    Binary search can also be implemented where the function is MONOTONOUS , example [T , T , T , T , F , F , F] You can apply binary search to find the first False in the array with this template

    start=0, end=size
    while(start<end)
        if(condition)
           end=mid
        else
           start=mid+1 
    return start
    

    returning start will give you the first element which is false.