First, the principle
(1) Compare the two adjacent elements. If the first one is bigger than the second one, swap them.
(2) for each pair of adjacent elements to do the same work, from the beginning of the first pair to the end of the last pair. At this point, the last element should be the maximum number.
(3) Repeat the above steps for all elements except the last one.
(4) Repeat the above steps each time for less and fewer elements until there are no pairs of numbers to compare.
Second, the realization
Package com.example.sort;/** * * @author Binchao * */public class Bubblesort {public static void main (string[] Arg s) {//TODO auto-generated method stubint[] ar = {5, 58};ar = Bubblesort (AR); for (int i:ar) {System.out.pri NT (i + "");}} /** * Bubble Sort * @param arr * @return */public static int[] Bubblesort (int[] arr) {int temp;for (int i = 0; i < arr.length; i++) {for (int j = 0; J < Arr.length-1; J + +) {//small float if (arr[j] > arr[j+1]) {temp = Arr[j];arr[j] = arr[j+1];arr[ J+1] = temp;}}} return arr;}}
Bubble algorithm of sorting algorithm