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

    /**

     * @author Administrator

     * 冒泡排序的算法

     * 比较n轮,每一轮都把最大元素移动到数组后端

     */

    public class Maopaosort {

        private static int count = 0;

     

        public static void Sort(int[] a) {

           int temp = 0;

           int i = 0, j = 0;

           for (i = 1; i < a.length; i++) {

               //找到最大值

               for (j = 0; j < a.length - i; j++) {

     

                  if (a[j] > a[j + 1]) { // 这里的“>”号决定了最后是升序,“<”则是降序

                      temp = a[j + 1];// temp仅做暂存单元

                      a[j + 1] = a[j];

                      a[j] = temp;

                  }

     

               }

               printAll(a);

           }

           printAll(a);// 打印结果

     

        }

     

        public static void printAll(int[] a) {

           System.out.println("第" + (++count) + "次:");

           for (int i = 0; i < a.length; i++) {

     

               System.out.print(a[i] + " ");

           }

           System.out.println();

        }

     

        public static void main(String[] args) {

           // 定义一个整型的数组

           int Data[] = new int[10];

           for (int i = 0; i < Data.length; i++) {

     

               Data[i] = (int) (Math.random() * 100);// 取得的数据在0到100之间不包括0

     

           }

           System.out.println("排序之前:");

           for (int i : Data) {

               System.out.print(i + " ");

           }

           System.out.println();

           System.out.println("排序之后:");

           Sort(Data);

     

        }

     

    }

  • 相关阅读:
    br_
    -fpic -fPIC -fpie -fPIE
    (OK) cross_compile_readline_for_android-x86_64-6.0
    Installing the GNU Readline library
    How to get libreadline for Android?
    (OK)(OK) seem-tools-init-android-x86_64-6.0-rc1-0.sh
    (OK)(OK) seem-tools-CLI-semi-auto.sh---nic1_2-hostonly-bridged
    (OK)(OK) seem-tools-auto_create_vm_android.sh
    (OK) run my script at boot time in android-x86_64 (Chih-Wei Huang)
    【习题 3-4 UVA
  • 原文地址:https://www.cnblogs.com/jinzhengquan/p/1980220.html
Copyright © 2011-2022 走看看