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 Article

Hot Tools

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

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),

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment