java 冒泡排序

来源:互联网 发布:mysql怎么删除数据库 编辑:程序博客网 时间:2024/06/01 11:56

public class BubbleSort
{
 public static void bubbleSort(int[] array)
 {
     for (int i = 0; i < array.length - 1; i++)
     {
           for(int j = 0; j < array.length - i - 1; j++)
   {
           if(array[j] > array[j + 1])
    {
      int temp = array[j];
         array[j] = array[j + 1];
      array[j + 1] = temp;
    }
    System.out.println("第" + (i + 1) + "趟排序");
          for(int n = 0; n < array.length; n++)
          {
             System.out.print(array[n] + " ");
           }
      System.out.println();
       }
     
     }
  
 }
 public static void main(String[] args)
 {
 int[] array = {5, 1, 4, 6, 8};
        bubbleSort(array);
 }

}

原创粉丝点击