Wednesday, November 4, 2015

Linked List Binary Search

 This  is a program to perform Binary Search in a Sorted Linked List

Input: Sorted List of Elements:

    public int Search(int element)    {
        return Search(element, 1, sizeOfList());
    }
   
    private int Search(int element, int StartinDex, int EndInDex) {
        LinkedList L = this;
        int middle = (StartinDex+EndInDex)/2;
        if(L.getValue(middle)==element)    {
            return middle;
        }    else if(L.getValue(middle)<element)    {
            return Search(element, middle+1, EndInDex);
        }    else    {
            return Search(element, StartinDex, middle-1);
        }
    }


 For Linked List Code, look at this link

No comments:

Post a Comment

UA-39217154-2