Add cpp codes for the chapter

computational complexity, sorting, searching.
This commit is contained in:
Yudong Jin
2022-11-27 04:20:30 +08:00
parent 431a0f6caf
commit 19a4ccd86a
32 changed files with 1362 additions and 52 deletions

View File

@@ -59,7 +59,7 @@ public class space_complexity_types {
/* 平方阶 */
static void quadratic(int n) {
// 矩阵占用 O(n^2) 空间
int numMatrix[][] = new int[n][n];
int[][] numMatrix = new int[n][n];
// 二维列表占用 O(n^2) 空间
List<List<Integer>> numList = new ArrayList<>();
for (int i = 0; i < n; i++) {

View File

@@ -20,7 +20,7 @@ public class hashing_search {
/* 哈希查找(链表) */
static ListNode hashingSearch1(Map<Integer, ListNode> map, int target) {
// 哈希表的 key: 目标结点值value: 结点对象
// 若哈希表中无此 key ,返回 -1
// 若哈希表中无此 key ,返回 null
return map.getOrDefault(target, null);
}

View File

@@ -9,7 +9,6 @@ package chapter_searching;
import include.*;
public class linear_search {
/* 线性查找(数组) */
static int linearSearch(int[] nums, int target) {
// 遍历数组

View File

@@ -47,10 +47,10 @@ public class bubble_sort {
public static void main(String[] args) {
int[] nums = { 4, 1, 3, 1, 5, 2 };
bubbleSort(nums);
System.out.println("排序后数组 nums = " + Arrays.toString(nums));
System.out.println("冒泡排序完成后 nums = " + Arrays.toString(nums));
int[] nums1 = { 4, 1, 3, 1, 5, 2 };
bubbleSortWithFlag(nums1);
System.out.println("排序后数组 nums1 = " + Arrays.toString(nums));
System.out.println("冒泡排序完成后 nums1 = " + Arrays.toString(nums));
}
}

View File

@@ -26,6 +26,6 @@ public class insertion_sort {
public static void main(String[] args) {
int[] nums = { 4, 1, 3, 1, 5, 2 };
insertionSort(nums);
System.out.println("排序后数组 nums = " + Arrays.toString(nums));
System.out.println("插入排序完成后 nums = " + Arrays.toString(nums));
}
}

View File

@@ -51,7 +51,7 @@ public class merge_sort {
public static void main(String[] args) {
/* 归并排序 */
int[] nums = { 2, 4, 1, 0, 3, 5 };
int[] nums = { 7, 3, 2, 6, 0, 1, 5, 4 };
mergeSort(nums, 0, nums.length - 1);
System.out.println("归并排序完成后 nums = " + Arrays.toString(nums));
}