Java冒泡排序

2020-02-12  本文已影响0人  手打小黑板
package com.liming.sort;

public class BubbleTest {
    public static void main(String[] args) {
        int[] intlist = new int[1024*128];
        for (int i = 0; i < intlist.length; i++) {
            intlist[i] = (int)(Math.random()*1024*128);
        }

        //flag 
        int exchange;
        boolean change=false;//检测是否更改过

        long startTime = System.currentTimeMillis();
        for (int j = 0; j < intlist.length - 1; j++) {
            for (int i = 0; i < intlist.length - j - 1; i++) {
                if(intlist[i] > intlist[i+1]){
                    exchange = intlist[i];
                    intlist[i] = intlist[i+1];
                    intlist[i+1] = exchange;
                    change = true;  //交换过排序
                }
            }
            if(!change){
                break;
            }
            change = false;
        }
        System.out.println("所用时间:" + (System.currentTimeMillis() - startTime));

       /* for (int i = 0; i < intlist.length; i++) {
            System.out.println(intlist[i]);
        }*/
        
    }
}

上一篇 下一篇

猜你喜欢

热点阅读