# 一、递归算法

``````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]

## 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]

# 三、查找算法

## 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、二分查找

``````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·地址
GitEE·地址