Home > Java > Javagetting Started > Implementation of bubble sorting java code

Implementation of bubble sorting java code

藏色散人
Release: 2020-05-12 09:33:59
Original
9015 people have browsed it

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++){//交换位置}
Copy after login
Copy after login

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++){//交换位置}
Copy after login
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template