Simple sorting algorithm: Bubbling Method Ordering (Java)

Source: Internet
Author: User

Bubble Sort formula: 22 comparison, 22 swap.

Manipulating objects: Random ordinal group

Mode of operation: the judgment of the condition in the double cycle and the inner layer cycle

Operation Result: Ascending or descending array

Example:

Given an array: int[] arr = new int[]{99,33,21,14,65,11,9,45,22,10};

The array is sorted in ascending order.

Analysis: The larger the number is to sink, the largest number of sink; the smaller the number the more it floats, the smallest number of floating tops. There are 10 numbers in the above array, and there are two ways to achieve the ascending arrangement of these 10 numbers:

The first aspect: one round (or a trip) from left to right to iterate through the array, each round of operation produces a maximum number to the right of the array, but not more than the maximum of the previous round. For an array of arr,First roundAfter operation, 99 is placed inArr[9];Second roundAfter operation, 65 is placed inArr[8];Third roundAfter operation, 45 is placed inArr[7];...... We have to work.Nine rounds。 The array hasTenNumber, to operateNine rounds . Since the operation to the Nineth round, the Nineth maximum number, that nineth maximum number should be placed in the position of arr[1], the tenth maximum number is the minimum number has not been compared with the object so for the 10 number of ascending order is the operation of nine rounds . So for the arrangement of n number, the inference is of course operation N-1 Wheel. the relationship between the number of rounds and the length of the array is derived: total number of wheels = = Array Length-1. Then there is a problem in the middle: in each round of operations, how to sink the number of steps? How many times do you need to compare in each round of operations? What is the relationship between the number of comparisons per round and the number of rounds? take a look at the second note:

Second aspect:

Start {33,21,14,65,11,9,45,22,10} comparison: 0

              First round: 99>33?  true  { , 99 , 21,14,65,11,9,45,22,10}   number of comparisons: 1

                     99>21?  true  { 33 , 21 , 99 , 14,65,11,9,45,22,10}   Comparison number: 2

                     99>14?  true  { , 21 , + , 99 , 65,11,9,45,22,10}   number of comparisons: 3

                     99>65?  true  { , 21 , , + ,

                     99>11?   True  { , 21 , , + , one , Span style= "Color:rgb (255,0,0);" >99 , 9,45,22,10}   number of comparisons: 5

                     99>9?   True  { , 21 , , + , one , 9 , 99 , 45,22,10}   number of comparisons: 6

                     99>45?  true  { , 21 , , + , one , 9 , 45 , 99 , 22,10}   comparison: 7

                     99>22?  true  { , 21 , + , + , , 9 , 45 , 22 , 99 ,   number of comparisons: 8

                     99>10?  true  { , 21 , , + , one , 9 , 45 , , 99 }   Number of comparisons: 9

Second round: 33>21? True {+, +, +,-one, 9,45,22,10,

33>14? True {, +, 65,11,9,45,22,10,

33>65? False {+, +,11,9,45,22,10,

...

...

...


33>65? False {ten,9,$,one, +/-/-(+) Comparison: 8

Third round: Number of comparisons: 7

Fourth round: Comparison number: 6

...

Nineth Round: Comparison number: 1

The following relationship is drawn: the number of rounds = array length-the number of wheels

also based on the first aspect of reasoning: total number of wheels = array Length-1

Noting that the principle of combining array subscripts starts with Arr[0], the design of the double loop is as follows:

           for (int i=0; i<arr.length-1;  i++) {  //external loop control wheel Count                 for (int j=0; j<arr.length-i-1;j++) {   //internal loop control the number of rounds                     if (arr[j]> Arr[j+1]) {      //22 comparison                         int temp =  arr[j];    //1                        arr[j] = arr[j+1];     //2                        arr[j+1] = temp;      //3          1, 2, 3 code implementation 22 Exchange                     }                }           }


Simple sorting algorithm: Bubbling Method Ordering (Java)

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.