zoukankan      html  css  js  c++  java
  • 冒泡排序算法

    package com.cn.gao;
    //冒泡排序
    public class BubbleSort {
        public static final int SIZE=10;
        //冒泡排序算法
        public static void bubbleSort(int[] a){
            int temp;
            for(int i=1;i<a.length;i++){
                for(int j=0;j<a.length-i;j++){
                    if(a[j]>a[j+1]){
                        temp = a[j];
                        a[j] = a[j+1];
                        a[j+1] = temp;
                    }
                }
                //输出每步排序后的结果
                System.out.print("第"+i+"步的排序结果为:");
                for(int j=0;j<a.length;j++){
                    System.out.print(" "+a[j]);
                }
                System.out.println();
            }
        }
        
        public static void main(String[] args) {
            int[] a = new int[SIZE];
            //为数组元素赋值
            for(int i=0;i<a.length;i++){
                a[i] = (int) (100 + Math.random()*100);
            }
            //输出排序前的数组元素
            System.out.println("排序前的数组为:");
            for(int i=0;i<a.length;i++){
                System.out.print(a[i]+" ");
            }
            System.out.print("
    ");
            //对数组排序
            bubbleSort(a);
            System.out.println("排序后的数组为:");
            for(int i=0;i<a.length;i++){
                System.out.print(a[i]+" ");
            }
            System.out.print("
    ");
        }
    
    }
  • 相关阅读:
    各种
    shell
    搭建个人信息平台
    基本tomcat+nginx
    vi编辑的使用
    linux权限管理
    Java观察者模式
    Flume+Kafka+Sparkstreaming日志分析
    科学计算与数学建模
    推荐系统起手式-几种简单推荐模型(基于内容的推荐)
  • 原文地址:https://www.cnblogs.com/gaopeng527/p/4480074.html
Copyright © 2011-2022 走看看