Home  >  Article  >  Java  >  How to implement bubble sorting in java

How to implement bubble sorting in java

coldplay.xixi
coldplay.xixiOriginal
2020-08-19 15:51:493661browse

How to implement java bubble sorting: first compare the 1st and 2nd numbers, put the decimals in front, and put the large numbers in the back; then compare the 2nd number with the 3rd number, put the decimals In the front, the large number is placed in the rear, and so on until the last two numbers are compared, the decimal is placed in front, and the large number is placed in the rear; finally, the first step is repeated until all sorting is completed.

How to implement bubble sorting in java

[Related learning recommendations: java basic tutorial]

How to implement java bubble sorting:

Principle: Compare two adjacent elements and swap the element with the larger value to the right end.

Idea: Compare two adjacent numbers in turn, put the decimal in front and the large number in the back. That is, in the first pass: first compare the first and second numbers, put the decimal first and the large number last. Then compare the second number and the third number, put the decimal in front and the large number in the back, and continue like this until comparing the last two numbers, put the decimal in front and the large number in the back. Repeat the first step until all sorting is completed.

Example: To sort the array: int[] arr={6,3,8,2,9,1};

First sorting:

 First sorting: Compare 6 and 3, 6 is greater than 3, swap positions: 3 6 8 2 9 1

The second sorting: Compare 6 and 8, 6 is less than 8, do not swap positions: 3 6 8 2 9 1

The third sorting: Compare 8 and 2, 8 is greater than 2, exchange positions: 3 6 2 8 9 1

The fourth sorting: Compare 8 and 9, 8 is less than 9 , do not exchange positions: 3 6 2 8 9 1

The fifth sorting: 9 and 1 comparison: 9 is greater than 1, exchange positions: 3 6 2 8 1 9

The first trip total Five comparisons were made, and the ranking results were: 3 6 2 8 1 9

-------------------------------- ----------------------------------------

Second sorting :

The first sorting: compare 3 and 6, 3 is less than 6, do not exchange positions: 3 6 2 8 1 9

The second sorting: compare 6 and 2, 6 is greater than 2 , exchange positions: 3 2 6 8 1 9

The third sorting: compare 6 and 8, 6 is greater than 8, do not exchange positions: 3 2 6 8 1 9

The fourth sorting : 8 and 1 are compared, 8 is greater than 1, exchange positions: 3 2 6 1 8 9

The second pass has a total of 4 comparisons, the sorting result: 3 2 6 1 8 9

-------------------------------------------------- -------------------

The third sorting:

The first sorting: comparing 3 and 2, 3 is greater than 2, Exchange positions: 2 3 6 1 8 9

The second sorting: Compare 3 and 6, 3 is less than 6, do not exchange positions: 2 3 6 1 8 9

The third sorting: Compare 6 and 1, 6 is greater than 1, exchange positions: 2 3 1 6 8 9

The second pass performed a total of 3 comparisons, the sorting result: 2 3 1 6 8 9

- -------------------------------------------------- ------------------

The fourth sorting:

The first sorting: Compare 2 and 3, 2 is less than 3, not Exchange position: 2 3 1 6 8 9

The second sorting: 3 and 1 are compared, 3 is greater than 1, exchange position: 2 1 3 6 8 9

The second trip was carried out in total 2 comparisons, sorting results: 2 1 3 6 8 9

---------------------------------- -------------------------------------

Fifth sorting:

The first sorting: 2 and 1 are compared, 2 is greater than 1, exchange positions: 1 2 3 6 8 9

The second time a total of 1 comparison was performed, the sorting result: 1 2 3 6 8 9

----------------------------------------- ----------------------------

Final result: 1 2 3 6 8 9

- -------------------------------------------------- ------------------

Advantages of bubble sorting: Every time you perform a sort, you will compare one less time, because every time you perform a sort, you will find out a larger value. As in the above example: after the first comparison, the last number must be the largest number. During the second sorting, you only need to compare other numbers except the last number, and you can also find the largest number. The numbers are ranked behind the numbers participating in the second comparison. In the third comparison, only the other numbers except the last two numbers need to be compared, and so on... In other words, without a comparison, every time One less comparison per trip reduces the amount of algorithm to a certain extent.

How to implement bubble sorting in java

Related recommendations: Programming Video Course

The above is the detailed content of How to implement bubble sorting in java. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn