Sorting of arrays in Java, selecting Sort method

Source: Internet
Author: User

Require: Array arr[] to sort by selection sort.

Ideas:

1, starting with the first element (the corner mark is 0), and comparing each element behind it, the smaller number is stored in the first element, so that the first element is the smallest value.

2, starting with the second element, and comparing it with each element behind it, the smaller number is stored in the second element, so that the second element is the smallest value except the first one.

3, continue ....

4, the second-to-last element (the corner mark is length-2), and the second-to-last element (length-1), the smaller one in the penultimate element.

At this point, the sorting is complete.

Implementation code:

Select sort
public static void Xuanze (int[] a) {
for (int i=0;i<a.length-1;i++) {//Select the number in the array
for (int j=i+1;j<a.length;j++) {//Select and array all to compare sort
if (A[i]>a[j]) {
int temp=a[i];
A[I]=A[J];
A[j]=temp;
}
}
}
}

Sorting of arrays in Java, selecting Sort method

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.