package bubbleSort;
/**
* @author 黄河祥
* @create 2020/3/23 20:51
* 冒泡排序:每一趟把最大的值找出来排到最后
*/
public class BubbleSort {
public static void main(String[] args) {
int arr[] = {2, 1, 4, 1, 28, 6, 10, 3, 7, 111, 0, -1};
bullleSort(arr);
System.out.println("排序后:");
for (int i : arr) {
System.out.print(i + " ");
}
}
private static void bullleSort(int[] arr) {
int temp;//交换变量临时值
for (int i = 0; i < arr.length; i++) {
for (int j = 0; j < arr.length - i - 1; j++) {
if (arr[j] > arr[j + 1]) {//如果前面的大于后面的,则交换位置
temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
}
}
冒泡排序
最新推荐文章于 2024-09-30 16:13:53 发布