冒泡算法在数据只有几个无序时是最快的算法,但是如果全部无序的话就变成了最慢的算法了,时间复杂度为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; } } } } }

 

相关文章:

  • 2022-12-23
  • 2021-12-06
  • 2021-06-06
  • 2022-12-23
  • 2021-10-07
  • 2021-06-22
  • 2021-11-28
猜你喜欢
  • 2021-09-13
  • 2022-12-23
  • 2021-11-04
  • 2021-10-25
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案