Home > Java > javaTutorial > Newbies learn how to learn JAVA bubble sort

Newbies learn how to learn JAVA bubble sort

醉折花枝作酒筹
Release: 2021-05-07 09:13:19
forward
2672 people have browsed it

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返回
	}}
Copy after login

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!

Related labels:
source:csdn.net
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