package com.example.demo.sort;
import java.util.Arrays;
/**
* 冒泡排序
* 相邻元素两两比较,
* 每一路结束后会在队尾的有序区域多一个元素
* 一共需要遍历size-1轮
* 时间复杂度O(n²)
*/
public class BubbleSortDemo {
public static void main(String[] args) {
int[] demo = {17, 5, 12, 8, 15, 6, 19, 10, 18, 10, 3, 9, 14, 2, 11, 4, 1, 16, 7, 13};
// bubbleSortV1(demo);
// bubbleSortV2(demo);
bubbleSortV3(demo);
}
/**
* v1 未优化
*
* @param demo
*/
public static void bubbleSortV1(int[] demo) {
long t1 = System.currentTimeMillis();
for (int i = 0; i < demo.length - 1; i++) {
for (int j = 0; j < demo.length - i - 1; j++) {
//内层循环如果写成demo.length-1也可以,只是相当于把队尾的有序区域也拿进来参与比较了
if (demo[j] > demo[j + 1]) {
int temp = demo[j];
demo[j] = demo[j + 1];
05-03
4万+
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)
07-25
06-29
“相关推荐”对你有帮助么?
-
非常没帮助
-
没帮助
-
一般
-
有帮助
-
非常有帮助
提交