1.22 Java基础总结 最常用冒泡排序

一、最常用冒泡排序(易记忆)两层循环都重0到length-1,第二层多减i更有效率,交换为相邻两个

for(int i=0;i<score.length-1;i++){
  for(int j=0;j<score.length-1;j++){
    if(score[j]>score[j+1]){//改变符号就降序排序
      int t=score[j];
      score[j]=score[j+1];
      score[j+1]=t;
    }
  }
}

二、最常用排序二    (比较相邻两个)

for(int i=0;i<score.length-1;i++){
  for(int j=i+1;j<score.length;j++){
    if(score[i]>score[j]){//改变符号就降序排序
      int t=score[i];
      score[i]=score[j];
      score[j]=t;
    }
  }
}

三、调用系统方法

int[] arrray = new array[10];

Arrays.sort(array);//升序排列

此方法里还有各种数据类型,从某个索引到某个索引的排序

原文地址:https://www.cnblogs.com/chenyuanqiu2008/p/5155100.html