search
HomeJavajavaTutorialNewbies learn how to learn JAVA bubble sort

This article will introduce to you how newbies can learn JAVA bubble sorting. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to everyone.

Newbies learn how to learn JAVA bubble sort

Bubble Sort is a relatively simple sorting algorithm in the field of computer science.
It repeatedly visits the elements that need to be sorted, compares two adjacent elements in turn, and swaps the positions of the elements if the order of the elements (such as from large to small, first letter from A to Z) is wrong.
Visiting elements is repeated until there are no adjacent elements that need to be swapped, and sorting is completed.
The name of this algorithm comes from the fact that larger elements will slowly "float" to the top of the sequence through exchange (arranged in ascending or descending order), just like the carbon dioxide bubbles in carbonated drinks will eventually float to the top, hence the name "Bubble Sort".
You may be a little confused just by looking at the theory, but it doesn’t matter. Next, let’s learn in detail how bubble sorting compares and how it is sorted~

Sort ideas

Adjacent comparison, sort from small to large, if smaller, move forward
I represents traversing the loop data from beginning to end

Newbies learn how to learn JAVA bubble sortImplementing bubble sorting

Create class: BubbleSort.java

package cn.tedu.array;import java.util.Arrays;/**本类用来完成Newbies learn how to learn JAVA bubble sort*/public class TestBubbleSort {
	public static void main(String[] args) {
		//1.创建一个无序的数组
		int[] a = {27,96,73,25,21};
		//2.调用method()完成排序
		int[] newA = method(a);
		System.out.println("排序完毕:"+Arrays.toString(newA));
	}
	public static int[] method(int[] a) {
		//1.外层循环,控制比较的轮数,假设有n个数,最多比较n-1次
		//开始值:1 结束值:<= a.length-1 变化:++
		//控制的是循环执行的次数,比如5个数,最多比较4轮,<= a.length-1,最多取到4,也就是[1,4]4次
		for(int i = 1 ; i <= a.length-1 ; i++) {
			System.out.println("第"+i+"轮:");
			//2.内层循环:相邻比较+互换位置
			for(int j=0; j < a.length-i ; j++) {
				//相邻比较,a[j]代表的就是前一个元素,a[j+1]代表的就是后一个元素
				if(a[j] > a[j+1]) {
					//交换数据
					int t = a[j];
					a[j] = a[j+1];
					a[j+1] = t;
					//System.out.println("第"+(j+1)+"次比较交换后:"+Arrays.toString(a));
				}
			}
			System.out.println("第"+i+"轮的结果:"+Arrays.toString(a));
		}
		return a;//把排序好的数组a返回
	}}

In fact, we can also optimize the existing sorting:
Optimization 1: The maximum value generated by the previous rounds of sorting does not need to be involved in the post-processing After several rounds of comparison, several values ​​will be generated and there is no need to participate in the comparison. The i round produces i values, so it is necessary - i

Optimization 2: We need to set a quantity, which is used Check whether there has been an exchange of elements in the current round of mutual comparison. If an exchange has occurred, it means that the order has not been arranged. The flag will be changed to true and the next round of comparison will be carried out. But if in the current round , all elements have been compared with each other and have not exchanged positions, which means that the order has been sorted. There is no need for the next round of comparison, just return the end method directly
Newbies learn how to learn JAVA bubble sort

Related free learning recommendations: java basic tutorial

The above is the detailed content of Newbies learn how to learn JAVA bubble sort. For more information, please follow other related articles on the PHP Chinese website!

Statement
This article is reproduced at:CSDN. If there is any infringement, please contact admin@php.cn delete

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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Tools

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

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 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development 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.