Bubble sort
Bubble sort is the comparison of two adjacent elements, through the constant comparison, the larger number to sink, smaller to the top, which is the source of bubble sort name
The first trip starts with a number labeled 0 in the array, arr[0] greater than arr[1] to exchange their position, continue to compare arr[1] and arr[2], until Arr[n]
The second trip starts with a number labeled 1 in the array, arr[1] greater than arr[2] to continue to swap positions, continue to compare arr[2] and arr[3], until Arr[n]
And so on
Algorithm introduction (subsequent additions)
Performance Introduction (Subsequent additions)
Introduction to Suboptimal (Follow-up supplement)
Code presentation (for example from small to large)
package com.nbj.test;/***冒泡排序*/public class BubbleSort{ public static void main(String [] args){ int[] arr= {1,22,32,2,5,75,46,37,23,99}; for (int i = 0; i < arr.length-1;i++){ for (int j = 0; j < arr.length-1-i;j++){ if(arr[j] > arr[j+1]){ arr[j+1] = arr[j] + arr[j+1]; arr[j] = arr[j+1] - arr[j]; arr[j+1] = arr[j+1] -arr[j]; } } } for(int i =0 ;i < arr.length; i++){ System.out.println(arr[i]); } }}
Bubble sort of Java basics