JAVASE基础模块十六(冒泡排序 快速排序 直接插入排序 选择排序)

JAVASE基础模块十六(冒泡排序 快速排序 直接插入排序 选择排序)

冒泡排序

  • public class Paix {
        public static void main(String[] args) {
            //数组排序 把数组中的无序元素 通过交换移动等方式 使数组中的元素 成为一个有序序列
            int[] a = {5, 99, 51, 231, 15, 48, 23, 66};
    
            //冒泡排序
            //数组中的元素两两比较 大的元素往后放 经过一轮比较后 最大的元素出现在最后面
            ShuZuPaiXu(a);
        }
    
        private static void ShuZuPaiXu(int[] a) {
            int m;
            for (int j = a.length - 2; j >= 0; j--) {
                System.out.print("第" + (j + 1) + "轮过程为:");
                System.out.println();
                for (int i = 0; i < a.length - 1; i++) {
                    if (a[i] > a[i + 1]) {
                        m = a[i + 1];
                        a[i + 1] = a[i];
                        a[i] = m;
                    }
                    System.out.print("循环比较过程为:");
                    for (int i1 = 0; i1 < a.length; i1++) {
                        System.out.print(a[i1] + "\t");
                    }
                    System.out.println();
                }
    
            }
            System.out.println("最终结果为:");
            for (int i1 = 0; i1 < a.length; i1++) {
                System.out.print(a[i1] + "\t");
            }
    
        }
    
    }
    运行结果:
    第7轮过程为:
    循环比较过程为:5	99	51	231	15	48	23	66	
    循环比较过程为:5	51	99	231	15	48	23	66	
    循环比较过程为:5	51	99	231	15	48	23	66	
    循环比较过程为:5	51	99	15	231	48	23	66	
    循环比较过程为:5	51	99	15	48	231	23	66	
    循环比较过程为:5	51	99	15	48	23	231	66	
    循环比较过程为:5	51	99	15	48	23	66	231	
    第6轮过程为:
    循环比较过程为:5	51	99	15	48	23	66	231	
    循环比较过程为:5	51	99	15	48	23	66	231	
    循环比较过程为:5	51	15	99	48	23	66	231	
    循环比较过程为:5	51	15	48	99	23	66	231	
    循环比较过程为:5	51	15	48	23	99	66	231	
    循环比较过程为:5	51	15	48	23	66	99	231	
    循环比较过程为:5	51	15	48	23	66	99	231	
    第5轮过程为:
    循环比较过程为:5	51	15	48	23	66	99	231	
    循环比较过程为:5	15	51	48	23	66	99	231	
    循环比较过程为:5	15	48	51	23	66	99	231	
    循环比较过程为:5	15	48	23	51	66	99	231	
    循环比较过程为:5	15	48	23	51	66	99	231	
    循环比较过程为:5	15	48	23	51	66	99	231	
    循环比较过程为:5	15	48	23	51	66	99	231	
    第4轮过程为:
    循环比较过程为:5	15	48	23	51	66	99	231	
    循环比较过程为:5	15	48	23	51	66	99	231	
    循环比较过程为:5	15	23	48	51	66	99	231	
    循环比较过程为:5	15	23	48	51	66	99	231	
    循环比较过程为:5	15	23	48	51	66	99	231	
    循环比较过程为:5	15	23	48	51	66	99	231	
    循环比较过程为:5	15	23	48	51	66	99	231	
    第3轮过程为:
    循环比较过程为:5	15	23	48	51	66	99	231	
    循环比较过程为:5	15	23	48	51	66	99	231	
    循环比较过程为:5	15	23	48	51	66	99	231	
    循环比较过程为:5	15	23	48	51	66	99	231	
    循环比较过程为:5	15	23	48	51	66	99	231	
    循环比较过程为:5	15	23	48	51	66	99	231	
    循环比较过程为:5	15	23	48	51	66	99	231	
    第2轮过程为:
    循环比较过程为:5	15	23	48	51	66	99	231	
    循环比较过程为:5	15	23	48	51	66	99	231	
    循环比较过程为:5	15	23	48	51	66	99	231	
    循环比较过程为:5	15	23	48	51	66	99	231	
    循环比较过程为:5	15	23	48	51	66	99	231	
    循环比较过程为:5	15	23	48	51	66	99	231	
    循环比较过程为:5	15	23	48	51	66	99	231	
    第1轮过程为:
    循环比较过程为:5	15	23	48	51	66	99	231	
    循环比较过程为:5	15	23	48	51	66	99	231	
    循环比较过程为:5	15	23	48	51	66	99	231	
    循环比较过程为:5	15	23	48	51	66	99	231	
    循环比较过程为:5	15	23	48	51	66	99	231	
    循环比较过程为:5	15	23	48	51	66	99	231	
    循环比较过程为:5	15	23	48	51	66	99	231	
    最终结果为:
    5	15	23	48	51	66	99	231	
    进程已结束,退出代码0
    

