冒泡排序的优化

xiaoxiao2021-02-28  75

近期我学习了一下算法,对冒泡排序有点看法

package op; import java.util.Iterator; public class sortmaopao { public static void main(String[] args) { Integer[] age={20,50,40,60,30}; int count=0; for (int i = 0; i < age.length; i++) { for (int j = 0; j < age.length-1; j++) { if (age[j]>age[j+1]) { int temp = age[j]; age[j] = age[j+1]; age[j+1] = temp; } count++; } } System.out.println("整个排序交互了"+count+"次"); } }

这个冒泡排序是排了20次

package op; import java.util.Iterator; public class sortmaopao { public static void main(String[] args) { Integer[] age={20,50,40,60,30}; int count=0; for (int i = 0; i < age.length; i++) { for (int j = 0; j < age.length-1-i; j++) { if (age[j]>age[j+1]) { int temp = age[j]; age[j] = age[j+1]; age[j+1] = temp; } count++; } } System.out.println("整个排序交互了"+count+"次"); } }

这个冒泡排序与上一个相差不大只有10次优化了一半

转载请注明原文地址: https://www.6miu.com/read-71359.html

最新回复(0)