美文网首页
【Java】冒泡排序

【Java】冒泡排序

作者: 董懂同学 | 来源:发表于2020-03-27 14:17 被阅读0次
    public class BubbleSort {
        public static void bubbleSort(int[] array) {
            int length = array.length;
            for (int i = 0; i < length - 1; i++) { // 这个循环记录扫描到了第几轮
                for (int j = 0; j < length - 1 - i; j++) { // 这个循环扫描交换未排序的数
                    if (array[j] > array[j + 1]) {
                        int temp = array[j];
                        array[j] = array[j + 1];
                        array[j + 1] = temp;
                    }
                }
            }
        }
    
        public static void main(String[] args) {
            int[] nums = {30, 20, 40, 10};
            bubbleSort(nums);
            for (int i = 0; i < nums.length; i++) {
                System.out.println(nums[i]);
            }
        }
    }
    

    相关文章

      网友评论

          本文标题:【Java】冒泡排序

          本文链接:https://www.haomeiwen.com/subject/tjpluhtx.html