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 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
| public class Solution { public static void swag(int[] arr,int i,int j) { int tmp; tmp=arr[i]; arr[i]=arr[j]; arr[j]=tmp; } public static void swag_nonum(int[] arr,int i,int j) { arr[i]+=arr[j]; arr[j]=arr[i]-arr[j]; arr[i]=arr[i]-arr[j]; } public static void swag_binary(int[] arr,int i,int j) { arr[i]^=arr[j]; arr[j]^=arr[i]; arr[i]^=arr[j]; } public static void BubbleSort(int[] arr) { boolean swapped=true; for(int i=0;i<arr.length-1;i++) { if(!swapped) break; swapped=false; for(int j=0;j<arr.length-1-i;j++) { if(arr[j]>arr[j+1]) { swag(arr,j,j+1); swapped=true; } } } }
public static void main(String[] args) { int arr[]= {9,8,6,4,1,5,9,36,54,7,0,2,5,-1,78}; BubbleSort(arr); for (int i : arr) { System.out.print(i+","); } }
}
|