See More

package others; import java.util.Arrays; public class BubbleSort { // ¶ÔÒ»´ó¶ÑÂÒÆß°ËÔãµÄÊý×ÖÅÅÐò£¬Ð¡µÄÔªËØ ±È½ÏÇá ÏñÆøÅÝÒ»ÑùÉϸ¡ public static void main(String[] args) { int[] input = {1,3,5,7,9,3,2,6}; bubbleSort(input); System.out.println(Arrays.toString(input)); } public static void bubbleSort(int[] array) { int size = array.length; for (int i = size - 1; i > 0; i--) { for (int j = 0; j < i; j++) { if (array[j] > array[j + 1]) { exchangeElements(array, j, j + 1);// ÏàÁÚµÄÁ½¸ö»»Î»Öà } } } } // ½»»»£¬Õâ¸ö·½·¨ºÜ³£Óà public static void exchangeElements(int[] array, int index1, int index2) { int temp = array[index1]; // µÚÒ»¸ö array[index1] = array[index2]; // µÚ¶þ¸ö¸³¸øµÚÒ»¸ö array[index2] = temp; // µÚÒ»¸ö¸³¸øµÚ¶þ¸ö } }