Algorithm--Simple bubble sort

Source: Internet
Author: User

The algorithm needs to master the simple bubble sort words not much to say we look at the following:

/** * Put a chaotic array int arr[] = {2,2323,23,456,1,342}; * According to the order that we often say the sort of counterfeit * or as we have said before when we encounter problems, we dismembered the problem.  * 1, first we want to put a chaotic array into order, we should first find out the largest (or the smallest) * 2, and then we sequentially follow the method of the loop can be arranged out of the order of the want, the idea has been, to achieve it! * @author Administrator * */public class bubblesorted {public static int arr[] = {2,2323,23,456,1,342};p ublic static void Main (string[] args) {test (arr);} public static void Test (int arr[]) {//First find the biggest idea in terms of what we say from arr[]: Think about it now you have a glass of water. Coffee how to swap the liquid in two cups? You're smart to take another cup! Come on, then. Because considering the length of the array may often be used to extract him into variables. int len = arr.length;//in order to make this sort method more efficient we can make a decision to join the loop again when there is no change in the row position we stop looping the Boolean boo = true;for (int j =0;j<len-1;j++) {Bo o = true;//assumes ordered System.out.println ("the" + (j+1) + "trip"); for (int i =0;i<len-1-j;i++) {System.out.println ("+" + (i+1) + "th"); if (arr[i]<arr[i+1]) {int temp = arr[i];//pour water into the temp cup arr[i] = arr[i+1];//Pour the coffee into the cup that just started to water arr[i+1] = temp;// Finally pour the water from the Temp Cup into the cup in which the coffe is just beginning. Boo = false;//If there is a change, place Boo as false}system.out.println (arr);} if (boo) {break;}}}}
Do the right thing when you don't know what to do.

Algorithm--Simple bubble sort

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.