search
HomeJavajavaTutorialHow to implement bubble sorting in java

How to implement bubble sorting in java

Aug 19, 2020 pm 03:51 PM
javaBubble Sort

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
How does IntelliJ IDEA identify the port number of a Spring Boot project without outputting a log?How does IntelliJ IDEA identify the port number of a Spring Boot project without outputting a log?Apr 19, 2025 pm 11:45 PM

Start Spring using IntelliJIDEAUltimate version...

How to elegantly obtain entity class variable names to build database query conditions?How to elegantly obtain entity class variable names to build database query conditions?Apr 19, 2025 pm 11:42 PM

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

How to use the Redis cache solution to efficiently realize the requirements of product ranking list?How to use the Redis cache solution to efficiently realize the requirements of product ranking list?Apr 19, 2025 pm 11:36 PM

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

How to safely convert Java objects to arrays?How to safely convert Java objects to arrays?Apr 19, 2025 pm 11:33 PM

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

How do I convert names to numbers to implement sorting and maintain consistency in groups?How do I convert names to numbers to implement sorting and maintain consistency in groups?Apr 19, 2025 pm 11:30 PM

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

E-commerce platform SKU and SPU database design: How to take into account both user-defined attributes and attributeless products?E-commerce platform SKU and SPU database design: How to take into account both user-defined attributes and attributeless products?Apr 19, 2025 pm 11:27 PM

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 default run configuration list of SpringBoot projects in Idea for team members to share?How to set the default run configuration list of SpringBoot projects in Idea for team members to share?Apr 19, 2025 pm 11:24 PM

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

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Tools

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

mPDF

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

SublimeText3 Mac version

God-level code editing software (SublimeText3)

MinGW - Minimalist GNU for Windows

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

Atom editor mac version download

The most popular open source editor