list集合排序

package com.wy.test;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

public class NewSort {
    public static void main(String[] args) {
        //创建list集合
        List<Integer> list = new ArrayList();

        //随机赋值
        for(int i=0; i<100; i++){
            Random random = new Random();
            int number = random.nextInt(1000);//最大值
            list.add(number);
        }
        System.out.println(list);

        //从小到大排序,
        for(int k=0; k<list.size()-1; k++){
            for(int j=0; j<list.size()-1; j++){

                //相邻两数相互比较
                if(list.get(j)<list.get(j+1)){

                    //取中间最小
                    int min = list.get(j);
                    list.set(j,list.get(j+1));
                    list.set(j+1,min);
                }
            }
            //内循环每执行一次外循环减少执行一次循环
            k++;
        }
        System.out.println(list);
    }
}

运行结果

[294, 824, 407, 366, 991, 905, 97, 329, 306, 935, 958, 79, 335, 568, 113, 540, 996, 735, 59, 598, 961, 683, 54, 927, 247, 678, 691, 408, 403, 470, 45, 804, 80, 862, 51, 591, 373, 447, 458, 150, 79, 412, 521, 632, 251, 212, 963, 700, 115, 41, 859, 582, 773, 0, 394, 676, 53, 288, 595, 543, 848, 59, 991, 8, 304, 764, 969, 210, 467, 685, 99, 40, 340, 792, 128, 94, 904, 318, 14, 813, 468, 155, 317, 943, 148, 806, 463, 467, 418, 830, 516, 908, 742, 633, 915, 212, 308, 69, 721, 64]
[996, 991, 963, 961, 958, 935, 927, 905, 862, 859, 848, 824, 991, 804, 773, 764, 969, 735, 700, 691, 685, 683, 678, 792, 676, 632, 904, 598, 595, 813, 591, 582, 568, 943, 543, 806, 540, 521, 470, 830, 516, 908, 742, 633, 915, 468, 467, 467, 721, 463, 458, 447, 418, 412, 408, 407, 403, 394, 373, 366, 340, 335, 329, 318, 317, 308, 306, 304, 294, 288, 251, 247, 212, 212, 210, 155, 150, 148, 128, 115, 113, 99, 97, 94, 80, 79, 79, 69, 64, 59, 59, 54, 53, 51, 45, 41, 40, 14, 8, 0]

 

posted on 2018-10-24 00:15  MasonWan  阅读(164)  评论(1编辑  收藏  举报

导航