Algorithm Exercise 4---Bubble sort Java Edition

Source: Internet
Author: User

Bubble sorting basic idea: In order to sort the number of the current is not yet ranked in the range of all the number, the top-down to the adjacent two numbers in turn to compare and adjust, so that the larger number to sink, smaller upward. That is, each time a comparison of two adjacent numbers finds that they are in the opposite order of order, they are interchanged.

Borrow Hip-hop algorithm one of the pictures in this book

If there is a number of N to sort, just n-1 the number of digits, that is, to do n-1 operation. and "Every trip" need to start from the 1th position of the adjacent two number of comparisons, the smaller one number in the back, after the comparison is done to move backward one to continue to compare the size of the following two adjacent numbers, repeat this step, until the last one has not been returned, the number has been returned no longer to compare.

The Java program code is as follows:

 Public class Paixu {    publicstaticvoid  main (string[] args) {

Paixu px = new Paixu ();
Int[] score = {5,3,5,2,8,0,7,10,4};

/** 20170205 Bubble sort*/        int[] Bubblesortresult =px. Bubblesort (score);  for(inti=0;i<score.length;i++) {System.out.print (Bubblesortresult[i]+"    "); }    }    /** Bubble sort * @param score score (0-10 points) * @return return count array * Description: There are n number, two adjacent number comparison, if the previous number is smaller than the number of the subsequent exchange position, assuming that a minimum number is placed in the most Back to a trip, you need to compare 1~n-1, set the number of times I, each trip compared to 1~n-i times*/     Public int[] Bubblesort (int[] score) {        intA=0; intn=score.length;  for(inti=1;i<=n-1;i++){             for(intj=1;j<=n-i;j++){                if(score[j-1]<Score[j]) {a=score[j-1]; Score[j-1]=Score[j]; SCORE[J]=A; }            }        }        returnscore; }}

The result of the code execution is:

    8    7    5    5    4    3    2    0    

Algorithm Exercise 4---Bubble sort Java Edition

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.