import Java.util.Scanner;
Public class Arraydemo {
Public Static void Main (String []args) {
//-------------------------------------------------------
Linear Lookup
int [] Num ={10,20,30,40,50};
Scanner input1 = new Scanner (System. in);
System. out. println ("Please enter the number to find (10,20,30,40,50):");
int INP = Input1.nextint ();
Input1.close ();
for (int i = 0;i<num.length;i++) {
if (INP = = Num[i]) {
System. out. println ("The number of checks below is labeled as:" +i);
}Else {
if (i==num.length-1) {
System. out. println ("-1");
}
}
}
//-------------------------------------------------------
Find array maximum, minimum value
int [] maxmin = new int[] {20,60,80,70,30,10,50};
System. out. println ("Array is (20,60,80,70,30,10,50), Max and Min:");
Method One (bubbling sort):
int temp=0;
for (int i = 0;i<maxmin.length-1;i++) {
if (Maxmin[i]>maxmin[i+1]) {
temp = maxmin[i+1];
MAXMIN[I+1] = Maxmin[i];
Maxmin[i] = temp;
}
}
System. out. println ("Maximum value:" +maxmin[6]);
for (int i = 0;i<maxmin.length-1;i++) {
if (Maxmin[i]<maxmin[i+1]) {
temp = maxmin[i+1];
MAXMIN[I+1] = Maxmin[i];
Maxmin[i] = temp;
}
}
Method Two:
int max = maxmin[0];
for (int i = 1;i<maxmin.length-1;i++) {
if (Maxmin[i]>max) {
max = Maxmin[i];
}
}
System. out. println ("Maximum value:" +max);
int min = maxmin[0];
for (int i = 1;i<maxmin.length-1;i++) {
if (maxmin[i]<min) {
min = Maxmin[i];
}
}
System. out. println ("Minimum Value:" +min);
//--------------------------------------------------------
Two-point Search
int [] BinarySearch = new int[] {10,20,30,40,70,80,90,100};
Scanner input2 = new Scanner (System. in);
System. out. println ("Please enter the number to find (10,20,30,40,70,80,90,100):");
Input2.close ();
int start;
int end;
int Middle;
int index = -1;//Save to find the book at the next label
Start = 0;//Save starting subscript
End = binarysearch.length-1;//Save End subscript
while (Start <=end) {
Middle = (start+end)/2;//find the value corresponding to the intermediate element
if (num2 = = Binarysearch[middle]) {
index = middle+1;
break;
}
if (Num2 > Binarysearch[middle]) {//If the number is greater than the middle
Start = middle+1;//start subscript to the next number of intermediate numbers
}
if (Num2 < Binarysearch[middle]) {
end = middle-1;//End subscript to the previous number of the middle number
}
}
if (Index = =-1) {
System. out. println ("not Found");
}
Else {
System. out. println ("checked, Position in section" +index+ "bit");
}
}
}
Java Array Review---Linear find maximum minimum value---binary lookup