19

结构与算法(04):排序规则与查找算法

 3 years ago
source link: https://segmentfault.com/a/1190000024529491
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

本文源码: GitHub·点这里 || GitEE·点这里

一、递归算法

递归就是方法自己调用自己,每次调用时传入不同的变量,可以让代码变得简洁。递归算法在计算机科学中是指一种通过重复将问题分解为同类的子问题而解决问题的方法,递归式方法可以被用于解决很多的计算机科学问题,因此它是计算机科学中十分重要的一个概念。

基础案例:通过递归打印数据;

public class M01_Recursion {
    public static void main(String[] args) {
        printNum(3);
    }
    private static void printNum (int num){
        if (num > 1){
            printNum(num-1);
        }
        System.out.println("num="+num);
    }
}

递归图解:

zaErMjj.png!mobile

基于栈结构的特点,递归调用会形成如上的结构,当所有递归方法入栈成功后,在依次执行出栈动作,打印数据结果。

在实际开发中递归经常用来接近树结构问题,阶乘算法,排序查找等数学类问题。

递归算法的条件必须要不断接近退出条件,不然很容易出现无限循环导致内存溢出异常问题。

二、排序算法

排序算法就是使一组数据记录,按照特定的排序策略,递增或递减的排列起来的操作;常用的排序算法:冒泡排序,选择排序,插入排序,希尔排序,归并排序,快速排序,基数排序等;排序算法选择:不同的排序业务可以通过多种算法测试,复杂度低,耗时短的优先使用。

1、冒泡排序

通过对排序序列依次比较相邻元素的值,若发现逆序则交换,使值较大的元素逐渐从前移向后部,算法的名字由来是因为越小的元素会经由排序交换慢慢浮到数列的一端,就如同碳酸饮料中二氧化碳的气泡最终会上浮到顶端一样,故名冒泡排序。

public class M02_Bubble {
    public static void main(String[] args) {
        int[] arr = {3,7,5,9,6};
        bubbleSort(arr);
        for (int num:arr){
            System.out.println(num);
        }
    }
    public static void bubbleSort(int[] arr) {
        // 声明临时变量
        int temp = 0;
        // 排序总趟数
        for (int i = 0; i < arr.length - 1; i++) {
            // 元素交换
            for (int j = 0; j < arr.length - 1 - i; j++) {
                if (arr[j] > arr[j + 1]) {
                    // 位置交换
                    temp = arr[j];
                    arr[j] = arr[j + 1];
                    arr[j + 1] = temp;
                }
            }
        }
    }
}

核心思路:

排序趟数只有多少元素,理论上要进行处理的次数;每个元素的位置交换都需要一次完整对比,外层循环总控制。内层循环交换单个元素位置。

2、选择排序

选择排序原理:第一次从待排序的数据元素中选出最小(或最大)的一个元素,存放在序列的起始位置,然后再从剩余的未排序元素中寻找到最小(大)元素,然后放到已排序的序列的末尾。以此类推,直到全部待排序的数据元素的个数为零。

public class M03_Selection {
    public static void main(String[] args) {
        int[] arr = {30,70,50,90,60};
        selectionSort(arr);
    }
    public static void selectionSort (int[] arr){
        for (int i = 0; i < arr.length - 1; i++) {
            int minIndex = i;
            int minData = arr[i];
            for (int j = i + 1; j < arr.length; j++) {
                // 假设最小值判断
                if (minData > arr[j]) {
                    // 交换小值
                    minData = arr[j];
                    // 重置 minIndex,递增
                    minIndex = j;
                }
            }
            // 最小值交换放在arr[0]位置
            if (minIndex != i) {
                arr[minIndex] = arr[i];
                arr[i] = minData ;
            }
            System.out.println("第"+(i+1)+"轮排序:"+Arrays.toString(arr));
        }
    }
}

输出结果:

第1轮排序:[30, 70, 50, 90, 60]
第2轮排序:[30, 50, 70, 90, 60]
第3轮排序:[30, 50, 60, 90, 70]
第4轮排序:[30, 50, 60, 70, 90]

3、插入排序

基本思想是将一个记录插入到已经排好序的有序表中,排序过程中每次从无序表中取出第一个元素,把它依次与有序表元素进行比较,将它插入到有序表中的适当位置,使之成为新的有序表。在实现过程使用双层循环,外层循环对除了第一个元素之外的所有元素,内层循环对当前元素前面有序表进行待插入位置查找,并进行移动。

