mirror of
https://github.com/krahets/hello-algo.git
synced 2025-12-19 07:17:54 +08:00
Merge branch 'krahets:master' into master
This commit is contained in:
41
codes/c/chapter_sorting/insertion_sort.c
Normal file
41
codes/c/chapter_sorting/insertion_sort.c
Normal file
@@ -0,0 +1,41 @@
|
||||
/*
|
||||
* File: insertion_sort.c
|
||||
* Created Time: 2022-12-29
|
||||
* Author: Listening (https://github.com/L-Super)
|
||||
*/
|
||||
|
||||
#include "../include/include.h"
|
||||
|
||||
/* 插入排序 */
|
||||
void insertionSort(int nums[], int size)
|
||||
{
|
||||
// 外循环:base = nums[1], nums[2], ..., nums[n-1]
|
||||
for (int i = 1; i < size; i++)
|
||||
{
|
||||
int base = nums[i], j = i - 1;
|
||||
// 内循环:将 base 插入到左边的正确位置
|
||||
while (j >= 0 && nums[j] > base)
|
||||
{
|
||||
// 1. 将 nums[j] 向右移动一位
|
||||
nums[j + 1] = nums[j];
|
||||
j--;
|
||||
}
|
||||
// 2. 将 base 赋值到正确位置
|
||||
nums[j + 1] = base;
|
||||
}
|
||||
}
|
||||
|
||||
/* Driver Code */
|
||||
int main()
|
||||
{
|
||||
int nums[] = {4, 1, 3, 1, 5, 2};
|
||||
insertionSort(nums, 6);
|
||||
printf("插入排序完成后 nums = \n");
|
||||
for (int i = 0; i < 6; i++)
|
||||
{
|
||||
printf("%d ", nums[i]);
|
||||
}
|
||||
printf("\n");
|
||||
|
||||
return 0;
|
||||
}
|
||||
49
codes/csharp/chapter_stack_and_queue/deque.cs
Normal file
49
codes/csharp/chapter_stack_and_queue/deque.cs
Normal file
@@ -0,0 +1,49 @@
|
||||
/**
|
||||
* File: deque.cs
|
||||
* Created Time: 2022-12-30
|
||||
* Author: moonache (microin1301@outlook.com)
|
||||
*/
|
||||
|
||||
using NUnit.Framework;
|
||||
|
||||
namespace hello_algo.chapter_stack_and_queue
|
||||
{
|
||||
public class deque
|
||||
{
|
||||
[Test]
|
||||
public void Test()
|
||||
{
|
||||
/* 初始化双向队列 */
|
||||
// 在 C# 中,将链表 LinkedList 看作双向队列来使用
|
||||
LinkedList<int> deque = new LinkedList<int>();
|
||||
|
||||
/* 元素入队 */
|
||||
deque.AddLast(2); // 添加至队尾
|
||||
deque.AddLast(5);
|
||||
deque.AddLast(4);
|
||||
deque.AddFirst(3); // 添加至队首
|
||||
deque.AddFirst(1);
|
||||
Console.WriteLine("双向队列 deque = " + String.Join(",", deque.ToArray()));
|
||||
|
||||
/* 访问元素 */
|
||||
int peekFirst = deque.First.Value; // 队首元素
|
||||
Console.WriteLine("队首元素 peekFirst = " + peekFirst);
|
||||
int peekLast = deque.Last.Value; // 队尾元素
|
||||
Console.WriteLine("队尾元素 peekLast = " + peekLast);
|
||||
|
||||
/* 元素出队 */
|
||||
deque.RemoveFirst(); // 队首元素出队
|
||||
Console.WriteLine("队首元素出队后 deque = " + String.Join(",", deque.ToArray()));
|
||||
deque.RemoveLast(); // 队尾元素出队
|
||||
Console.WriteLine("队尾元素出队后 deque = " + String.Join(",", deque.ToArray()));
|
||||
|
||||
/* 获取双向队列的长度 */
|
||||
int size = deque.Count;
|
||||
Console.WriteLine("双向队列长度 size = " + size);
|
||||
|
||||
/* 判断双向队列是否为空 */
|
||||
bool isEmpty = deque.Count == 0;
|
||||
Console.WriteLine("双向队列是否为空 = " + isEmpty);
|
||||
}
|
||||
}
|
||||
}
|
||||
80
codes/go/chapter_array_and_linkedlist/array.go
Normal file
80
codes/go/chapter_array_and_linkedlist/array.go
Normal file
@@ -0,0 +1,80 @@
|
||||
// File: array.go
|
||||
// Created Time: 2022-12-29
|
||||
// Author: GuoWei (gongguowei01@gmail.com)
|
||||
|
||||
package chapter_array_and_linkedlist
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"math/rand"
|
||||
)
|
||||
|
||||
/**
|
||||
我们将 Go 中的 Slice 切片看作 Array 数组,降低理解成本,
|
||||
有利于我们将关注点放在数据结构与算法上。
|
||||
*/
|
||||
|
||||
/* 随机返回一个数组元素 */
|
||||
func randomAccess(nums []int) (randomNum int) {
|
||||
// 在区间 [0, nums.length) 中随机抽取一个数字
|
||||
randomIndex := rand.Intn(len(nums))
|
||||
// 获取并返回随机元素
|
||||
randomNum = nums[randomIndex]
|
||||
return
|
||||
}
|
||||
|
||||
/* 扩展数组长度 */
|
||||
func extend(nums []int, enlarge int) []int {
|
||||
// 初始化一个扩展长度后的数组
|
||||
res := make([]int, len(nums)+enlarge)
|
||||
// 将原数组中的所有元素复制到新数组
|
||||
for i := 0; i < len(nums); i++ {
|
||||
res[i] = nums[i]
|
||||
}
|
||||
// 返回扩展后的新数组
|
||||
return res
|
||||
}
|
||||
|
||||
/* 在数组的索引 index 处插入元素 num */
|
||||
func insert(nums []int, num int, index int) {
|
||||
// 把索引 index 以及之后的所有元素向后移动一位
|
||||
// 如果超出了数组长度,会被直接舍弃
|
||||
for i := len(nums) - 1; i > index; i-- {
|
||||
nums[i] = nums[i-1]
|
||||
}
|
||||
// 将 num 赋给 index 处元素
|
||||
nums[index] = num
|
||||
}
|
||||
|
||||
/* 删除索引 index 处元素 */
|
||||
func remove(nums []int, index int) {
|
||||
// 把索引 index 之后的所有元素向前移动一位
|
||||
for i := index; i < len(nums) - 1; i++ {
|
||||
nums[i] = nums[i+1]
|
||||
}
|
||||
}
|
||||
|
||||
/* 遍历数组 */
|
||||
func traverse(nums []int) {
|
||||
var count int
|
||||
// 通过索引遍历数组
|
||||
for i := 0; i < len(nums); i++ {
|
||||
count++
|
||||
}
|
||||
// 直接遍历数组
|
||||
for index, val := range nums {
|
||||
fmt.Printf("index:%v value:%v\n", index, val)
|
||||
}
|
||||
}
|
||||
|
||||
/* 在数组中查找指定元素 */
|
||||
func find(nums []int, target int) (index int) {
|
||||
index = -1
|
||||
for i := 0; i < len(nums); i++ {
|
||||
if nums[i] == target {
|
||||
index = i
|
||||
break
|
||||
}
|
||||
}
|
||||
return
|
||||
}
|
||||
47
codes/go/chapter_array_and_linkedlist/array_test.go
Normal file
47
codes/go/chapter_array_and_linkedlist/array_test.go
Normal file
@@ -0,0 +1,47 @@
|
||||
// File: array_test.go
|
||||
// Created Time: 2022-12-29
|
||||
// Author: GuoWei (gongguowei01@gmail.com)
|
||||
|
||||
package chapter_array_and_linkedlist
|
||||
|
||||
/**
|
||||
我们将 Go 中的 Slice 切片看作 Array 数组。因为这样可以
|
||||
降低理解成本,利于我们将关注点放在数据结构与算法上。
|
||||
*/
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"testing"
|
||||
)
|
||||
|
||||
/* Driver Code */
|
||||
func TestArray(t *testing.T) {
|
||||
/* 初始化数组 */
|
||||
var arr []int
|
||||
fmt.Println("数组 arr = ", arr)
|
||||
nums := []int{1, 3, 2, 5, 4}
|
||||
fmt.Println("数组 nums = ", nums)
|
||||
|
||||
/* 随机访问 */
|
||||
randomNum := randomAccess(nums)
|
||||
fmt.Println("在 nums 中获取随机元素 ", randomNum)
|
||||
|
||||
/* 长度扩展 */
|
||||
nums = extend(nums, 3)
|
||||
fmt.Println("将数组长度扩展至 8 ,得到 nums = ", nums)
|
||||
|
||||
/* 插入元素 */
|
||||
insert(nums, 6, 3)
|
||||
fmt.Println("在索引 3 处插入数字 6 ,得到 nums = ", nums)
|
||||
|
||||
/* 删除元素 */
|
||||
remove(nums, 2)
|
||||
fmt.Println("删除索引 2 处的元素,得到 nums = ", nums)
|
||||
|
||||
/* 遍历数组 */
|
||||
traverse(nums)
|
||||
|
||||
/* 查找元素 */
|
||||
index := find(nums, 3)
|
||||
fmt.Println("在 nums 中查找元素 3 ,得到索引 = ", index)
|
||||
}
|
||||
54
codes/typescript/chapter_searching/binary_search.ts
Normal file
54
codes/typescript/chapter_searching/binary_search.ts
Normal file
@@ -0,0 +1,54 @@
|
||||
/*
|
||||
* File: binary_search.ts
|
||||
* Created Time: 2022-12-27
|
||||
* Author: Daniel (better.sunjian@gmail.com)
|
||||
*/
|
||||
|
||||
/* 二分查找(双闭区间) */
|
||||
const binarySearch = function (nums: number[], target: number): number {
|
||||
// 初始化双闭区间 [0, n-1] ,即 i, j 分别指向数组首元素、尾元素
|
||||
let i = 0, j = nums.length - 1;
|
||||
// 循环,当搜索区间为空时跳出(当 i > j 时为空)
|
||||
while (i <= j) {
|
||||
const m = Math.floor(i + (j - i) / 2); // 计算中点索引 m
|
||||
if (nums[m] < target) { // 此情况说明 target 在区间 [m+1, j] 中
|
||||
i = m + 1;
|
||||
} else if (nums[m] > target) { // 此情况说明 target 在区间 [i, m-1] 中
|
||||
j = m - 1;
|
||||
} else { // 找到目标元素,返回其索引
|
||||
return m;
|
||||
}
|
||||
}
|
||||
return -1; // 未找到目标元素,返回 -1
|
||||
}
|
||||
|
||||
/* 二分查找(左闭右开) */
|
||||
const binarySearch1 = function (nums: number[], target: number): number {
|
||||
// 初始化左闭右开 [0, n) ,即 i, j 分别指向数组首元素、尾元素+1
|
||||
let i = 0, j = nums.length;
|
||||
// 循环,当搜索区间为空时跳出(当 i = j 时为空)
|
||||
while (i < j) {
|
||||
const m = Math.floor(i + (j - i) / 2); // 计算中点索引 m
|
||||
if (nums[m] < target) { // 此情况说明 target 在区间 [m+1, j] 中
|
||||
i = m + 1;
|
||||
} else if (nums[m] > target) { // 此情况说明 target 在区间 [i, m] 中
|
||||
j = m;
|
||||
} else { // 找到目标元素,返回其索引
|
||||
return m;
|
||||
}
|
||||
}
|
||||
return -1; // 未找到目标元素,返回 -1
|
||||
}
|
||||
|
||||
|
||||
/* Driver Code */
|
||||
const target = 6;
|
||||
const nums = [ 1, 3, 6, 8, 12, 15, 23, 67, 70, 92 ];
|
||||
|
||||
/* 二分查找(双闭区间) */
|
||||
let index = binarySearch(nums, target);
|
||||
console.info('目标元素 6 的索引 = %d', index);
|
||||
|
||||
/* 二分查找(左闭右开) */
|
||||
index = binarySearch1(nums, target);
|
||||
console.info('目标元素 6 的索引 = %d', index);
|
||||
Reference in New Issue
Block a user