循序搜寻法

public static void main(String[] args)
{ int[] pArray = {1, 2, 4, 8, 9, 78, 89, 54, 63, 47, 28, 454, 3648, 4123, 999, 546, 231, 213, 985};
        int targetIndex = linerSearch(pArray, 213);
        int i = 0;
        int lengthArray = pArray.length;
        System.out.println(lengthArray);
        while (i <= lengthArray)
        {
            if (pArray[i] == 213)
            {
                System.out.println("找到该值了,它的坐标:" + i);
                break;
            }
            i++;
        }

}

//卫兵查找

 public static int linerSearch(int[] pArray, int target)
    {
        int[] temp = new int[pArray.length + 1];
        for (int i = 1; i < temp.length; i++)
        {
            temp[i] = pArray[i - 1];
        }
        temp[0] = target;
        int index = temp[0];
        int j = 1;
        while (temp[j] != index)
        {
            j++;
        }
        return j - 1;
    }

你可能感兴趣的:(J#)