美文网首页
Java冒泡排序

Java冒泡排序

作者: 手打小黑板 | 来源:发表于2020-02-12 09:53 被阅读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]);
        }*/
        
    }
}

相关文章

网友评论

      本文标题:Java冒泡排序

      本文链接:https://www.haomeiwen.com/subject/avcffhtx.html