Sorting Algorithm - Bubble Sort (Java)

package com.rao.sort;

import java.util.Arrays;

/**
* @author Srao
* @className BubbleSort
* @date 2019/12/4 12:33
* @package com.rao.sort
* @Description 冒泡排序
*/
public class BubbleSort {

/**
* 冒泡排序
* @param arr
*/
public static void bubbleSort(int[] arr){
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]){
int temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
}
}
}
}

public static void main(String[] args) {
int[] arr = new int[]{3,6,2,5,9,1,0,8};
System.out.println(Arrays.toString(arr));
bubbleSort(arr);
System.out.println(Arrays.toString(arr));

}
}

1. comparison with the latter element is the first element of the array

2. If the element is larger than the latter, then the two exchange number, then this number is large compared to the following

3. After this round down, the largest of the last element in the array surface

4. The number of cycles of the operation, every time that the rest of the largest one on the final surface, and finally became an ordered array

Guess you like

Origin www.cnblogs.com/rao11/p/11982204.html