Java Array Review---Linear find maximum minimum value---binary lookup

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.