- public class demo29 {
- public static void main(String[] args) {
- List<Integer> al = sortArray(getRandom());
- for (Integer integer : al) {
- System.out.println(integer);}
- }
- public static List<Integer> getRandom() { //获取随机数存入集合
- ArrayList<Integer> al = new ArrayList<Integer>();
- Random r = new Random();
- for(int i=0;i<5;i++){
- al.add(r.nextInt(10)+1);
- }
- return al;
- }
- public static List<Integer> sortByBubble(List<Integer> list){ //冒泡排序
- Integer[] arr= list.toArray(new Integer[list.size()]);
- int temp;
- for(int i=0;i<arr.length-1;i++){
- for(int j=0;j<arr.length-i-1;j++){
- if(arr[j]>arr[j+1]){
- temp = arr[j];
- arr[j] = arr[j+1];
- arr[j+1] = temp;}
- }
- }
- return Arrays.asList(arr);
- }
- }
复制代码 |
|