1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
|
import java.util.Arrays;
public class TestBubble2 {
public static void main(String[] args) { // TODO Auto-generated method stub int[] arr1 = {11, 221, 321, 21, 214, 21, 13}; bubble(arr1); System.out.println(Arrays.toString(arr1)); } public static void bubble(int[] arr) { if(arr == null || arr.length < 2) { return; } int temp; boolean flag = true; for(int i = 0; i < arr.length; i++) { for(int j = 0; j < arr.length - 1 - i; j++) { if(arr[j] > arr[j+1]) { temp = arr[j]; arr[j] = arr[j+1]; arr[j+1] = temp; flag = false; } } if(flag) { break; } } } }
|
近期评论