zoukankan      html  css  js  c++  java
  • 数组系列教材 (三)- Java 数组选择法,冒泡法排序

    步骤1:选择法排序

    步骤2:冒泡法排序
    步骤3:练习-排序
    步骤4:答案-排序
    步骤 1 : 选择法排序
    选择法排序的思路: 
    把第一位和其他所有的进行比较,只要比第一位小的,就换到第一个位置来 
    比较完后,第一位就是最小的 
    然后再从第二位和剩余的其他所有进行比较,只要比第二位小,就换到第二个位置来 
    比较完后,第二位就是第二小的 
    以此类推
    选择法排序
    public class HelloWorld {
        public static void main(String[] args) {
            int a [] = new int[]{18,62,68,82,65,9};
            //排序前,先把内容打印出来
            for (int i = 0; i < a.length; i++) {
                System.out.print(a[i] + " ");
            }
            System.out.println(" ");
            //选择法排序
         
            //第一步: 把第一位和其他所有位进行比较
            //如果发现其他位置的数据比第一位小,就进行交换
             
            for (int i = 1; i < a.length; i++) {
                if(a[i]<a[0]){   
                    int temp = a[0];
                    a[0] = a[i];
                    a[i] = temp;
                }
            }
            //把内容打印出来
            //可以发现,最小的一个数,到了最前面
            for (int i = 0; i < a.length; i++) {
                System.out.print(a[i] + " ");
            }
            System.out.println(" ");
             
            //第二步: 把第二位的和剩下的所有位进行比较
            for (int i = 2; i < a.length; i++) {
                if(a[i]<a[1]){   
                    int temp = a[1];
                    a[1] = a[i];
                    a[i] = temp;
                }
            }
            //把内容打印出来
            //可以发现,倒数第二小的数,到了第二个位置
            for (int i = 0; i < a.length; i++) {
                System.out.print(a[i] + " ");
            }
            System.out.println(" ");        
             
            //可以发现一个规律
            //移动的位置是从0 逐渐增加的
            //所以可以在外面套一层循环
             
            for (int j = 0; j < a.length-1; j++) {
                for (int i = j+1; i < a.length; i++) {
                    if(a[i]<a[j]){   
                        int temp = a[j];
                        a[j] = a[i];
                        a[i] = temp;
                    }
                }
            }
             
            //把内容打印出来
            for (int i = 0; i < a.length; i++) {
                System.out.print(a[i] + " ");
            }
            System.out.println(" ");        
        }
    }
    步骤 2 : 冒泡法排序
    冒泡法排序的思路: 
    第一步:从第一位开始,把相邻两位进行比较 
    如果发现前面的比后面的大,就把大的数据交换在后面,循环比较完毕后,最后一位就是最大的 
    第二步: 再来一次,只不过不用比较最后一位 
    以此类推
    冒泡法排序
    public class HelloWorld {
        public static void main(String[] args) {
            int a [] = new int[]{18,62,68,82,65,9};
            //排序前,先把内容打印出来
            for (int i = 0; i < a.length; i++) {
                System.out.print(a[i] + " ");
            }
            System.out.println(" ");
            //冒泡法排序
          
            //第一步:从第一位开始,把相邻两位进行比较
            //如果发现前面的比后面的大,就把大的数据交换在后面
              
            for (int i = 0; i < a.length-1; i++) {
                if(a[i]>a[i+1]){   
                    int temp = a[i];
                    a[i] = a[i+1];
                    a[i+1] = temp;
                }
            }
            //把内容打印出来
            //可以发现,最大的到了最后面
            for (int i = 0; i < a.length; i++) {
                System.out.print(a[i] + " ");
            }
            System.out.println(" ");
              
            //第二步: 再来一次,只不过不用比较最后一位
            for (int i = 0; i < a.length-2; i++) {
                if(a[i]>a[i+1]){   
                    int temp = a[i];
                    a[i] = a[i+1];
                    a[i+1] = temp;
                }
            }
            //把内容打印出来
            //可以发现,倒数第二大的到了倒数第二个位置
            for (int i = 0; i < a.length; i++) {
                System.out.print(a[i] + " ");
            }
            System.out.println(" ");        
              
            //可以发现一个规律
            //后边界在收缩
            //所以可以在外面套一层循环
              
            for (int j = 0; j < a.length; j++) {
                for (int i = 0; i < a.length-j-1; i++) {
                    if(a[i]>a[i+1]){   
                        int temp = a[i];
                        a[i] = a[i+1];
                        a[i+1] = temp;
                    }
                }
            }
              
            //把内容打印出来
            for (int i = 0; i < a.length; i++) {
                System.out.print(a[i] + " ");
            }
            System.out.println(" ");        
        }
    }

    更多内容,点击了解: https://how2j.cn?p=61135
  • 相关阅读:
    python3 str.encode bytes.decode
    GTX 1080显卡出错
    Android studio2.2 app:transformNative_libsWithStripDebugSymbolForDebug
    psmisc
    How to configure locales to Unicode in a Docker Ubuntu 14.04 container?
    keras中文文档笔记1——概述
    keras中的一些小tips(一)
    基于Theano的深度学习(Deep Learning)框架Keras学习随笔-01-FAQ
    Android studio 更新android SDK
    groovy基础
  • 原文地址:https://www.cnblogs.com/Lanht/p/12441159.html
Copyright © 2011-2022 走看看