标签:
1 /** 2 *冒泡排序: 3 * 两个两个比较,一轮过后最大的排在了最后面 4 * n个数变为n-1个没排好的数 5 * 再进行一轮 6 * 第二大的排在了倒数第二个 7 * 以此类推 8 * 直到排到第一个为止 9 * 10 * 弄两个循环,相邻两个数比较 11 */ 12 public class BubbleSort 13 { 14 /** 15 *冒泡排序主方法 16 * 17 */ 18 public static void bubbleSort(int[] resouceArr) 19 { 20 for(int i = 0 ; i <= resouceArr.length - 1 ; i++) 21 { 22 //数组下标是数组长度-1 , n-1个j,最后一个不能算所以还要-1 23 //一趟排序完成后,数组大小减小1个,因为后面的已经排好序了。 24 for(int j = 0 ; j <= resouceArr.length -1 - 1 - i ; j++) 25 { 26 if(resouceArr[j] > resouceArr[j+1]) 27 { 28 int temp = resouceArr[j]; 29 resouceArr[j] = resouceArr[j+1]; 30 resouceArr[j+1] = temp; 31 } 32 } 33 } 34 } 35 36 public static void main(String[] args) 37 { 38 int[] bubbleArr = new int[]{1,3,6,2,7,9,5,8}; 39 bubbleSort(bubbleArr); 40 for(int i : bubbleArr) 41 { 42 System.out.println(i); 43 } 44 } 45 }
标签:
原文地址:http://www.cnblogs.com/fubinhnust/p/4183295.html