算法:
冒泡排序
选择排序
二分查找
1. 冒泡排序
1.1 定义
1.2 代码示例
Java业务逻辑-1(冒泡排序)-CSDN博客https://blog.csdn.net/XiaomeiGuiSnJs/article/details/140880229
2. 选择排序
2.1 定义
2.2 代码示例
package cn.hdc.itWork.d5.d2;
import java.util.Arrays;
//选择排序
public class t2 {
public static void main(String[] args) {
int[] arr = {5, 2, 3, 1};
for (int i = 0; i < arr.length; i++) {
int minIndex = i;
for (int j = i + 1; j < arr.length; j++) {
if (arr[i] > arr[j]) {
minIndex = j;
}
}
int temp = arr[i];
arr[i] = arr[minIndex];
arr[minIndex] = temp;
}
System.out.println(Arrays.toString(arr));
}
}
3. 二分查找
3.1 定义
3.2 代码示例
package cn.hdc.itWork.d5.d2;
import java.util.Scanner;
//二分查找
public class t3 {
public static void main(String[] args) {
int u = u();
System.out.println(u);
}
public static int u() {
int[] arr = {7, 23, 79, 81, 103, 127, 131, 147};
int start = 0;
int end = arr.length - 1;
Scanner sc = new Scanner(System.in);
int num = sc.nextInt();
if (num < arr[0] || num > arr[arr.length - 1]) {
return -1;
}
while (start <= end) {
int mid = (start + end) / 2;
if (num == arr[mid]) {
return mid;
} else if (num > arr[mid]) {
start = mid + 1;
} else {
end = mid - 1;
}
}
return -2;
}
}
4. 方法递归
Java算法__递归-CSDN博客https://blog.csdn.net/XiaomeiGuiSnJs/article/details/141816321