两个用户定义点之间的二进制搜索,返回输入的两个之间的所有名称

时间:2013-11-28 00:19:42

标签: java

在java中,我必须进行二分查找,要求用户选择列表中存在的两个名称,然后打印这两个名字之间的所有人,从他们输入的第一个名字开始,到第二个名字结束他们输入。我首先调用方法searchTwoPeople。它什么都没有返回....这是我的代码:

         public int binarySearch(String searchItem)
{
    int first = 0;
    int last = nMembers - 1;
    int mid = 0;
    boolean found = false;

    while (first <= last && !found)
    {
        mid = (first + last) / 2;

        if (bookMembers[mid].lastName.compareTo(searchItem) == 0)
            found = true;
        else if (bookMembers[mid].lastName.compareTo(searchItem) > 0)
            last = mid - 1;
        else
            first = mid + 1;
    }

    if (!found)
        mid = -1; //it is an unsuccessful search

    return mid;
}//end binarySearch


public int findFirstPosition(int position)  
{
    int newPosition = position;

    if (position == 0)  
    {
        return position;
    }
    while (true)  
    {
        if (bookMembers[newPosition-1].lastName.compareTo(bookMembers
          [position].lastName) != 0)
        {
            return newPosition;
        }
        else
        {
            --newPosition;

            if (newPosition == 0)
            {
                return newPosition;  // no more people in list
            }

        }
      // end sequential search
    }
}


    public void searchTwoPeople()  
    {
        String lastName = new String();
        String lastName2 = new String();
        int position, nextPosition;

        int tryCount = 0;
        boolean more = true;

            do
            {
                lastName = inputFirstLastName();//this is the      
         method where I have user input a name where to start search and it's passed in here
                lastName2 = inputSecondLastName(); //user puts 
        name where to end search

                position = binarySearch(lastName);


                if (position == -1)
                {

                    System.out.println ("\n You have no 
      contacts with the name  " + lastName);
                    ++ tryCount;
                }
            }
            while (position == -1 && tryCount < 3);// only let them try 
      3 times....

            if (tryCount == 3)
            {
                System.out.println("Only allowed 3 attempts - 
         select a new menu option");
                return;
            }

            nextPosition = findFirstPosition(position);
            while (more)
            {

                    System.out.println(bookMembers
    [nextPosition].toString());

                }

                ++ nextPosition;  // check next person

                if (nextPosition == nMembers) // last person in 
     the list
                {
                    more = false;
                }
                else
                {
                    if (bookMembers
    [nextPosition].lastName.compareTo(lastName2) == 0) 
                    {
                        more = false;
                    }
                }
            }

0 个答案:

没有答案