﻿<?xml version="1.0" encoding="utf-8" ?>
<CodeSnippets  xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
   <CodeSnippet Format="1.0.0">
        <Header>
            <Title> Searching an element in an arraylist using recursive binary search </Title>
            <Description> Expansion snippet to search an element in the array list using recursive binary search. </Description>
            <Author>Microsoft Corporation</Author>
            <SnippetTypes>          
                <SnippetType> Expansion </SnippetType>
            </SnippetTypes>
        </Header>
        <Snippet>
            <Declarations>
                <Literal>
                    <ID> ArrayList </ID>
                    <Default> arrList </Default>
                    <ToolTip> Array list containing sorted values list </ToolTip>
                </Literal>
                <Literal>
                    <ID> LowIndex </ID>
                    <Default> nLowIndx </Default>
                    <ToolTip> The start index of the array list </ToolTip>
                </Literal>
                <Literal>
                    <ID> HighIndex </ID>
                    <Default> nHighIndx </Default>
                    <ToolTip> The last index of the array list </ToolTip>
                </Literal>
                <Literal>
                    <ID> FindValue </ID>
                    <Default> findValue </Default>
                    <ToolTip> The value to be searched for in the array list </ToolTip>
                </Literal>
            </Declarations>
            <Code Language="vjsharp" Format="CData"><![CDATA[
public boolean binarySearch(java.util.ArrayList $ArrayList$, int $LowIndex$, int $HighIndex$, Object $FindValue$)
{
    int firstIndx = $LowIndex$;
    int lastIndx = $HighIndex$;

    if ($ArrayList$.size() == 0)
        return false;

    if (firstIndx <= lastIndx)
    {
        int midIndx = (firstIndx + lastIndx) / 2;  

        if (((java.lang.Integer)$FindValue$).ToInt32(null) > ((java.lang.Integer)$ArrayList$.get(midIndx)).ToInt32(null))
        {
            firstIndx = midIndx + 1; //set position to upper half of the array
        }
        else if (((java.lang.Integer)$FindValue$).ToInt32(null) < ((java.lang.Integer)$ArrayList$.get(midIndx)).ToInt32(null))
        {
            lastIndx = midIndx - 1; //set position to lower half of the array
        }
        else 
            return true; // Search key found 

    // Search the array list with new start and end index positions.
        return binarySearch($ArrayList$, firstIndx, lastIndx, $FindValue$);
}
return false;    // Search key not found 
}
$selected$ $end$
]]>
</Code>
        </Snippet>
    </CodeSnippet>
</CodeSnippets>
