Translate all code to English (#1836)

* Review the EN heading format.

* Fix pythontutor headings.

* Fix pythontutor headings.

* bug fixes

* Fix headings in **/summary.md

* Revisit the CN-to-EN translation for Python code using Claude-4.5

* Revisit the CN-to-EN translation for Java code using Claude-4.5

* Revisit the CN-to-EN translation for Cpp code using Claude-4.5.

* Fix the dictionary.

* Fix cpp code translation for the multipart strings.

* Translate Go code to English.

* Update workflows to test EN code.

* Add EN translation for C.

* Add EN translation for CSharp.

* Add EN translation for Swift.

* Trigger the CI check.

* Revert.

* Update en/hash_map.md

* Add the EN version of Dart code.

* Add the EN version of Kotlin code.

* Add missing code files.

* Add the EN version of JavaScript code.

* Add the EN version of TypeScript code.

* Fix the workflows.

* Add the EN version of Ruby code.

* Add the EN version of Rust code.

* Update the CI check for the English version  code.

* Update Python CI check.

* Fix cmakelists for en/C code.

* Fix Ruby comments
This commit is contained in:
Yudong Jin
2025-12-31 07:44:52 +08:00
committed by GitHub
parent 45e1295241
commit 2778a6f9c7
1284 changed files with 71557 additions and 3275 deletions
@@ -0,0 +1,38 @@
// File: bubble_sort.go
// Created Time: 2022-12-06
// Author: Slone123c (274325721@qq.com)
package chapter_sorting
/* Bubble sort */
func bubbleSort(nums []int) {
// Outer loop: unsorted range is [0, i]
for i := len(nums) - 1; i > 0; i-- {
// Inner loop: swap the largest element in the unsorted range [0, i] to the rightmost end of that range
for j := 0; j < i; j++ {
if nums[j] > nums[j+1] {
// Swap nums[j] and nums[j + 1]
nums[j], nums[j+1] = nums[j+1], nums[j]
}
}
}
}
/* Bubble sort (flag optimization) */
func bubbleSortWithFlag(nums []int) {
// Outer loop: unsorted range is [0, i]
for i := len(nums) - 1; i > 0; i-- {
flag := false // Initialize flag
// Inner loop: swap the largest element in the unsorted range [0, i] to the rightmost end of that range
for j := 0; j < i; j++ {
if nums[j] > nums[j+1] {
// Swap nums[j] and nums[j + 1]
nums[j], nums[j+1] = nums[j+1], nums[j]
flag = true // Record element swap
}
}
if flag == false { // No elements were swapped in this round of "bubbling", exit directly
break
}
}
}
@@ -0,0 +1,20 @@
// File: bubble_sort_test.go
// Created Time: 2022-12-06
// Author: Slone123c (274325721@qq.com)
package chapter_sorting
import (
"fmt"
"testing"
)
func TestBubbleSort(t *testing.T) {
nums := []int{4, 1, 3, 1, 5, 2}
bubbleSort(nums)
fmt.Println("After bubble sort completes, nums = ", nums)
nums1 := []int{4, 1, 3, 1, 5, 2}
bubbleSortWithFlag(nums1)
fmt.Println("After bubble sort completes, nums1 = ", nums1)
}
@@ -0,0 +1,37 @@
// File: bucket_sort.go
// Created Time: 2023-03-27
// Author: Reanon (793584285@qq.com)
package chapter_sorting
import "sort"
/* Bucket sort */
func bucketSort(nums []float64) {
// Initialize k = n/2 buckets, expected to allocate 2 elements per bucket
k := len(nums) / 2
buckets := make([][]float64, k)
for i := 0; i < k; i++ {
buckets[i] = make([]float64, 0)
}
// 1. Distribute array elements into various buckets
for _, num := range nums {
// Input data range is [0, 1), use num * k to map to index range [0, k-1]
i := int(num * float64(k))
// Add num to bucket i
buckets[i] = append(buckets[i], num)
}
// 2. Sort each bucket
for i := 0; i < k; i++ {
// Use built-in slice sorting function, can also be replaced with other sorting algorithms
sort.Float64s(buckets[i])
}
// 3. Traverse buckets to merge results
i := 0
for _, bucket := range buckets {
for _, num := range bucket {
nums[i] = num
i++
}
}
}
@@ -0,0 +1,17 @@
// File: bucket_sort_test.go
// Created Time: 2023-03-27
// Author: Reanon (793584285@qq.com)
package chapter_sorting
import (
"fmt"
"testing"
)
func TestBucketSort(t *testing.T) {
// Assume input data is floating point, interval [0, 1)
nums := []float64{0.49, 0.96, 0.82, 0.09, 0.57, 0.43, 0.91, 0.75, 0.15, 0.37}
bucketSort(nums)
fmt.Println("After bucket sort completes, nums = ", nums)
}
@@ -0,0 +1,68 @@
// File: counting_sort.go
// Created Time: 2023-03-20
// Author: Reanon (793584285@qq.com)
package chapter_sorting
type CountingSort struct{}
/* Counting sort */
// Simple implementation, cannot be used for sorting objects
func countingSortNaive(nums []int) {
// 1. Count the maximum element m in the array
m := 0
for _, num := range nums {
if num > m {
m = num
}
}
// 2. Count the occurrence of each number
// counter[num] represents the occurrence of num
counter := make([]int, m+1)
for _, num := range nums {
counter[num]++
}
// 3. Traverse counter, filling each element back into the original array nums
for i, num := 0, 0; num < m+1; num++ {
for j := 0; j < counter[num]; j++ {
nums[i] = num
i++
}
}
}
/* Counting sort */
// Complete implementation, can sort objects and is a stable sort
func countingSort(nums []int) {
// 1. Count the maximum element m in the array
m := 0
for _, num := range nums {
if num > m {
m = num
}
}
// 2. Count the occurrence of each number
// counter[num] represents the occurrence of num
counter := make([]int, m+1)
for _, num := range nums {
counter[num]++
}
// 3. Calculate the prefix sum of counter, converting "occurrence count" to "tail index"
// counter[num]-1 is the last index where num appears in res
for i := 0; i < m; i++ {
counter[i+1] += counter[i]
}
// 4. Traverse nums in reverse order, placing each element into the result array res
// Initialize the array res to record results
n := len(nums)
res := make([]int, n)
for i := n - 1; i >= 0; i-- {
num := nums[i]
// Place num at the corresponding index
res[counter[num]-1] = num
// Decrement the prefix sum by 1, getting the next index to place num
counter[num]--
}
// Use result array res to overwrite the original array nums
copy(nums, res)
}
@@ -0,0 +1,20 @@
// File: counting_sort_test.go
// Created Time: 2023-03-20
// Author: Reanon (793584285@qq.com)
package chapter_sorting
import (
"fmt"
"testing"
)
func TestCountingSort(t *testing.T) {
nums := []int{1, 0, 1, 2, 0, 4, 0, 2, 2, 4}
countingSortNaive(nums)
fmt.Println("After counting sort (cannot sort objects) completes, nums = ", nums)
nums1 := []int{1, 0, 1, 2, 0, 4, 0, 2, 2, 4}
countingSort(nums1)
fmt.Println("After counting sort completes, nums1 = ", nums1)
}
+44
View File
@@ -0,0 +1,44 @@
// File: heap_sort.go
// Created Time: 2023-05-29
// Author: Reanon (793584285@qq.com)
package chapter_sorting
/* Heap length is n, start heapifying node i, from top to bottom */
func siftDown(nums *[]int, n, i int) {
for true {
// If node i is largest or indices l, r are out of bounds, no need to continue heapify, break
l := 2*i + 1
r := 2*i + 2
ma := i
if l < n && (*nums)[l] > (*nums)[ma] {
ma = l
}
if r < n && (*nums)[r] > (*nums)[ma] {
ma = r
}
// Swap two nodes
if ma == i {
break
}
// Swap two nodes
(*nums)[i], (*nums)[ma] = (*nums)[ma], (*nums)[i]
// Loop downwards heapification
i = ma
}
}
/* Heap sort */
func heapSort(nums *[]int) {
// Build heap operation: heapify all nodes except leaves
for i := len(*nums)/2 - 1; i >= 0; i-- {
siftDown(nums, len(*nums), i)
}
// Extract the largest element from the heap and repeat for n-1 rounds
for i := len(*nums) - 1; i > 0; i-- {
// Delete node
(*nums)[0], (*nums)[i] = (*nums)[i], (*nums)[0]
// Start heapifying the root node, from top to bottom
siftDown(nums, i, 0)
}
}
@@ -0,0 +1,16 @@
// File: heap_sort_test.go
// Created Time: 2023-05-29
// Author: Reanon (793584285@qq.com)
package chapter_sorting
import (
"fmt"
"testing"
)
func TestHeapSort(t *testing.T) {
nums := []int{4, 1, 3, 1, 5, 2}
heapSort(&nums)
fmt.Println("After heap sort completes, nums = ", nums)
}
@@ -0,0 +1,20 @@
// File: insertion_sort.go
// Created Time: 2022-12-12
// Author: msk397 (machangxinq@gmail.com)
package chapter_sorting
/* Insertion sort */
func insertionSort(nums []int) {
// Outer loop: sorted interval is [0, i-1]
for i := 1; i < len(nums); i++ {
base := nums[i]
j := i - 1
// Inner loop: insert base into the correct position within the sorted interval [0, i-1]
for j >= 0 && nums[j] > base {
nums[j+1] = nums[j] // Move nums[j] to the right by one position
j--
}
nums[j+1] = base // Assign base to the correct position
}
}
@@ -0,0 +1,16 @@
// File: insertion_sort_test.go
// Created Time: 2022-12-12
// Author: msk397 (machangxinq@gmail.com)
package chapter_sorting
import (
"fmt"
"testing"
)
func TestInsertionSort(t *testing.T) {
nums := []int{4, 1, 3, 1, 5, 2}
insertionSort(nums)
fmt.Println("After insertion sort, nums =", nums)
}
+54
View File
@@ -0,0 +1,54 @@
// File: merge_sort.go
// Created Time: 2022-12-13
// Author: msk397 (machangxinq@gmail.com)
package chapter_sorting
/* Merge left subarray and right subarray */
func merge(nums []int, left, mid, right int) {
// Left subarray interval is [left, mid], right subarray interval is [mid+1, right]
// Create a temporary array tmp to store the merged results
tmp := make([]int, right-left+1)
// Initialize the start indices of the left and right subarrays
i, j, k := left, mid+1, 0
// While both subarrays still have elements, compare and copy the smaller element into the temporary array
for i <= mid && j <= right {
if nums[i] <= nums[j] {
tmp[k] = nums[i]
i++
} else {
tmp[k] = nums[j]
j++
}
k++
}
// Copy the remaining elements of the left and right subarrays into the temporary array
for i <= mid {
tmp[k] = nums[i]
i++
k++
}
for j <= right {
tmp[k] = nums[j]
j++
k++
}
// Copy the elements from the temporary array tmp back to the original array nums at the corresponding interval
for k := 0; k < len(tmp); k++ {
nums[left+k] = tmp[k]
}
}
/* Merge sort */
func mergeSort(nums []int, left, right int) {
// Termination condition
if left >= right {
return
}
// Divide and conquer stage
mid := left + (right - left) / 2
mergeSort(nums, left, mid)
mergeSort(nums, mid+1, right)
// Merge stage
merge(nums, left, mid, right)
}
@@ -0,0 +1,16 @@
// File: merge_sort_test.go
// Created Time: 2022-12-13
// Author: msk397 (machangxinq@gmail.com)
package chapter_sorting
import (
"fmt"
"testing"
)
func TestMergeSort(t *testing.T) {
nums := []int{7, 3, 2, 6, 0, 1, 5, 4}
mergeSort(nums, 0, len(nums)-1)
fmt.Println("After merge sort completes, nums = ", nums)
}
+130
View File
@@ -0,0 +1,130 @@
// File: quick_sort.go
// Created Time: 2022-12-12
// Author: msk397 (machangxinq@gmail.com)
package chapter_sorting
// Quick sort
type quickSort struct{}
// Quick sort (recursion depth optimization)
type quickSortMedian struct{}
// Quick sort (recursion depth optimization)
type quickSortTailCall struct{}
/* Sentinel partition */
func (q *quickSort) partition(nums []int, left, right int) int {
// Use nums[left] as the pivot
i, j := left, right
for i < j {
for i < j && nums[j] >= nums[left] {
j-- // Search from right to left for the first element smaller than the pivot
}
for i < j && nums[i] <= nums[left] {
i++ // Search from left to right for the first element greater than the pivot
}
// Swap elements
nums[i], nums[j] = nums[j], nums[i]
}
// Swap the pivot to the boundary between the two subarrays
nums[i], nums[left] = nums[left], nums[i]
return i // Return the index of the pivot
}
/* Quick sort */
func (q *quickSort) quickSort(nums []int, left, right int) {
// Terminate recursion when subarray length is 1
if left >= right {
return
}
// Sentinel partition
pivot := q.partition(nums, left, right)
// Recursively process the left subarray and right subarray
q.quickSort(nums, left, pivot-1)
q.quickSort(nums, pivot+1, right)
}
/* Select the median of three candidate elements */
func (q *quickSortMedian) medianThree(nums []int, left, mid, right int) int {
l, m, r := nums[left], nums[mid], nums[right]
if (l <= m && m <= r) || (r <= m && m <= l) {
return mid // m is between l and r
}
if (m <= l && l <= r) || (r <= l && l <= m) {
return left // l is between m and r
}
return right
}
/* Sentinel partition (median of three) */
func (q *quickSortMedian) partition(nums []int, left, right int) int {
// Use nums[left] as the pivot
med := q.medianThree(nums, left, (left+right)/2, right)
// Swap the median to the array's leftmost position
nums[left], nums[med] = nums[med], nums[left]
// Use nums[left] as the pivot
i, j := left, right
for i < j {
for i < j && nums[j] >= nums[left] {
j-- // Search from right to left for the first element smaller than the pivot
}
for i < j && nums[i] <= nums[left] {
i++ // Search from left to right for the first element greater than the pivot
}
// Swap elements
nums[i], nums[j] = nums[j], nums[i]
}
// Swap the pivot to the boundary between the two subarrays
nums[i], nums[left] = nums[left], nums[i]
return i // Return the index of the pivot
}
/* Quick sort */
func (q *quickSortMedian) quickSort(nums []int, left, right int) {
// Terminate recursion when subarray length is 1
if left >= right {
return
}
// Sentinel partition
pivot := q.partition(nums, left, right)
// Recursively process the left subarray and right subarray
q.quickSort(nums, left, pivot-1)
q.quickSort(nums, pivot+1, right)
}
/* Sentinel partition */
func (q *quickSortTailCall) partition(nums []int, left, right int) int {
// Use nums[left] as the pivot
i, j := left, right
for i < j {
for i < j && nums[j] >= nums[left] {
j-- // Search from right to left for the first element smaller than the pivot
}
for i < j && nums[i] <= nums[left] {
i++ // Search from left to right for the first element greater than the pivot
}
// Swap elements
nums[i], nums[j] = nums[j], nums[i]
}
// Swap the pivot to the boundary between the two subarrays
nums[i], nums[left] = nums[left], nums[i]
return i // Return the index of the pivot
}
/* Quick sort (recursion depth optimization) */
func (q *quickSortTailCall) quickSort(nums []int, left, right int) {
// Terminate when subarray length is 1
for left < right {
// Sentinel partition operation
pivot := q.partition(nums, left, right)
// Perform quick sort on the shorter of the two subarrays
if pivot-left < right-pivot {
q.quickSort(nums, left, pivot-1) // Recursively sort the left subarray
left = pivot + 1 // Remaining unsorted interval is [pivot + 1, right]
} else {
q.quickSort(nums, pivot+1, right) // Recursively sort the right subarray
right = pivot - 1 // Remaining unsorted interval is [left, pivot - 1]
}
}
}
@@ -0,0 +1,34 @@
// File: quick_sort_test.go
// Created Time: 2022-12-12
// Author: msk397 (machangxinq@gmail.com)
package chapter_sorting
import (
"fmt"
"testing"
)
// Quick sort
func TestQuickSort(t *testing.T) {
q := quickSort{}
nums := []int{4, 1, 3, 1, 5, 2}
q.quickSort(nums, 0, len(nums)-1)
fmt.Println("After quick sort completes, nums = ", nums)
}
// Quick sort (recursion depth optimization)
func TestQuickSortMedian(t *testing.T) {
q := quickSortMedian{}
nums := []int{4, 1, 3, 1, 5, 2}
q.quickSort(nums, 0, len(nums)-1)
fmt.Println("After quick sort (median pivot optimization), nums = ", nums)
}
// Quick sort (recursion depth optimization)
func TestQuickSortTailCall(t *testing.T) {
q := quickSortTailCall{}
nums := []int{4, 1, 3, 1, 5, 2}
q.quickSort(nums, 0, len(nums)-1)
fmt.Println("After quick sort (recursion depth optimization), nums = ", nums)
}
+60
View File
@@ -0,0 +1,60 @@
// File: radix_sort.go
// Created Time: 2023-01-18
// Author: Reanon (793584285@qq.com)
package chapter_sorting
import "math"
/* Get the k-th digit of element num, where exp = 10^(k-1) */
func digit(num, exp int) int {
// Passing exp instead of k can avoid repeated expensive exponentiation here
return (num / exp) % 10
}
/* Counting sort (based on nums k-th digit) */
func countingSortDigit(nums []int, exp int) {
// Decimal digit range is 0~9, therefore need a bucket array of length 10
counter := make([]int, 10)
n := len(nums)
// Count the occurrence of digits 0~9
for i := 0; i < n; i++ {
d := digit(nums[i], exp) // Get the k-th digit of nums[i], noted as d
counter[d]++ // Count the occurrence of digit d
}
// Calculate prefix sum, converting "occurrence count" into "array index"
for i := 1; i < 10; i++ {
counter[i] += counter[i-1]
}
// Traverse in reverse, based on bucket statistics, place each element into res
res := make([]int, n)
for i := n - 1; i >= 0; i-- {
d := digit(nums[i], exp)
j := counter[d] - 1 // Get the index j for d in the array
res[j] = nums[i] // Place the current element at index j
counter[d]-- // Decrease the count of d by 1
}
// Use result to overwrite the original array nums
for i := 0; i < n; i++ {
nums[i] = res[i]
}
}
/* Radix sort */
func radixSort(nums []int) {
// Get the maximum element of the array, used to determine the maximum number of digits
max := math.MinInt
for _, num := range nums {
if num > max {
max = num
}
}
// Traverse from the lowest to the highest digit
for exp := 1; max >= exp; exp *= 10 {
// Perform counting sort on the k-th digit of array elements
// k = 1 -> exp = 1
// k = 2 -> exp = 10
// i.e., exp = 10^(k-1)
countingSortDigit(nums, exp)
}
}
@@ -0,0 +1,18 @@
// File: radix_sort_test.go
// Created Time: 2023-01-18
// Author: Reanon (793584285@qq.com)
package chapter_sorting
import (
"fmt"
"testing"
)
func TestRadixSort(t *testing.T) {
/* Radix sort */
nums := []int{10546151, 35663510, 42865989, 34862445, 81883077,
88906420, 72429244, 30524779, 82060337, 63832996}
radixSort(nums)
fmt.Println("After radix sort completes, nums = ", nums)
}
@@ -0,0 +1,24 @@
// File: selection_sort.go
// Created Time: 2023-05-29
// Author: Reanon (793584285@qq.com)
package chapter_sorting
/* Selection sort */
func selectionSort(nums []int) {
n := len(nums)
// Outer loop: unsorted interval is [i, n-1]
for i := 0; i < n-1; i++ {
// Inner loop: find the smallest element within the unsorted interval
k := i
for j := i + 1; j < n; j++ {
if nums[j] < nums[k] {
// Record the index of the smallest element
k = j
}
}
// Swap the smallest element with the first element of the unsorted interval
nums[i], nums[k] = nums[k], nums[i]
}
}
@@ -0,0 +1,16 @@
// File: selection_sort_test.go
// Created Time: 2023-05-29
// Author: Reanon (793584285@qq.com)
package chapter_sorting
import (
"fmt"
"testing"
)
func TestSelectionSort(t *testing.T) {
nums := []int{4, 1, 3, 1, 5, 2}
selectionSort(nums)
fmt.Println("After selection sort completes, nums = ", nums)
}