冒泡排序1:
public class Bubbling { public static void sort(int array[]) { for(int i =0; i < array.length -1; i++) { for(int j = 0; j < array.length -1 -i; j++) { int tmp = 0; if(array[j] > array[j+1]) { tmp = array[j]; array[j] = array[j+1]; array[j+1] = tmp; } } } } public static void main(String[] args){ int[] array = new int[]{5,8,6,3,9,2,1,7}; sort(array); System.out.println(Arrays.toString(array)); } }
冒泡排序优化2
public class Bubbling1 { public static void sort(int array[]) { for(int i =0; i < array.length -1; i++) { //有序标记,每一轮的初始值都是true boolean isSorted = true; for(int j = 0; j < array.length -1 -i; j++) { int tmp = 0; if(array[j] > array[j+1]) { tmp = array[j]; array[j] = array[j+1]; array[j+1] = tmp; //因为有元素进行交换,所以不是有序的,标记变为false isSorted = false; } } if(isSorted){ break; } } } public static void main(String[] args){ int[] array = new int[]{5,8,6,3,9,2,1,7}; sort(array); System.out.println(Arrays.toString(array)); } }
冒泡排序优化3
public class Bubbling2 { public static void sort(int array[]) { //记录最后一次交换的位置 int lastExchangeIndex = 0; //无序数列的边界,每次比较只需要比到这里为止 int sortBorder = array.length - 1; for(int i =0; i < array.length -1; i++) { //有序标记,每一轮的初始值都是true boolean isSorted = true; for(int j = 0; j < sortBorder; j++) { int tmp = 0; if(array[j] > array[j+1]) { tmp = array[j]; array[j] = array[j+1]; array[j+1] = tmp; //因为有元素进行交换,所以不是有序的,标记变为false isSorted = false; // 更新为最后一次交换元素的位置 lastExchangeIndex = j; } } sortBorder = lastExchangeIndex; if(isSorted){ break; } } } public static void main(String[] args){ int[] array = new int[]{5,8,6,3,9,2,1,7}; sort(array); System.out.println(Arrays.toString(array)); } }
冒泡排序4
public class Bubbling3 { public static void sort(int array[]) { int tmp = 0; for(int i=0; i<array.length/2; i++) { //有序标记,每一轮的初始值都是true boolean isSorted = true; //奇数轮,从左向右比较和交换 for(int j=i; j<array.length-i-1; j++) { if(array[j] > array[j+1]) { tmp = array[j]; array[j] = array[j+1]; array[j+1] = tmp; //因为有元素进行交换,所以不是有序的,标记变为false isSorted = false; } } if(isSorted){ break; } // 在偶数轮之前,将isSorted重新标记为true isSorted = true; //偶数轮,从右向左比较和交换 for(int j=array.length-i-1; j>i; j--) { if(array[j] < array[j-1]) { tmp = array[j]; array[j] = array[j-1]; array[j-1] = tmp; //因为有元素进行交换,所以不是有序的,标记变为false isSorted = false; } } if(isSorted){ break; } } } public static void main(String[] args){ int[] array = new int[]{2,3,4,5,6,7,8,1}; sort(array); System.out.println(Arrays.toString(array)); } }