public class M04_Insert {
    public static void main(String[] args) {
        int[] arr = {10,40,90,20,80};
        insertSort(arr);
    }
    public static void insertSort (int[] arr) {
        int insertValue = 0;
        int insertIndex = 0;
        for (int i = 1; i < arr.length; i++) {
            // 待插入数的值和下标
            insertValue = arr[i];
            insertIndex = i - 1;
            // 写入位置
            while (insertIndex >= 0 && insertValue < arr[insertIndex]) {
                arr[insertIndex + 1] = arr[insertIndex];
                insertIndex--;
            }
            if (insertIndex + 1 != i) {
                arr[insertIndex + 1] = insertValue;
            }
            System.out.println("第" + i + "轮插入排序:"+Arrays.toString(arr));
        }
    }
}

输出结果:

第1轮插入排序:[10, 40, 90, 20, 80]
第2轮插入排序:[10, 40, 90, 20, 80]
第3轮插入排序:[10, 20, 40, 90, 80]
第4轮插入排序:[10, 20, 40, 80, 90]

三、查找算法

查找算法是指在一组元素中寻找一个特定的信息元素,在计算机应用中,查找是常用的基本运算,例如编译程序中符号表的查找;常用的查找算法有:顺序查找,二分查找,插值查找,斐波那契查找。

1、顺序查找

顺序查找是按照序列原有顺序对一组元素进行遍历,并与要查找的元素逐个比较的基本查找算法。

public class M05_OrderFind {
    public static void main(String[] args) {
        String[] arr = {"first","second","third"};
        System.out.println(seqSearch(arr,"second"));
    }
    public static int seqSearch(String[] arr, String value) {
        // 数组下标,-1代表没有
        int findIndex = -1 ;
        // 遍历并逐个对比
        for (int i = 0; i < arr.length; i++) {
            if(value.equals(arr[i])) {
                return i ;
            }
        }
        return findIndex ;
    }
}

2、二分查找

二分查找也称折半查找(Binary Search),它是一种效率较高的查找方法。但是,折半查找要求线性表必须采用顺序存储结构,而且表中元素按关键字有序排列。

public class M06_BinaryFind {
    public static void main(String[] args) {
        int arr[] = { 10, 20, 30, 40, 50 };
        int index = binarySearch (arr, 0, arr.length - 1, 40);
        System.out.println("index="+index);
    }
    public static int binarySearch(int[] arr, int leftIndex, int rightIndex, int findValue) {
        // leftIndex > rightIndex,没有查到
        if (leftIndex > rightIndex) {
            return -1;
        }
        int midIndex = (leftIndex + rightIndex) / 2;
        int midValue = arr[midIndex];
        // 向左递归
        if (findValue < midValue) {
            return binarySearch(arr, leftIndex, midIndex - 1, findValue);
        // 向右递归
        } else if (findValue > midValue) {
            return binarySearch(arr, midIndex + 1, rightIndex, findValue);
        // 直接找到
        } else {
            return midIndex;
        }
    }
}

如果要查询的元素是没有顺序的,可以基于上述模块二中的排序算法,先排序再查找。

四、源代码地址

GitHub·地址
https://github.com/cicadasmile/model-arithmetic-parent
GitEE·地址
https://gitee.com/cicadasmile/model-arithmetic-parent

推荐阅读:编程体系整理

序号 项目名称 GitHub地址 GitEE地址 推荐指数 01 Java描述设计模式,算法,数据结构 GitHub·点这里 GitEE·点这里 ☆☆☆☆☆ 02 Java基础、并发、面向对象、Web开发 GitHub·点这里 GitEE·点这里 ☆☆☆☆ 03 SpringCloud微服务基础组件案例详解 GitHub·点这里 GitEE·点这里 ☆☆☆ 04 SpringCloud微服务架构实战综合案例 GitHub·点这里 GitEE·点这里 ☆☆☆☆☆ 05 SpringBoot框架基础应用入门到进阶 GitHub·点这里 GitEE·点这里 ☆☆☆☆ 06 SpringBoot框架整合开发常用中间件 GitHub·点这里 GitEE·点这里 ☆☆☆☆☆ 07 数据管理、分布式、架构设计基础案例 GitHub·点这里 GitEE·点这里 ☆☆☆☆☆ 08 大数据系列、存储、组件、计算等框架 GitHub·点这里 GitEE·点这里 ☆☆☆☆☆

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK