a) Principle : Sequential search is in order from beginning to end to look down, find the data, then advance the search, can not find the search continues until the last data.
b) Illustration : Original data: int[]a={4,6,2,8,1,9,0,3}; To find the number: 8
Finds data 8 in the array and returns the position.
Code Demo :
Import Java.util.Scanner;
* * * Order
Lookup
/public class Sequelsearch {public
static void Main (string[] arg) {
int[] a={ 4,6,2,8,1,9,0,3};
Scanner input=new Scanner (system.in);
System.out.println ("Please enter the number you want to find:");
The statement that holds the input of the console
int num=input.nextint ();
Call the Searc () method to save the return value in result
int result=search (A, num);
if (result==-1) {
System.out.println ("The number you entered does not exist in the array.") ");
}
else
System.out.println ("The number you enter exists, the position in the array is the first:" + (result+1) + "");
}
Sequential sort algorithm public
static int search (int[] A, int num) {for
(int i = 0; i < a.length; i++) {
if (a[i] = num {///If the data exists return
i;//returns the subscript where the data is located, that is, the position
}
}
return-1;//does not exist, returns-1
}
}
Run Screenshots: