search
HomeJavaJavaBaseHow to implement bubble sorting of arrays in java

How to implement bubble sorting of arrays in java

Nov 26, 2019 pm 03:22 PM
javaBubble Sortarray

How to implement bubble sorting of arrays in java

Bubble sort

Compare two adjacent elements in turn. If the former is greater than the latter, the positions are swapped. After each sorting pass, the The maximum value in this pass is placed last, and the appeal process is repeated until there are no more elements to compare.

java related free learning videos: java learning videos

Examples are as follows:

public class Bubble_Sort {
	public static void main(String[] args){
		int[] arr = {2,4,1,5,6,9,0,6,7};
		bubbleSort(arr);
	}
	// 打印数组
	public static void print(int[] arr){
		for(int i = 0;i < arr.length;i++){
			System.out.print(arr[i]+"\t");
		}
		System.out.println();
	}
	/**
		冒泡排序
		依次比较两个相邻的元素,如果前者大于后者就交换位置
		每一趟排序之后就会把这趟中的最大值放在最后一位
		重复上诉过程,直到没有在需要比较的元素为止
	*/
	public static void bubbleSort(int[] arr){
		
		// 多趟比较,直到没有需要再比较的元素为止,最后的一个值不需要比较
		for(int i = 0;i < arr.length-1;i++){

			// 依次比较相邻的两个元素,每次比较的总个数少一个
			for(int j = 0;j < arr.length-i-1;j++){
				if(arr[j] > arr[j+1]){
					int temp = arr[j+1];
					arr[j+1] = arr[j];
					arr[j] = temp;
				}
			}
			print(arr);
		}
	}
}

Recommended related articles and tutorials: Getting started with java study

The above is the detailed content of How to implement bubble sorting of arrays in java. 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 Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
Will R.E.P.O. Have Crossplay?
1 months agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools