Java Classic algorithm Rollup order lookup (sequential search) _java

Source: Internet
Author: User

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:

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.