Cyclic search (using flag)

xiaoxiao2021-04-06  288

Beginners see the next steps, most of them use the following ways to search:

While (i

IF (Number [i] == k) {

Printf ("find the specified value");

Break;

}

i ;

}

This method is basically not wrong, but it can be improved. You can use the setting guard. If you want to judge, the guard usually set in the series, the last or the forefront, suppose set in the column before the column (the location of index 0), We look forward from the rearward directions. If you find the specified information, the index value is not 0, indicating that it is found before the number of visits, in the process of writing, as long as you use a While recoil.

Public class linearsearch {public static int search (int Len = number, int des) {int LEN = number.length; int = number = new int [l 1]; / * for (int i = 1; i

INT K = TMP [0]; INT i = Number.Length; While (TMP [i]! = K) I -; RETURN I - 1;

Public static void main (string [] args) {int [] Number = {1, 4, 2, 6, 7, 3, 9, 8}; int found = linearsearch.search (Number, 8); if (Find! = 0) System.out.println ("Find value to index" find); else system.out.println ("No value");}}

转载请注明原文地址:https://www.9cbs.com/read-132265.html

New Post(0)