java实现冒泡排序

总结一下自己的学习成果,留着以后回顾

package sort.bubblesorter;

import java.util.Random;

public class SorterUtils {

    public static void main(String[] args) {
        /*Random rand = new Random();
        for (int j = 0; j < 1000; j++) {
            System.out.print(rand.nextInt(1000000) + ",");
        }*/
        SorterUtils bs = new SorterUtils();
        int[] arr = {305095,914174,825383,754767,142756,981171,329499,310849,170028,997102,468727,94832,985477,202690,660958,256574,476840,367023,805785,278900,799592,237095,72428,325084,829859,466542,539668,521708,604356,601626,989792,365596,849650,457850,291817,941785,301592,23315,233392,592850,265618,872646,308818,831407,582449,56909,892394,72929,639979,808356,358467,276500,607586,792132,450168,32386,543503,386122,405803,952289,51531,583671,488990,239932,974842,542801,120945,312288,46740,371430,494392,823148,730193,997872,639224,878540,627056,442068,779967,20351,257701,755653,889757,55571,372573,298565,121474,829491,87087,126348,109301,107991,420448,743809,88943,551257,34067,924773,757010,324904,723915,190287,296969,804885,863775,964370,813629,999947,702009,377850,870576,390364,258954,912181,51834,188150,610958,259727,769950,823860,195319,610198,66889,769488,673401,619356,652326,804343,327921,163483,64928,226165,380210,525299,717877,435215,766254,447630,220596,450928,71998,231780,743379,262331,646478,587930,156527,515007,600104,276934,109920,718600,618523,725262,196472,681170,749384,488110,803545,843353,776008,694039,585840,161932,817597,595765,346258,748384,475380,959753,799729,599488,40948,793781,32698,144225,967142,498903,721871,774324,745440,855884,634460,316405,151248,985878,41880,874529,614465,497368,233626,138500,755533,11582,748181,627952,614432,386361,140658,159369,161380,954600,239689,256638,618062,829714,577723,955090,277829,387024,426862,80664,400938,382216,954918,258493,39368,778388,705085,728255,975513,430272,773887,645052,77427,423538,677988,510821,275022,958565,493738,839249,989234,615885,436367,228782,842049,45023,277288,450770,407198,693033,634465,839472,623161,138844,473746,315237,154681,833194,622073,513778,6660,531146,627354,100421,292782,398745,30128,631600,379899,772890,821326,905595,586643,514078,181975,932116,250625,141823,744406,89445,385959,664454,93883,897406,731522,270444,328691,650161,802710,763582,277027,95741,804534,484505,885312,926306,555058,451477,967821,232660,700637,534847,359154,842698,179641,331774,696054,359925,786178,769750,960217,176910,918292,320331,374609,193608,900217,109853,88543,897253,945171,282290,662028,19813,823522,969426,168901,150680,779891,138700,32272,967189,27942,600757,680170,475203,301210,654096,674815,595540,650487,955785,15109,131538,923671,530908,930678,777617,589073,902547,785640,48133,88356,33900,256842,886252,974397,818520,39485,254955,360677,75872,709645,511799,833482,358243,641546,400490,749289,696762,862279,612790,210068,624060,431022,414517,185253,855965,863998,615646,489727,540209,118179,250644,906301,621327,237873,529048,801083,301328,50428,269919,279869,196791,986262,969215,579583,264682,313894,941763,470617,99167,74337,654813,752463,947474,152680,30715,585011,563506,383294,312562,663543,143324,505636,900951,726099,633662,625427,62573,771727,561374,633046,116984,710638,770355,986689,926768,640631,925745,627601,396369,824801,138786,228258,933071,809498,822000,923783,341991,700148,825343,831306,269393,874732,587447,863040,548946,617177,547999,274631,635693,680622,14117,525854,779961,948843,857594,83682,919438,996262,570266,132193,484697,741490,141417,374173,355663,495017,689145,528351,385726,829660,674792,120180,593023,115219,394854,195657,744328,408131,985429,255582,602462,33611,578441,554346,205799,449834,107374,705484,65054,830594,696009,505512,94303,741441,698263,699302,608546,631602,997457,835267,440388,702303,592209,571602,474113,128480,733391,461587,114239,791173,841028,44462,916611,773275,738223,535821,3142,679199,181685,516883,326050,321469,308003,463356,61095,817987,562512,534162,233355,604327,463134,126217,678077,98353,819935,99704,931674,870406,35411,91252,865209,288723,415183,136587,393569,856786,470138,436634,666,970236,297586,395723,819488,721291,66124,647855,860212,247787,934519,331508,639886,487164,486017,357635,547757,250886,952244,93049,345998,838330,789337,487359,549371,471137,771740,986760,745338,559076,132367,719910,374311,260790,333321,103306,8348,791394,508903,334550,399372,261996,812018,679969,500719,700199,946465,738758,245463,546387,542133,861417,489058,304368,147522,600008,771336,905763,613838,822521,515604,373956,506962,396613,797808,418562,920285,462817,424728,429876,480920,634565,74737,64957,925156,225004,629555,233818,510432,47462,882496,586635,179334,781661,310553,998683,477813,900514,610278,324430,1033,882803,624178,477031,868518,267084,260218,282259,295380,100838,535962,202037,309380,933712,478312,811407,56689,173609,274396,691145,348130,360312,256940,549921,421644,753640,705079,180720,796820,528483,564439,620373,420593,915017,7897,786560,370032,513101,579721,905693,557457,213228,848294,998874,864143,343051,982324,349463,430653,189997,829041,930154,557132,651214,839519,848139,844649,675358,34944,160355,212291,321245,112736,547244,830890,64847,256396,200580,258835,186077,395104,485012,553336,475848,218878,696279,334236,98954,778907,987530,166060,973131,90227,654831,770113,124412,362781,148605,395714,130068,117753,108903,600039,532901,282437,247559,509912,253402,377039,301159,926067,307407,239045,488876,880493,848845,85224,876896,338768,196641,960287,994266,184610,996311,253224,893452,950694,448792,173673,691753,186492,354205,100453,516273,969827,304911,513802,901466,326815,750845,71078,783964,433398,864953,507322,988122,453908,107536,223079,3624,368782,38627,988869,606141,601667,575782,34835,73078,602847,857333,993224,391812,433081,253774,601928,774452,549468,22511,399986,754010,949411,204685,73558,904025,193405,94638,189700,122960,575121,783204,389055,270244,916347,447462,626065,800742,734771,232204,51192,497505,972175,918951,668542,815107,89439,119802,291110,407141,296246,324569,142146,877177,794824,511709,586210,97743,774383,116033,849575,351428,179912,248733,720629,387335,516346,78908,963590,126729,530006,119516,904732,872385,726170,371713,266511,941337,144784,170230,897156,556536,937236,450209,10622,146925,112306,79170,257742,588405,277037,75080,397990,319017,588189,312272,275596,914503,24560,232123,749994,103323,540497,328426,813377,619337,77285,628440,61694,769786,228722,496533,458917,18145,404250,304283,4970,954985,162054,896911,708063,749693,319262,575381,181830,509555,367092,439376,910664,142357,161280,576629,270591,938062,969926,937163,563374,433454,515796,696513,480400,192154,233191,483947,104589,565459,19693,859611,36425,331873,526182,64152,83543,903643,174564,394622,768268,935510,278065,286961,430317,102568,422031,661823,169802,166936,71962,191570,167844,629796,371018,835470,357193,226669,889301,669133,554305,724547,920433,343476,211105,92105,162871,578162,287036,360261,969507,326037,559737,674950,755384,741832,362240,382386,253824,23149,343246,514667,745521,563283,5609,955294,916908,177876,794733,616936,746526,424270,54788,34724,552713,15612,151988,906965,978205,61232,140818,407917,969408,690543,785044,62392,17709,460329,584772,747112,467704,5395,543313
        };
        //bs.printIntArr(arr);
        long begin = System.currentTimeMillis();
        bs.bubbleSort(arr);
        long end = System.currentTimeMillis();
        System.out.println("用时:" + (end - begin));
        bs.printIntArr(arr);
    }

