集册 Java实例教程 顺序搜索:对数组的穷举搜索

顺序搜索:对数组的穷举搜索

欢马劈雪     最近更新时间:2020-01-02 10:19:05

645
提示:您可在线编辑运行本教程的实例 - 运行实例,去试试!
顺序搜索:对数组的穷举搜索

public class Main {


    static int SequentialSearch(int[] array, int key) {
    /* 
     来自 
    *NowJava.com - 时代Java*/

        int i;

        for (i = 0; i < array.length; i++)

            if (array[i] == key)

                return i;


        return -1;

    }


    public static void main(String[] args) {

        int[] v = {1, 6, 9, 12, 45, 67, 76, 80, 95};


        System.out.println("Seeking for element 6: Position " + SequentialSearch(v, 6));

        System.out.println("Seeking for element 80: Position " + SequentialSearch(v,
展开阅读全文