首页 > 代码库 > 冒泡排序

冒泡排序

1.普通冒泡排序

 1 package BubbleSort; 2 /** 3  * 冒泡排序 4  */ 5 import java.util.Arrays; 6  7 public class BubbleSort1 { 8     public static void main(String[] args) { 9     int[] array = { 9, 89, 4, 45, 67, 3, 96, 631 };10     boolean sort;11     for (int i = 0; i < array.length - 1; i++) {12         sort = true;13         for (int j = 0; j < array.length - i - 1; j++) {14         if (array[j] > array[j + 1]) {15             int temp = array[j];16             array[j] = array[j + 1];17             array[j + 1] = temp;18             sort = false;19         }20         if (sort = true) {21             break;22         }23         }24     }25     System.out.println(Arrays.toString(array));26     }27 }

 

冒泡排序