Java Sorting Algorithm _ 001 Bubble Sorting

Source: Internet
Author: User
Package WZS. sort; import Java. util. arrays; // Bubble Sorting // basic idea: In the number of groups to be sorted, all the numbers within the range not sorted yet, // compare and adjust the adjacent two numbers from top to bottom, so that the larger numbers will sink, and the smaller numbers will go up. // That is, when the numbers of two adjacent parties are compared and their sorting and sorting requirements are the opposite, they are exchanged. // 54321 // start bubbling... // 45321 // 43521 // 43251 // 43215 // the end of the first round... // 34215 // 32415 // 32145 // the end of the second round... // 23145 // 21345 // the end of the third round... // 12345 // the end of the fourth round... public class test_wzs001 {public static void main (string [] ARGs) {int [] intarray = {10, 9, 5, 3, 6, 4, 7, 8, 2, 1}; bubblesort (intarray);}/*** bubble sort * @ Param intarray array to be sorted */static void bubblesort (INT [] intarray) {int temp; // I indicates the number of comparisons for (INT I = 0; I <intarray. length-1; I ++) {// J indicates the number of comparisons for (Int J = 0; j <intarray. length-1-I; j ++) {If (intarray [J]> intarray [J + 1]) {temp = intarray [J]; intarray [J] = intarray [J + 1]; intarray [J + 1] = temp;} system. out. println ("Number" + (I + 1) + "sort:" + arrays. tostring (intarray);} system. out. println ("\ n sorting result:" + arrays. tostring (intarray ));}}

Output result:

1st sorting: [9, 5, 3, 6, 4, 7, 8, 2, 1, 10] 2nd sorting: [5, 3, 6, 4, 7, 8, 2, 1, 9, 10] 3rd sorting: [3, 5, 4, 6, 7, 2, 1, 8, 9, 10] 4th sorting: [3, 4, 5, 6, 2, 1, 7, 8, 9, 10] 5th sorting: [3, 4, 5, 2, 1, 6, 7, 8, 9, 10] 6th sorting: [3, 4, 2, 1, 5, 6, 7, 8, 9, 10] 7th sorting: [3, 2, 1, 4, 5, 6, 7, 8, 9, 10] 8th sorting: [2, 1, 3, 4, 5, 6, 7, 8, 9, 10] 9th sorting: [1, result of sorting 2, 3, 4, 5, 6, 7, 8, 9, 10: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

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.