zoukankan      html  css  js  c++  java
  • 冒泡排序原理(BubbleSorted)

    //简单的冒泡排序
    public class BubbleMath {
    public static void main(String[] args) {
    //分清楚数组的头和尾,开始为尾,最后为头,因为头可以生长,
    //在最后添加数据,所以最后为头,就像池塘里的水泡上升变大一样
    //水泡在水底因为水压大所以气泡小,在逐渐上升的过程中,外面水压变小
    //气泡变大,接触水面时最大,故此称为冒泡排序
    int[] array={12,24,9,74,23,15,67};
    sorted(array);
    print(array);

    }

    private static void sorted(int[] array){
    //1、比较相邻元素
    //2、每次将最大值移动到数组头部(数组的最后)
    for(int i=0;i<array.length-1;i++){
    for(int j=0;j<array.length-i-1;j++){

      //此处多加注意,内部循环只用一个变量 j 控制
    if(array[j]>array[j+1]){

    int temp=array[j+1];
    array[j+1]=array[j];
    array[j]=temp;
    }
    }
    }
    }

    private static void print(int[] array){
    for(int i=0;i<array.length;i++){
    System.out.print(array[i]+" ");
    }
    }
    }

  • 相关阅读:
    C# 委托/Func() 中 GetInvocationList() 方法的使用 | 接收委托多个返回值
    蒋廷黻著《中国近代史》-中国近代屈辱史读后感
    ASP.NET Core 上传多文件 超简单教程
    Python
    Python
    Python
    Python
    Python
    Python
    Python
  • 原文地址:https://www.cnblogs.com/TankRuning/p/3672564.html
Copyright © 2011-2022 走看看