冒泡排序算法
2019-03-22 / JAVA / 871 次围观 / 0 次吐槽 /int[] arr = {18,30,69,29,25,10,0}; //1.比较4轮 for(int i=0;i<arr.length-1;i++){ System.out.println("第" + (i + 1) + "轮比较"); //2.打印出每一轮比较的对应索引 for(int j=0;j < arr.length - 1 - i; j++){ //System.out.println(j); int left = j; int right = j + 1; System.out.println(left + "-" + right); //换位置 if(arr[left] > arr[right]){ int tmp = arr[left]; arr[left] = arr[right]; arr[right] = tmp; } } }
原理:相临的两个数比较,大的数放在右边
Powered By Cheug's Blog
Copyright Cheug Rights Reserved.