    /**
     * 冒泡排序,
     * 排序规则是从小到大
     * @param tarArray 要排序的数组
     */
    public void bubbleSort(int[] tarArray) {
        if (tarArray.length < 2) {
            return;
        }
        //length个元素,要循环length-1次 例如:10个元素,要循环9次
        for (int i = 0; i < tarArray.length - 1; i++) {
            //每次循环要比较的次数,
            for (int j = 0; j < tarArray.length - 1 - i; j++) {
                //将大的值交换到后面
                if (tarArray[j] > tarArray[j + 1]) {
                    swap(tarArray, j, j+1);
                }
            }
        }
    }

    /**
     * 交换数组中的两个元素的位置,索引从0开始
     * @param arr 要进行操作的数组
     * @param pos1 数组中的索引1
     * @param pos2 数组中的索引2
     */
    private void swap(int[] arr, int pos1, int pos2) {
        int temp = arr[pos1];
        arr[pos1] = arr[pos2];
        arr[pos2] = temp;
    }

    /**
     * 打印数组中的所有元素
     * @param arr 要进行操作的数组
     */
    public void printIntArr(int[] arr) {
        for (int i = 0; i < arr.length; i++) {
            System.out.print(arr[i] + ",");
        }
        System.out.println();
    }

}


运行结果:

------------------------------------------------
如有错误或者不当之处,还请及时评论留言。多谢。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值