1. package io.github.chenshun00.web.sort;
    2. import java.util.Arrays;
    3. /**
    4. * @author luobo.cs@raycloud.com
    5. * @since 2021/8/12 9:44 上午
    6. */
    7. public class BubbleSort {
    8. public static void main(String[] args) {
    9. int[] array = {9, 8, 7, 6, 5, 4, 3, 2, 1};
    10. sort(array);
    11. System.out.println(Arrays.toString(array));
    12. }
    13. public static void sort(int[] array) {
    14. for (int i = 0; i < array.length; i++) {
    15. final int i1 = array[i];
    16. int j = i + 1;
    17. for (; j <= array.length - 1; j++) {
    18. final int j2 = array[j];
    19. if (i1 > j2) {
    20. swap(array, i, j);
    21. }
    22. }
    23. }
    24. }
    25. public static void swap(int[] array, int a, int b) {
    26. int temp = array[a];
    27. array[a] = array[b];
    28. array[b] = temp;
    29. }
    30. }