算法之排序算法-冒泡排序与优化

package com.ebiz.sort;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @author YHj
 * @create 2019-07-26 16:11
 */
public class Bubble {


    public static void main(String[] args) {


        int[] arr = new int[80000];
        for (int i = 0; i < 80000; i++) {
            arr[i] = (int) (Math.random() * 800000);
        }



        String s = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
        System.out.println("排序前 = " + s);

        getResult(arr);


        String l = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
        System.out.println("排序后 = " + l);


    }


    public static void getResult(int[] arr) {
        //临时变量
        int temp;
        for (int i = 0; i < arr.length - 1; i++) {
            //优化标识
            boolean flag = false;
            //内层
            for (int j = 0; j < arr.length - 1 - i; j++) {
                if (arr[j] > arr[j + 1]) {
                    flag = true;
                    temp = arr[j];
                    arr[j] = arr[j + 1];
                    arr[j + 1] = temp;
                }
            }
            //判断标识符
            if (!flag) {
                break;
            }
        }
    }




}

待完善...

猜你喜欢

转载自www.cnblogs.com/jiushixihuandaqingtian/p/11299202.html