首页 > 编程语言 > 详细

冒泡排序算法(java实现)

时间:2018-07-17 21:36:58      阅读:153      评论:0      收藏:0      [点我收藏+]

public class HelloJava{
 public static void main(String[] args) {
  int[] arr1 = new int[] {20,40,90,30,80,70,50};
  bubbleSort(arr1);
  int[] arr2 = new int[] {11,52,4,5,65,4,2,1,45};
  bubbleSort(arr2);
 }
 public static void bubbleSort(int[] array) {
  System.out.println("排序前: ");
  for(int i=0; i<array.length; i++) {
   System.out.print(array[i]+" ");
  }
  System.out.print("\n"+"冒泡排序后 : "+"\n");
  int flag;
  for(int i=1; i<array.length; i++) {
   flag = 0;
   for(int j=0; j<array.length-i; j++) {
    if(array[j] > array[j+1]) {
     flag = 1;
     int temp = array[j+1];
     array[j+1] = array[j];
                    array[j] = temp;      
    }
    System.out.print(array[j]+" ");
   }
   
   System.out.print("[ ");
   for(int j=array.length-i; j<array.length; j++) {
    System.out.print(array[j]+" ");
   }
   System.out.println("]");
   if(flag == 0)
       break;
  }
 }
}

冒泡排序算法(java实现)

原文:https://www.cnblogs.com/guangxiaolin/p/9325980.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!