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.
[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.
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!

Start Spring using IntelliJIDEAUltimate version...

When using MyBatis-Plus or other ORM frameworks for database operations, it is often necessary to construct query conditions based on the attribute name of the entity class. If you manually every time...

Java...

How does the Redis caching solution realize the requirements of product ranking list? During the development process, we often need to deal with the requirements of rankings, such as displaying a...

Conversion of Java Objects and Arrays: In-depth discussion of the risks and correct methods of cast type conversion Many Java beginners will encounter the conversion of an object into an array...

Solutions to convert names to numbers to implement sorting In many application scenarios, users may need to sort in groups, especially in one...

Detailed explanation of the design of SKU and SPU tables on e-commerce platforms This article will discuss the database design issues of SKU and SPU in e-commerce platforms, especially how to deal with user-defined sales...

How to set the SpringBoot project default run configuration list in Idea using IntelliJ...


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SublimeText3 English version
Recommended: Win version, supports code prompts!

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

SublimeText3 Mac version
God-level code editing software (SublimeText3)

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

Atom editor mac version download
The most popular open source editor