Java basics-Bubble sorting, java basics bubble

Source: Internet
Author: User

Java basics-Bubble sorting, java basics bubble

 
// Bubble sort, and compare public static void sort1 (int [] aa) {int size = aa from the back of the array. length; int temp; // cyclic array for (int I = 0; I <size; I ++) {// aa [I] compares and exchanges all numbers after the I bit respectively. aa [I] becomes the minimum value for (int j = I + 1; j <size; j ++) {if (aa [I]> aa [j]) {temp = aa [I]; aa [I] = aa [j]; aa [j] = temp ;}}for (int I = 0; I <aa. length; I ++) {System. out. println (aa [I]) ;}}

 

Sort idea: the first layer for loop, each value in the loop array, the second layer for loop, compare the size of aa [I] In the first for loop with other numbers starting from I + 1. If it is smaller than aa [I, switch aa [I] and aa [j]. Through the second-layer for loop, aa [I] will be the minimum value of all numbers starting from I + 1. And so on, you can obtain the minimum values of the remaining digits that are larger than the remaining digits by swapping them. In this way, we can sort the array from small to large.

// Bubble sort. Compare the public static void sort2 (int [] aa) {int size = aa from the forward loop after the array. length; int temp; // cyclic array for (int I = 0; I <size; I ++) {// aa [I] compares and exchanges all numbers after the I bit respectively. aa [I] becomes the minimum value for (int j = size-1; j> I; j --) {if (aa [I]> aa [j]) {temp = aa [I]; aa [I] = aa [j]; aa [j] = temp ;}}for (int I = 0; I <aa. length; I ++) {System. out. println (aa [I]) ;}}

 

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.