Java Array 4 (2015-8-27)

Source: Internet
Author: User

1, insert the sorting method (with relatively few).

public class insertsort{

public static void Main (String [] args) {

int [] array={2,6,3,9,5,22,19,26,11};

for (int i=1;i<array.length;i++) {

int temp=array[i];

int j=i;//Save the subscript.

while (J>0&&temp<array[j-1]) {

The above number overrides the number below it

ARRAY[J]=ARRAY[J-1];

j--;

}

array[j]=temp;//Inserting data

}

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

System.out.print (array[i]+ "");

}

}}

2, select the sorting method (from the array to find the largest or smallest data for one by one comparison)

public class selectsort{

public static void Main (String [] args) {

int [] array={1,5,4,6,8,90,45,22,87,212,447};

int min=0;

for (int i=0;i<array.length-1;i++) {

min=i;//The first one is the smallest first.

for (int j=i+1;j<array.length;j++) {

if (Array[min]>array[j]) {

Min=j;

}

}

If the minimum number of I is not located on I, then the Exchange

if (i!=min) {

int temp=array[i];

Array[i]=array[min];

Array[min]=temp;

}

}

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

System.out.print (array[i]+ "");

}

}}

Java Array 4 (2015-8-27)

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.