选择排序

  • public class XuanZe {
        public static void main(String[] args) {
            //选择排序
            //每次拿一个元素 跟后面的元素挨个比较 最大的或者最小的往前放 然后一直循环
            int[] a = {5, 99, 51, 21, 15, 48, 23, 66};
            XuanZePaiXu(a);
        }
    
        private static void XuanZePaiXu(int[] a) {
            for (int j = a.length - 2; j >= 0; j--) {
                System.out.print("第" + (j + 1) + "轮过程为:");
                System.out.println();
                int max = a[a.length - 2 - j];
                int n = a.length - 2 - j;
                for (int i = a.length - 2 - j; i < a.length; i++) {
                    if (a[i] > max) {
                        max = a[i];
                        n = i;
                    }
                }
                a[n] = a[a.length - 2 - j];
                a[a.length - 2 - j] = max;
    
    
                System.out.println("当前最大值为:" + max);
                System.out.println("当前最大值元素下标为:" + n);
                for (int i1 = 0; i1 < a.length; i1++) {
                    System.out.print(a[i1] + "\t");
                }
                System.out.println();
            }
            System.out.println();
            System.out.println("最终结果为:");
            for (int i11 = 0; i11 < a.length; i11++) {
                System.out.print(a[i11] + "\t");
            }
        }
    }
    运行结果:
    第7轮过程为:
    当前最大值为:99
    当前最大值元素下标为:1
    99	5	51	21	15	48	23	66	
    第6轮过程为:
    当前最大值为:66
    当前最大值元素下标为:7
    99	66	51	21	15	48	23	5	
    第5轮过程为:
    当前最大值为:51
    当前最大值元素下标为:2
    99	66	51	21	15	48	23	5	
    第4轮过程为:
    当前最大值为:48
    当前最大值元素下标为:5
    99	66	51	48	15	21	23	5	
    第3轮过程为:
    当前最大值为:23
    当前最大值元素下标为:6
    99	66	51	48	23	21	15	5	
    第2轮过程为:
    当前最大值为:21
    当前最大值元素下标为:5
    99	66	51	48	23	21	15	5	
    第1轮过程为:
    当前最大值为:15
    当前最大值元素下标为:6
    99	66	51	48	23	21	15	5	
    
    最终结果为:
    99	66	51	48	23	21	15	5	
    进程已结束,退出代码0
    

直接插入排序

  • public class ZhiJieChaRu {
        public static void main(String[] args) {
            int[] a = {5, 99, 51, 231, 15, 48, 23, 66};
            //直接插入排序 从第二个元素 每次拿一个元素插入之前的序列 使之保持有序
    
            ZhiJieChaRu(a);
        }
    
        private static void ZhiJieChaRu(int[] a) {
            int m;
            int n = 1;
            for (int i = 1; i < a.length; i++) {
                int j = i;
                j--;
                while (a[i] < a[i - 1]) {
                    m = a[i - 1];
                    a[i - 1] = a[i];
                    a[i] = m;
                    i--;
                    System.out.print(n + "循环比较过程为:");
                    n++;
                    for (int i1 = 0; i1 < a.length; i1++) {
                        System.out.print(a[i1] + "\t");
                    }
                    System.out.println();
                }
            }
            System.out.println("最终结果为:");
            for (int i1 = 0; i1 < a.length; i1++) {
                System.out.print(a[i1] + "\t");
            }
    
        }
    }
    运行结果:
    1:循环比较过程为:5	51	99	231	15	48	23	66	
    2:循环比较过程为:5	51	99	15	231	48	23	66	
    3:循环比较过程为:5	51	15	99	231	48	23	66	
    4:循环比较过程为:5	15	51	99	231	48	23	66	
    5:循环比较过程为:5	15	51	99	48	231	23	66	
    6:循环比较过程为:5	15	51	48	99	231	23	66	
    7:循环比较过程为:5	15	48	51	99	231	23	66	
    8:循环比较过程为:5	15	48	51	99	23	231	66	
    9:循环比较过程为:5	15	48	51	23	99	231	66	
    10:循环比较过程为:5 15	48	23	51	99	231	66	
    11:循环比较过程为:5 15	23	48	51	99	231	66	
    12:循环比较过程为:5 15	23	48	51	99	66	231	
    13:循环比较过程为:5 15	23	48	51	66	99	231	
    最终结果为:
    5	15	23	48	51	66	99	231	
    进程已结束,退出代码0
    
    

快速排序

  • import java.util.Arrays;
    
    public class KuaiPai {
        public static void main(String[] args) {
            int[] a = {59, 99, 51, 231, 15, 48, 23, 66};
            KuaiSuPaiXu(a, 0, a.length - 1);
            System.out.println("最终结果:" + Arrays.toString(a));
        }
    
        private static void KuaiSuPaiXu(int[] a, int start, int end) {
            if (start < end) {
                //获取左右两区分隔位置索引
                int index = getIndex(a, start, end);
                KuaiSuPaiXu(a, start, index - 1);
                KuaiSuPaiXu(a, index + 1, end);
            }
        }
    
        private static int getIndex(int[] a, int start, int end) {
            int i = start;
            int j = end;
            //定义基准数
            int x = a[i];
            while (i < j) {//由后向前找比它小的
                while (i < j && a[j] >= x) {
                    j--;
                }
                if (i < j) {
                    a[i] = a[j];
                    i++;
                    System.out.print("--循环过程:");
                    for (int i1 = 0; i1 < a.length; i1++) {
                        System.out.print(a[i1] + "\t");
                    }
                    System.out.println();
                }
    
                //由前向后找比他大的
                while (i < j && a[i] < x) {
                    i++;
                }
                if (i < j) {
                    a[j] = a[i];
                    j--;
    
                    System.out.print("--循环过程:");
                    for (int i1 = 0; i1 < a.length; i1++) {
                        System.out.print(a[i1] + "\t");
                    }
                    System.out.println();
                }
            }
            a[j] = x;
            return j;
        }
    }
    运行结果:
    --循环过程:23	99	51	231	15	48	23	66	
    --循环过程:23	99	51	231	15	48	99	66	
    --循环过程:23	48	51	231	15	48	99	66	
    --循环过程:23	48	51	231	15	231	99	66	
    --循环过程:23	48	51	15	15	231	99	66	
    --循环过程:15	48	51	15	59	231	99	66	
    --循环过程:15	48	51	48	59	231	99	66	
    --循环过程:15	23	48	48	59	231	99	66	
    --循环过程:15	23	48	51	59	66	99	66	
    最终结果:[15, 23, 48, 51, 59, 66, 99, 231]
    
    进程已结束,退出代码0
    

    待续…

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值