首页 > 编程语言 > 详细

数组排序

时间:2021-07-22 23:27:04      阅读:16      评论:0      收藏:0      [点我收藏+]

冒泡排序

package com.prh.sort;

import java.util.Arrays;

public class BubbleSort {
    public static void main(String[] args) {
        int[] a = {6,5,4,68,1,9,123,44,7};
        //创建新数组来接收排序好的数组
        int[] sort = Sort(a);
        System.out.println(Arrays.toString(sort));

    }
    //冒泡排序
    public static int[] Sort(int[] array){
        //用于判断需要循环次数
        for (int i = 0; i < array.length-1; i++) {
            //用于排序
           for (int j = 0;j < array.length-1; j++){
            //如果前面数组大于后面数组则互相替换
            if (array[j]>array[j+1]){
                   int mm = array[j+1];
                   array[j+1] = array[j];
                   array[j] = mm;
                   
               }
           }


        }
        return array;


    }

}

 

数组排序

原文:https://www.cnblogs.com/prh19991101/p/15046414.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!