Add cpp codes for the chapter
computational complexity, sorting, searching.
This commit is contained in:
@@ -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++) {
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
|
||||
@@ -9,7 +9,6 @@ package chapter_searching;
|
||||
import include.*;
|
||||
|
||||
public class linear_search {
|
||||
|
||||
/* 线性查找(数组) */
|
||||
static int linearSearch(int[] nums, int target) {
|
||||
// 遍历数组
|
||||
|
||||
@@ -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));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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));
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user