• 排序算法


    package org.apache.ibatis.binding;
    
    import com.alibaba.fastjson.JSON;
    import org.junit.After;
    import org.junit.Test;
    
    import java.util.Scanner;
    
    /**
     * @description:
     * @author: AlbertXe
     * @create: 2021-08-07 12:10
     */
    public class A {
        public static void main(String[] args) {
            Scanner s = new Scanner(System.in);
            String line = s.nextLine();
            String r = line.replaceAll("([0-9]+)", "*$1*");
            System.out.println(r);
    
        }
        int[] arr = {7, 5, 8, 9, 3, 7};
        @After
        public void after() {
            System.out.println(JSON.toJSONString(arr));
        }
    
        @Test
        public void bubbleSort() {
    //        bubbleSort(arr);
            bubbleSort2(arr);
        }
    
        @Test
        public void directInsert() {
            directInsert(arr);
        }
    
        @Test
        public void select() {
            select(arr);
        }
    
        @Test
        public void shellSort() {
            shellSort(arr);
        }
    
        /**
         * 希尔排序               1234  步长2  13 24
         * 最后一步是插入排序
         * @param arr
         */
        private void shellSort(int[] arr) {
            int l = arr.length;
            int h = 1;
            while (h < l / 3) {
                h = 3 * h + 1; // 1 ,4 ,7
            }
            while (h >= 1) {
                for (int i = h; i < l; i++) {// 8 9 10  多组 每组中最后一个数据
                    int temp = arr[i];
                    int j;
                    for (j = i; j >=h && temp<arr[j-h]; j-=h) {
                        arr[j] = arr[j - h]; //前面数据后移 也会空出一个空格
                    }
                    arr[j] = temp; // k空出插入数据
                }
                h /= 3;
            }
    
        }
    
        /**
         * 直接选择排序  每轮最小放对应的起始位置
       * @param arr
         */
        private void select(int[] arr) {
            int l = arr.length;
            for (int i = 0; i < l-1; i++) {
                int tem = i; // 存最小元素下标
                for (int j = i+1; j < l; j++) {
                    if (arr[tem] > arr[j]) {
                        tem = j;
                    }
                }
                if (tem != i) {
                    swap(arr, i, tem);
                }
            }
        }
    
        /**
         * 直接插入排序
         * @param arr
         */
        private void directInsert(int[] arr) {
            for (int i = 0; i < arr.length; i++) {
                int j = i;
                while (j > 0 && arr[j] < arr[j - 1]) { // 1个数有序
                    swap(arr, j, j - 1);
                    j--;
                }
            }
        }
    
        /**
         * 冒泡  比较左右
         * @param arr
         */
        private void bubbleSort(int[] arr) {
            int l = arr.length;
            for (int i = 0; i < l-1; i++) {// 8个数 排前个
                for (int j = 0; j < l-1-i; j++) {
                    if (arr[j] > arr[j + 1]) {
                        swap(arr, j, j + 1);
                    }
                }
            }
        }
    
        /**
         * 冒泡优化2
         * @param arr
         */
        private void bubbleSort2(int[] arr) {
            int l = arr.length;
            for (int i = 0; i < l-1; i++) {
                boolean flag = true; //表示循环未交换
                for (int j = 0; j < l-1-i; j++) {
                    if (arr[j] > arr[j + 1]) {
                        swap(arr, j, j + 1);
                        flag = false;
                    }
    
                }
                if (flag) {
                    break;
                }
            }
        }
    
        private void swap(int[] arr, int i, int j) {
            int temp = arr[i];
            arr[i] = arr[j];
            arr[j] = temp;
        }
    }
    
    
  • 相关阅读:
    GJM : Unity的profiler性能分析【转载】
    GJM :Unity UI 之 UGUI 开发
    GJM:笔记记录
    GJM : UGUI 缩放循环拖动展示卡牌效果
    GJM :HTC Vive VRTK.Package 踩坑之路(一)
    GJM :Mono 介绍
    GJM :Unity3D 介绍
    GJM : C# 介绍
    GJM : 通用类型系统(Common Type System CTS)
    GJM :SQL Server中常用的SQL语句
  • 原文地址:https://www.cnblogs.com/albertXe/p/15120656.html
Copyright © 2020-2023  润新知