Java Implementation selection and sorting

Source: Internet
Author: User

Java Implementation selection and sorting


Public static void main (String [] args) {// array int [] arr = {2, 5, 7, 1, 3, 8, 6, 4} to be sorted }; // control the subscript of each element in the array. The element symbol to be compared for (int I = 0; I <arr. length; I ++) {// all the subscript for (int j = I + 1; j <arr. length; j ++) {// if arr [I] is greater than arr [j] if (arr [I]> arr [j]) {// define the Temporary Variable storage arr [I] and larger int tmp = arr [I] In arr [j]; // exchange the two values of the two comparison positions, arr [I] = arr [j]; arr [j] = tmp ;}}for (int I = 0; I <arr. length; I ++) {// System in ascending order after sorting output. out. println (arr [I]);}


Sorting principle: Compare the elements on the leftmost position with all elements on the right. If the conditions are met, swap the positions of the elements on the two positions to the leftmost value.



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.