Array algorithm:

Source: Internet
Author: User

array algorithm :1), copy algorithm: for example: (int[] a={1,2,3,4,5,};

int[] b=new int[a.length];//b length is determined by the length of a;

For (int i=0;i<a.length;i++) {//write a.length or write B.length Can. The same length;

a[i]=b[i]; Copy one to b;

}

For (int i=0;i<a.length;i++) {output b: (System.out.println (b[i]+ ""));

}//output b[i] value:

2), Find algorithm (search); for example: scnner in=new Scanner (system.in);

int[] array={1,2,3,4,5,};

System.out.println ("please Enter the number you want to insert:");

int Index=in.nextint ();

Arrray[a.length-1]=index;//a.length-1: is the maximum subscript for array arrays;

For (int i=a.length-1;; i>0;i--) {

If (array[i]<array[i-1]) {

int T=array[i];//int T: with intermediate variables;

array[i]=array[i-1];

array[i-1]=t;

}else{

Break

}

}

For (int I=0;i<array.length;i++) {

System.out.println (array[i]+ "");//output array[i];

}

3), Bubble sort: for example:

For (int J=0;j<a.length-1;j++) {

If (a[j]>a[j+1]) {

int t=a[j];

a[i]=a[j+1];

a[j+1]=t;

}

}

Array algorithm:

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.