package com.example.demo.study;
/**
*
* @author Administrator
*/
public class BubbleSort {
public static void main(String[] args) {
int [] arr = {2,4,1,8,6,5,7,5,4};
BubbleSort.bubbleSort(arr);
for (int i = 0; i < arr.length; i++) {
System.out.println(arr[i]);
}
}
private static void bubbleSort(int [] arr) {
int temp = 0;
//1.外循环控制排序需要走多少趟
for (int i = 0; i < arr.length-1; i++) {
//2.内循环控制每趟需要比较的次数
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;
}
}
}
}
}