冒泡算法-bubble


冒泡算法在数据只有几个无序时是最快的算法,但是如果全部无序的话就变成了最慢的算法了,时间复杂度为O(n^2)
public class bubbleSort { public static void main(String[] args) { // TODO Auto-generated method stub int aa[]={2,34,45,6545,6546,234,234,65,6,752,32}; bubble(aa); for(int j=0;j<aa.length;j++){ System.out.println(aa[j]); } } public static void bubble(int [] sorted){ for(int i=0;i<sorted.length;i++){ for(int k=0;k<sorted.length-1;k++){ if(sorted[k+1]<sorted[k]){ int tmp=sorted[k+1]; sorted[k+1]=sorted[k]; sorted[k]=tmp; } } } } }
原文地址:https://www.cnblogs.com/luo-mao/p/6038143.html