java之冒泡排序

来源:互联网 发布:在淘宝网上怎么买东西 编辑:程序博客网 时间:2024/05/17 07:37

package com.test1;
public class Test1 {
 public static void main(String[] args) {
  //降序
  int arr[] ={2,3,1,5,7,6,4,8,9,10,13,12,11};
  //第二步循环arr.length次依次比较比较arr[i]大的数放在前面
  for (int i = 0; i < arr.length; i++) {
   //第一步根据arr[0]去比较数组中的其他元素把比它大的数放在它的前面
   for (int j = 0; j < arr.length-1; j++) {
     if(arr[j]<arr[j+1]){
      int temp=0;
      temp=arr[j];
      arr[j]=arr[j+1];
      arr[j+1]=temp;
     }
   }
  }
  //输出arr[i]
  for (int j = 0; j< arr.length; j++) {
   System.out.print(arr[j]+"\t");
  }
 }
}

 

     下面简单的解释一下原理:


0 0
原创粉丝点击