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);

     

        }

     

    }

  • 相关阅读:
    PPT幻灯片放映不显示备注,只让备注显示在自己屏幕上
    Available Date 相关
    App Store常用推广方法
    iPhone应用提交流程:如何将App程序发布到App Store-转
    [转]关于适配iphone5,Invalid Launch Image的退信
    NSLog 不打印中文 - 解决
    Xcode GDB 调试
    xcode找不到真机设备 - 转
    使用静态库的一些问题 -all_load
    [深入浅出iOS库]之图形库CorePlot
  • 原文地址:https://www.cnblogs.com/jinzhengquan/p/1980220.html
Copyright © 2011-2022 走看看