Android学习总结之算法篇四(排序)
归并排序原理
归并排序(Merge Sort)是一种采用分治法(Divide and Conquer)的排序算法,其基本思想是将一个大问题分解为多个小问题,分别解决这些小问题,然后将小问题的解合并起来得到原问题的解。具体步骤如下:
-
分解(Divide):将待排序的数组从中间分成两个子数组,递归地对这两个子数组继续进行分解,直到每个子数组中只有一个元素(因为单个元素的数组本身就是有序的)。
-
解决(Conquer):对每个子数组进行排序,由于每个子数组只有一个元素,所以这一步实际上已经完成了排序。
-
合并(Merge):将两个已排序的子数组合并成一个新的有序数组。重复这个合并过程,直到所有的子数组合并成一个完整的有序数组。
代码实现及注释
下面是用 Java 实现的归并排序代码,并带有详细的注释:
public class MergeSort {
// 归并排序的主函数
public static void mergeSort(int[] arr) {
if (arr == null || arr.length <= 1) {
return; // 如果数组为空或只有一个元素,无需排序
}
int[] temp = new int[arr.length]; // 创建一个临时数组,用于合并过程
mergeSort(arr, 0, arr.length - 1, temp);
}
// 递归进行归并排序
private static void mergeSort(int[] arr, int left, int right, int[] temp) {
if (left < right) {
int mid = left + (right - left) / 2; // 计算中间位置
// 递归排序左半部分
mergeSort(arr, left, mid, temp);
// 递归排序右半部分
mergeSort(arr, mid + 1, right, temp);
// 合并两个已排序的子数组
merge(arr, left, mid, right, temp);
}
}
// 合并两个已排序的子数组
private static void merge(int[] arr, int left, int mid, int right, int[] temp) {
int i = left; // 左子数组的起始索引
int j = mid + 1; // 右子数组的起始索引
int k = left; // 临时数组的起始索引
// 比较左右子数组的元素,将较小的元素放入临时数组
while (i <= mid && j <= right) {
if (arr[i] <= arr[j]) {
temp[k++] = arr[i++];
} else {
temp[k++] = arr[j++];
}
}
// 将左子数组中剩余的元素复制到临时数组
while (i <= mid) {
temp[k++] = arr[i++];
}
// 将右子数组中剩余的元素复制到临时数组
while (j <= right) {
temp[k++] = arr[j++];
}
// 将临时数组中的元素复制回原数组
for (k = left; k <= right; k++) {
arr[k] = temp[k];
}
}
public static void main(String[] args) {
int[] arr = {9, 5, 7, 1, 3, 8, 4, 2, 6};
mergeSort(arr);
for (int num : arr) {
System.out.print(num + " ");
}
}
}
100000个数字中,对前10000小的数字进行排序(堆排)
import java.util.Arrays;
import java.util.PriorityQueue;
public class SortTop10000Smallest {
/**
* 从给定的数字数组中找出前 10000 小的数字
*
* @param numbers 包含 100000 个数字的数组
* @return 包含前 10000 小数字的数组
*/
public static int[] getTop10000Smallest(int[] numbers) {
// 创建一个最大堆,堆的大小为 10000。
// 通过自定义比较器 (a, b) -> b - a 来实现最大堆,即堆顶元素为堆中最大的元素
PriorityQueue<Integer> maxHeap = new PriorityQueue<>(10000, (a, b) -> b - a);
// 遍历给定的数字数组
for (int num : numbers) {
if (maxHeap.size() < 10000) {
// 如果堆的大小小于 10000,直接将当前数字加入堆中
maxHeap.offer(num);
} else if (num < maxHeap.peek()) {
// 如果堆的大小已经达到 10000,且当前数字小于堆顶元素
// 则移除堆顶元素(即当前堆中的最大元素),并将当前数字加入堆中
maxHeap.poll();
maxHeap.offer(num);
}
}
// 创建一个大小为 10000 的数组,用于存储最终的前 10000 小的数字
int[] top10000 = new int[10000];
// 从数组的最后一个位置开始,将堆中的元素依次取出放入数组中
for (int i = 9999; i >= 0; i--) {
top10000[i] = maxHeap.poll();
}
return top10000;
}
public static void main(String[] args) {
// 生成一个包含 100000 个随机数字的数组,数字范围在 0 到 999999 之间
int[] numbers = new int[100000];
for (int i = 0; i < 100000; i++) {
numbers[i] = (int) (Math.random() * 1000000);
}
// 调用 getTop10000Smallest 方法,获取前 10000 小的数字
int[] top10000 = getTop10000Smallest(numbers);
// 对前 10000 小的数字进行排序,使用 Arrays 类的 sort 方法
Arrays.sort(top10000);
// 遍历排序后的数组,打印每个数字
for (int num : top10000) {
System.out.println(num);
}
}
}
代码解释
- 最大堆的创建:使用
PriorityQueue
来创建一个最大堆,堆的大小为 10000。通过自定义比较器(a, b) -> b - a
来实现最大堆。 - 遍历数字数组:遍历 100000 个数字,若堆的大小小于 10000,直接将数字加入堆中;若当前数字小于堆顶元素,移除堆顶元素并加入当前数字。
- 将堆中的元素转换为数组:遍历堆,将堆中的元素依次取出放入数组中。
- 对前 10000 小的数字进行排序:使用
Arrays.sort
方法对数组进行排序。 - 打印排序后的结果:遍历排序后的数组,打印每个元素。
二维数组每行每列都按非严格递增排列,找target是否在里面。
(二分查找)
public class TwoDArrayBinarySearch {
// 该方法用于在二维数组中查找目标值
public static boolean searchMatrix(int[][] matrix, int target) {
// 检查二维数组是否为空,如果为空则直接返回 false
if (matrix == null || matrix.length == 0 || matrix[0].length == 0) {
return false;
}
// 获取二维数组的行数
int rows = matrix.length;
// 获取二维数组的列数
int cols = matrix[0].length;
// 第一次二分查找:找到可能包含目标值的行
// 初始化上边界为第一行
int top = 0;
// 初始化下边界为最后一行
int bottom = rows - 1;
while (top <= bottom) {
// 计算中间行的索引
int midRow = top + (bottom - top) / 2;
// 检查目标值是否在中间行元素的范围内
if (matrix[midRow][0] <= target && target <= matrix[midRow][cols - 1]) {
// 第二次二分查找:在该行中查找目标值
// 初始化左边界为该行的第一个元素
int left = 0;
// 初始化右边界为该行的最后一个元素
int right = cols - 1;
while (left <= right) {
// 计算中间列的索引
int midCol = left + (right - left) / 2;
// 如果中间元素等于目标值,说明找到了目标值,返回 true
if (matrix[midRow][midCol] == target) {
return true;
} else if (matrix[midRow][midCol] < target) {
// 如果中间元素小于目标值,说明目标值在右半部分,更新左边界
left = midCol + 1;
} else {
// 如果中间元素大于目标值,说明目标值在左半部分,更新右边界
right = midCol - 1;
}
}
// 在该行中未找到目标值,返回 false
return false;
} else if (matrix[midRow][0] > target) {
// 如果中间行的第一个元素大于目标值,说明目标值在上面的行,更新下边界
bottom = midRow - 1;
} else {
// 如果中间行的第一个元素小于目标值,说明目标值在下面的行,更新上边界
top = midRow + 1;
}
}
// 在所有行中都未找到可能包含目标值的行,返回 false
return false;
}
public static void main(String[] args) {
// 定义一个示例二维数组
int[][] matrix = {
{1, 4, 7, 11, 15},
{2, 5, 8, 12, 19},
{3, 6, 9, 16, 22},
{10, 13, 14, 17, 24},
{18, 21, 23, 26, 30}
};
// 定义目标值
int target = 5;
// 调用 searchMatrix 方法进行查找,并输出结果
System.out.println(searchMatrix(matrix, target));
}
}
二叉树中最近公共祖先(LCA)
二叉树节点定义
class TreeNode {
int val;
TreeNode left;
TreeNode right;
TreeNode(int x) { val = x; }
}
最近公共祖先算法(递归解法)
public class LowestCommonAncestor {
// 主函数:查找 p 和 q 的最近公共祖先
public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
// 递归终止条件:当前节点为空,或找到 p 或 q(自己也是自己的祖先)
if (root == null || root == p || root == q) {
return root;
}
// 递归查找左子树和右子树
TreeNode left = lowestCommonAncestor(root.left, p, q);
TreeNode right = lowestCommonAncestor(root.right, p, q);
// 情况 1:左子树和右子树都找到节点 → 说明当前根节点是 LCA
if (left != null && right != null) {
return root;
}
// 情况 2:左子树找到节点,右子树没找到 → 返回左子树的结果(可能是 p/q 或它们的祖先)
else if (left != null) {
return left;
}
// 情况 3:右子树找到节点,左子树没找到 → 返回右子树的结果
else {
return right;
}
}
// 测试用例
public static void main(String[] args) {
// 构建示例二叉树
TreeNode root = new TreeNode(3);
root.left = new TreeNode(5);
root.right = new TreeNode(1);
root.left.left = new TreeNode(6);
root.left.right = new TreeNode(2);
root.right.left = new TreeNode(0);
root.right.right = new TreeNode(8);
root.left.right.left = new TreeNode(7);
root.left.right.right = new TreeNode(4);
LowestCommonAncestor solution = new LowestCommonAncestor();
// 测试案例 1:p=5, q=1 → LCA 是 root(3)
TreeNode p = root.left; // val=5
TreeNode q = root.right; // val=1
System.out.println("LCA of 5 and 1 is: " + solution.lowestCommonAncestor(root, p, q).val); // 输出 3
// 测试案例 2:p=5, q=4 → LCA 是 5
q = root.left.right.right; // val=4
System.out.println("LCA of 5 and 4 is: " + solution.lowestCommonAncestor(root, p, q).val); // 输出 5
// 测试案例 3:p=7, q=4 → LCA 是 2
TreeNode p2 = root.left.right.left; // val=7
q = root.left.right.right; // val=4
System.out.println("LCA of 7 and 4 is: " + solution.lowestCommonAncestor(root, p2, q).val); // 输出 2
}
}
算法原理(递归三要素)
-
终止条件:
- 当前节点为空(
root == null
):说明没找到,返回null
- 当前节点是
p
或q
:直接返回自己(自己是自己的祖先)
- 当前节点为空(
-
递归逻辑:
- 分别在左子树和右子树中查找
p
和q
的祖先 - 若左子树和右子树都找到节点 → 说明当前节点是它们的公共祖先(因为左子树和右子树各有一个节点,当前节点是最近的公共祖先)
- 若只有左子树找到 → 结果在左子树中(可能是
p/q
或它们的祖先) - 若只有右子树找到 → 结果在右子树中
- 分别在左子树和右子树中查找
-
合并结果:
通过递归的返回值,自底向上判断当前节点是否为 LCA,最终返回最近的那个祖先。
算法特点
- 时间复杂度:O (n),每个节点最多被访问一次
- 空间复杂度:O (n)(递归栈空间,最坏情况下树退化为链表)
- 适用场景:二叉树(无论是否为二叉搜索树),且节点值唯一,p 和 q 一定存在于树中
关键思路
- 分治思想:将问题分解为左子树和右子树的子问题,通过子问题的解合并得到原问题的解
- 后序遍历:先处理左右子树,再处理当前节点,符合 “自底向上” 查找祖先的逻辑
- 唯一性假设:利用 “树中节点值唯一” 的特性,直接通过节点引用判断是否为 p 或 q
找View树的最近公共祖先
// 定义 View 类
class View {
View[] childs;
View parent;
public View() {
this.childs = null;
this.parent = null;
}
}
public class ViewTreeLCA {
// 查找两个 View 的最近公共祖先
public static View findLowestCommonAncestor(View view1, View view2) {
// 存储 view1 到根节点的路径
java.util.HashSet<View> path = new java.util.HashSet<>();
// 从 view1 开始,将其到根节点的路径上的所有 View 加入到 path 集合中
View current = view1;
while (current != null) {
path.add(current);
current = current.parent;
}
// 从 view2 开始,沿着其父节点向上遍历
current = view2;
while (current != null) {
// 如果当前 View 已经在 path 集合中,说明找到了最近公共祖先
if (path.contains(current)) {
return current;
}
current = current.parent;
}
// 如果没有找到公共祖先,返回 null
return null;
}
public static void main(String[] args) {
// 构建一个简单的 View 树
View root = new View();
View child1 = new View();
View child2 = new View();
View grandChild1 = new View();
View grandChild2 = new View();
root.childs = new View[]{child1, child2};
child1.parent = root;
child2.parent = root;
child1.childs = new View[]{grandChild1};
grandChild1.parent = child1;
child2.childs = new View[]{grandChild2};
grandChild2.parent = child2;
// 查找 grandChild1 和 grandChild2 的最近公共祖先
View lca = findLowestCommonAncestor(grandChild1, grandChild2);
if (lca != null) {
System.out.println("最近公共祖先是存在的。");
} else {
System.out.println("未找到最近公共祖先。");
}
}
}
代码思路:
- 存储路径:首先,从
view1
开始,沿着其父节点向上遍历,将经过的所有View
存储在一个HashSet
中,这个集合记录了view1
到根节点的路径。 - 查找公共祖先:接着,从
view2
开始,同样沿着其父节点向上遍历。对于每个经过的View
,检查它是否已经在之前存储的路径集合中。如果存在,说明找到了最近公共祖先,返回该View
。 - 未找到情况:如果遍历完
view2
到根节点的路径都没有找到公共祖先,返回null
。