冒泡排序
1 int[] arr = {5, 8, 9, 2, 45, 24, 62, 41, 26}; 2 3 /** 4 * 5 * @Description 冒泡排序 6 * @throws Exception 7 */ 8 @Test 9 public void testBubble() throws Exception { 10 11 int temp; 12 for (int i = 0; i < arr.length; i++) { 13 for (int j = 0; j < arr.length - i -1; j++) { 14 if(arr[j] > arr[j+1]){ 15 temp = arr[j+1]; 16 arr[j+1] = arr[j]; 17 arr[j] = temp; 18 } 19 } 20 } 21 22 System.out.println(Arrays.toString(arr)); 23 }
结果:[2, 5, 8, 9, 24, 26, 41, 45, 62]
IT技术和行业交流群 417691667