search
HomeJavaJavagetting StartedImplementation of bubble sorting java code

Implementation of bubble sorting java code

May 12, 2020 am 09:33 AM
javaBubble Sort

Implementation of bubble sorting java code

Implementation of java code for bubble sort

Bubble sort is a relatively classic sorting algorithm. The code is as follows:

for(int i=1;i<arr.length;i++){for(int j=1;j<arr.length-i;j++){//交换位置}

Extension information:

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.

After the first comparison is completed, the last number must be the largest number in the array, so the last number does not participate in the comparison during the second comparison;

The second comparison After completion, the penultimate number must also be the second largest number in the array, so the last two numbers will not participate in the comparison in the third comparison;

and so on, the number of comparisons in each comparison is -1 ;

......

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

for(int i=1;i<arr.length;i++){for(int j=1;j<arr.length-i;j++){//交换位置}

Recommended: "javalearning"

The above is the detailed content of Implementation of bubble sorting java code. 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

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

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.

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software