• 数组的经典排序


    1.选择排序原理
        a.将数组中每个元素与第一个元素比较,如果这个元素小于第一个元素,则交换这俩个元素的位置
        b.循环第一条规则,找出最小元素,防于第一个位置
        c.经过N-1轮比较完成排序

    
    package paixu;
    
    /**
     * 选择排序
     */
    import java.util.Scanner;
    
    public class zy2 {
        public static void main(String[] args) {
            Scanner input = new Scanner(System.in);
            System.out.print("请输入输入数字的位数:");
            int c = input.nextInt();
            int[] a = new int[c];
            for (int i = 0; i < a.length; i++) {
                System.out.print("请输入输第" + (i + 1) + "位数字:");
                a[i] = input.nextInt();
            }
            int temp = 0;
            for (int i = 0; i < a.length; i++) {
                for (int j = i + 1; j < a.length; j++) {
                    if (a[i] > a[j]) {
                        temp = a[i];
                        a[i] = a[j];
                        a[j] = temp;
                    }
                }
            }
            for (int i = 0; i < a.length; i++)
                System.out.print(a[i] + "  ");
        }
    }
    
    复制代码

     2.冒泡排序原理
       a.逐一比较数组中相邻的两个元素,如果后面的元素小于前面的元素就互换
       b.经过一轮比较,一定有一个最大的排在最后的位置
       c.每一次比较剩下的元素,经过N-1次比较可以实现排序

    package paixu;
    
    /**
     * 冒泡排序
     */
    
    public class zy1 {
        public static void main(String[] args) {
            int[] sz = { 8, 6, 5, 4, 3, 1, 2 };
            int bl = 0;
            for (int i = 0; i < sz.length - 1; i++) {
                for (int j = 0; j < sz.length - 1 - i; j++) {
                    if (sz[j] > sz[j + 1]) {
                        bl = sz[j];
                        sz[j] = sz[j + 1];
                        sz[j + 1] = bl;
                    }
                }
            }
            for (int i = 0; i < sz.length; i++) {
                System.out.print(sz[i] + "	");
            }
        }
    
    }
    

     3.插入排序原理
       a.将数组分为两部分,将后部分的第一张逐一与前部分每一张比较,如果当前元素小,就移动被比较的元素
       b.找到合理位置插入

    package paixu;
    
    /**
     * 插入排序
     */
    public class zy3 {
        public static void main(String[] args) {
            int[] sz = { 1, 9, 5, 2, 7, 4 };
            for (int i = 1; i < sz.length; i++) {
                int temp = sz[i];
                int j;
                for (j = i - 1; j >= 0; j--) {
                    if (temp < sz[j]) {
                        sz[j + 1] = sz[j];// 元素向后移动
                    } else {
                        break;
                    }
                }
                sz[j + 1] = temp;// 找到合适的位值插入元素
            }
            for (int i = 0; i < sz.length; i++) {
                System.out.print(sz[i] + "   ");
            }
        }
    }
  • 相关阅读:
    单点登录场景中的CAS协议和OAuth2.0协议对比
    https的URL参数传递中文乱码问题
    Goby
    Burp_suite安装及使用教程(专业版)
    IIS下配置php运行环境。
    iis强制使用https
    IIS-详解IIS中URL重写工具的规则条件(Rule conditions)
    树莓派鼓捣记
    树莓派鼓捣记
    WSL1 升级为 WSL2
  • 原文地址:https://www.cnblogs.com/fangtao1997/p/6986136.html
Copyright © 2020-2023  润新知