lundi 27 avril 2015

Else statement not printing. Used binary search

I have a list of numbers and I am trying to find the numbers "58" and "85" in this list. It should print True if it is the number, and False otherwise. I have just been trying to get the 58 part so far. When I run the program, all it prints is "True 58" Is there something wrong with my else statement? I can't seem to find the problem.

import java.util.Arrays;


public class asgn9
{
    public static void main(String[] args)
    {
        int[] data = { 12, 25, 35, 45, 58, 64, 77, 80, 84, 93 };
        int searchedValue = 58;
        int searchedValue2 = 85;

        keyTest(data, searchedValue, searchedValue2);
    }

    public static void keyTest(int[] data, int searchedValue, int searchedValue2)
    {
        boolean found = false; 
        int low = 0;
        int high = data.length - 1;
        int pos = 0;

        while (low <= high && !found)
        {
            pos = (low + high) / 2; // Midpoint of the subsequence
            if (data[pos] == searchedValue) 
            { found = true; }    
            if (data[pos] < searchedValue) 
            { low = pos + 1; }      // Look in first half
            else
            { high = pos - 1; }     // Look in second half
        }
        if (found = false)
            System.out.println("False " + data[pos]);
        else if (found = true)
            System.out.println("True " + data[pos]);
    }//end of keyTest
}

Aucun commentaire:

Enregistrer un commentaire