本文源码: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); }}
递归图解:
基于栈构造的特点,递归调用会造成如上的构造,当所有递归办法入栈胜利后,在顺次执行出栈动作,打印数据后果。
在理论开发中递归常常用来靠近树结构问题,阶乘算法,排序查找等数学类问题。
递归算法的条件必须要一直靠近退出条件,不然很容易呈现有限循环导致内存溢出异样问题。
二、排序算法
排序算法就是使一组数据记录,依照特定的排序策略,递增或递加的排列起来的操作;罕用的排序算法:冒泡排序,抉择排序,插入排序,希尔排序,归并排序,疾速排序,基数排序等;排序算法抉择:不同的排序业务能够通过多种算法测试,复杂度低,耗时短的优先应用。
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-parentGitEE·地址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·点这里 | ☆☆☆☆☆ |