import java.util.Arrays;
/**
* Created by yajie.wang on 2018/10/31.
*/
public class BubbleSort {
public static void main(String [] args){
int [] arr = {6,3,8,2,9,1,10,24,33,88,44,36,52};
bubbleSort2(arr);
System.out.println(Arrays.toString(arr));
}
private static void bubbleSort(int[] arr) {
for (int i = 0; i < arr.length-1; i++) {
for (int j = 0; j < arr.length-1 -i; j++) {
if (arr[j]>arr[j+1]){
int temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
}
}
}
}
private static void bubbleSort2(int [] arr) {
int len = arr.length;
int flag = len;
while (flag > 0) {
flag = 0;
for (int i = 0; i < len-1; i++) {
if (arr[i] > arr[i+1]) {
arr[i]+=arr[i+1];
arr[i+1] = arr[i]-arr[i+1];
arr[i] = arr[i]-arr[i+1];
flag = i +1;
}
}
len = flag;
}
}
}