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

View File

@@ -0,0 +1,49 @@
/**
* File: bubble_sort.js
* Created Time: 2022-12-01
* Author: IsChristina (christinaxia77@foxmail.com)
*/
/* Bubble sort */
function bubbleSort(nums) {
// Outer loop: unsorted range is [0, i]
for (let i = nums.length - 1; i > 0; i--) {
// Inner loop: swap the largest element in the unsorted range [0, i] to the rightmost end of that range
for (let j = 0; j < i; j++) {
if (nums[j] > nums[j + 1]) {
// Swap nums[j] and nums[j + 1]
let tmp = nums[j];
nums[j] = nums[j + 1];
nums[j + 1] = tmp;
}
}
}
}
/* Bubble sort (flag optimization) */
function bubbleSortWithFlag(nums) {
// Outer loop: unsorted range is [0, i]
for (let i = nums.length - 1; i > 0; i--) {
let flag = false; // Initialize flag
// Inner loop: swap the largest element in the unsorted range [0, i] to the rightmost end of that range
for (let j = 0; j < i; j++) {
if (nums[j] > nums[j + 1]) {
// Swap nums[j] and nums[j + 1]
let tmp = nums[j];
nums[j] = nums[j + 1];
nums[j + 1] = tmp;
flag = true; // Record element swap
}
}
if (!flag) break; // No elements were swapped in this round of "bubbling", exit directly
}
}
/* Driver Code */
const nums = [4, 1, 3, 1, 5, 2];
bubbleSort(nums);
console.log('After bubble sort, nums =', nums);
const nums1 = [4, 1, 3, 1, 5, 2];
bubbleSortWithFlag(nums1);
console.log('After bubble sort, nums =', nums1);

View File

@@ -0,0 +1,39 @@
/**
* File: bucket_sort.js
* Created Time: 2023-04-08
* Author: Justin (xiefahit@gmail.com)
*/
/* Bucket sort */
function bucketSort(nums) {
// Initialize k = n/2 buckets, expected to allocate 2 elements per bucket
const k = nums.length / 2;
const buckets = [];
for (let i = 0; i < k; i++) {
buckets.push([]);
}
// 1. Distribute array elements into various buckets
for (const num of nums) {
// Input data range is [0, 1), use num * k to map to index range [0, k-1]
const i = Math.floor(num * k);
// Add num to bucket i
buckets[i].push(num);
}
// 2. Sort each bucket
for (const bucket of buckets) {
// Use built-in sorting function, can also replace with other sorting algorithms
bucket.sort((a, b) => a - b);
}
// 3. Traverse buckets to merge results
let i = 0;
for (const bucket of buckets) {
for (const num of bucket) {
nums[i++] = num;
}
}
}
/* Driver Code */
const nums = [0.49, 0.96, 0.82, 0.09, 0.57, 0.43, 0.91, 0.75, 0.15, 0.37];
bucketSort(nums);
console.log('After bucket sort, nums =', nums);

View File

@@ -0,0 +1,65 @@
/**
* File: counting_sort.js
* Created Time: 2023-04-08
* Author: Justin (xiefahit@gmail.com)
*/
/* Counting sort */
// Simple implementation, cannot be used for sorting objects
function countingSortNaive(nums) {
// 1. Count the maximum element m in the array
let m = Math.max(...nums);
// 2. Count the occurrence of each number
// counter[num] represents the occurrence of num
const counter = new Array(m + 1).fill(0);
for (const num of nums) {
counter[num]++;
}
// 3. Traverse counter, filling each element back into the original array nums
let i = 0;
for (let num = 0; num < m + 1; num++) {
for (let j = 0; j < counter[num]; j++, i++) {
nums[i] = num;
}
}
}
/* Counting sort */
// Complete implementation, can sort objects and is a stable sort
function countingSort(nums) {
// 1. Count the maximum element m in the array
let m = Math.max(...nums);
// 2. Count the occurrence of each number
// counter[num] represents the occurrence of num
const counter = new Array(m + 1).fill(0);
for (const num of 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 (let 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
const n = nums.length;
const res = new Array(n);
for (let i = n - 1; i >= 0; i--) {
const num = nums[i];
res[counter[num] - 1] = num; // Place num at the corresponding index
counter[num]--; // Decrement the prefix sum by 1, getting the next index to place num
}
// Use result array res to overwrite the original array nums
for (let i = 0; i < n; i++) {
nums[i] = res[i];
}
}
/* Driver Code */
const nums = [1, 0, 1, 2, 0, 4, 0, 2, 2, 4];
countingSortNaive(nums);
console.log('After counting sort (cannot sort objects), nums =', nums);
const nums1 = [1, 0, 1, 2, 0, 4, 0, 2, 2, 4];
countingSort(nums1);
console.log('After counting sort, nums1 =', nums1);

View File

@@ -0,0 +1,49 @@
/**
* File: heap_sort.js
* Created Time: 2023-06-04
* Author: Justin (xiefahit@gmail.com)
*/
/* Heap length is n, start heapifying node i, from top to bottom */
function siftDown(nums, n, i) {
while (true) {
// If node i is largest or indices l, r are out of bounds, no need to continue heapify, break
let l = 2 * i + 1;
let r = 2 * i + 2;
let 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 */
function heapSort(nums) {
// Build heap operation: heapify all nodes except leaves
for (let i = Math.floor(nums.length / 2) - 1; i >= 0; i--) {
siftDown(nums, nums.length, i);
}
// Extract the largest element from the heap and repeat for n-1 rounds
for (let i = nums.length - 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);
}
}
/* Driver Code */
const nums = [4, 1, 3, 1, 5, 2];
heapSort(nums);
console.log('After heap sort, nums =', nums);

View File

@@ -0,0 +1,25 @@
/**
* File: insertion_sort.js
* Created Time: 2022-12-01
* Author: IsChristina (christinaxia77@foxmail.com)
*/
/* Insertion sort */
function insertionSort(nums) {
// Outer loop: sorted interval is [0, i-1]
for (let i = 1; i < nums.length; i++) {
let base = nums[i],
j = i - 1;
// Inner loop: insert base into the correct position within the sorted interval [0, i-1]
while (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
}
}
/* Driver Code */
const nums = [4, 1, 3, 1, 5, 2];
insertionSort(nums);
console.log('After insertion sort, nums =', nums);

View File

@@ -0,0 +1,52 @@
/**
* File: merge_sort.js
* Created Time: 2022-12-01
* Author: IsChristina (christinaxia77@foxmail.com)
*/
/* Merge left subarray and right subarray */
function merge(nums, left, mid, right) {
// Left subarray interval is [left, mid], right subarray interval is [mid+1, right]
// Create a temporary array tmp to store the merged results
const tmp = new Array(right - left + 1);
// Initialize the start indices of the left and right subarrays
let i = left,
j = mid + 1,
k = 0;
// While both subarrays still have elements, compare and copy the smaller element into the temporary array
while (i <= mid && j <= right) {
if (nums[i] <= nums[j]) {
tmp[k++] = nums[i++];
} else {
tmp[k++] = nums[j++];
}
}
// Copy the remaining elements of the left and right subarrays into the temporary array
while (i <= mid) {
tmp[k++] = nums[i++];
}
while (j <= right) {
tmp[k++] = nums[j++];
}
// Copy the elements from the temporary array tmp back to the original array nums at the corresponding interval
for (k = 0; k < tmp.length; k++) {
nums[left + k] = tmp[k];
}
}
/* Merge sort */
function mergeSort(nums, left, right) {
// Termination condition
if (left >= right) return; // Terminate recursion when subarray length is 1
// Divide and conquer stage
let mid = Math.floor(left + (right - left) / 2); // Calculate midpoint
mergeSort(nums, left, mid); // Recursively process the left subarray
mergeSort(nums, mid + 1, right); // Recursively process the right subarray
// Merge stage
merge(nums, left, mid, right);
}
/* Driver Code */
const nums = [7, 3, 2, 6, 0, 1, 5, 4];
mergeSort(nums, 0, nums.length - 1);
console.log('After merge sort, nums =', nums);

View File

@@ -0,0 +1,161 @@
/**
* File: quick_sort.js
* Created Time: 2022-12-01
* Author: IsChristina (christinaxia77@foxmail.com)
*/
/* Quick sort class */
class QuickSort {
/* Swap elements */
swap(nums, i, j) {
let tmp = nums[i];
nums[i] = nums[j];
nums[j] = tmp;
}
/* Sentinel partition */
partition(nums, left, right) {
// Use nums[left] as the pivot
let i = left,
j = right;
while (i < j) {
while (i < j && nums[j] >= nums[left]) {
j -= 1; // Search from right to left for the first element smaller than the pivot
}
while (i < j && nums[i] <= nums[left]) {
i += 1; // Search from left to right for the first element greater than the pivot
}
// Swap elements
this.swap(nums, i, j); // Swap these two elements
}
this.swap(nums, i, left); // Swap the pivot to the boundary between the two subarrays
return i; // Return the index of the pivot
}
/* Quick sort */
quickSort(nums, left, right) {
// Terminate recursion when subarray length is 1
if (left >= right) return;
// Sentinel partition
const pivot = this.partition(nums, left, right);
// Recursively process the left subarray and right subarray
this.quickSort(nums, left, pivot - 1);
this.quickSort(nums, pivot + 1, right);
}
}
/* Quick sort class (median pivot optimization) */
class QuickSortMedian {
/* Swap elements */
swap(nums, i, j) {
let tmp = nums[i];
nums[i] = nums[j];
nums[j] = tmp;
}
/* Select the median of three candidate elements */
medianThree(nums, left, mid, right) {
let l = nums[left],
m = nums[mid],
r = nums[right];
// m is between l and r
if ((l <= m && m <= r) || (r <= m && m <= l)) return mid;
// l is between m and r
if ((m <= l && l <= r) || (r <= l && l <= m)) return left;
return right;
}
/* Sentinel partition (median of three) */
partition(nums, left, right) {
// Select the median of three candidate elements
let med = this.medianThree(
nums,
left,
Math.floor((left + right) / 2),
right
);
// Swap the median to the array's leftmost position
this.swap(nums, left, med);
// Use nums[left] as the pivot
let i = left,
j = right;
while (i < j) {
while (i < j && nums[j] >= nums[left]) j--; // Search from right to left for the first element smaller than the pivot
while (i < j && nums[i] <= nums[left]) i++; // Search from left to right for the first element greater than the pivot
this.swap(nums, i, j); // Swap these two elements
}
this.swap(nums, i, left); // Swap the pivot to the boundary between the two subarrays
return i; // Return the index of the pivot
}
/* Quick sort */
quickSort(nums, left, right) {
// Terminate recursion when subarray length is 1
if (left >= right) return;
// Sentinel partition
const pivot = this.partition(nums, left, right);
// Recursively process the left subarray and right subarray
this.quickSort(nums, left, pivot - 1);
this.quickSort(nums, pivot + 1, right);
}
}
/* Quick sort class (recursion depth optimization) */
class QuickSortTailCall {
/* Swap elements */
swap(nums, i, j) {
let tmp = nums[i];
nums[i] = nums[j];
nums[j] = tmp;
}
/* Sentinel partition */
partition(nums, left, right) {
// Use nums[left] as the pivot
let i = left,
j = right;
while (i < j) {
while (i < j && nums[j] >= nums[left]) j--; // Search from right to left for the first element smaller than the pivot
while (i < j && nums[i] <= nums[left]) i++; // Search from left to right for the first element greater than the pivot
this.swap(nums, i, j); // Swap these two elements
}
this.swap(nums, i, left); // Swap the pivot to the boundary between the two subarrays
return i; // Return the index of the pivot
}
/* Quick sort (recursion depth optimization) */
quickSort(nums, left, right) {
// Terminate when subarray length is 1
while (left < right) {
// Sentinel partition operation
let pivot = this.partition(nums, left, right);
// Perform quick sort on the shorter of the two subarrays
if (pivot - left < right - pivot) {
this.quickSort(nums, left, pivot - 1); // Recursively sort the left subarray
left = pivot + 1; // Remaining unsorted interval is [pivot + 1, right]
} else {
this.quickSort(nums, pivot + 1, right); // Recursively sort the right subarray
right = pivot - 1; // Remaining unsorted interval is [left, pivot - 1]
}
}
}
}
/* Driver Code */
/* Quick sort */
const nums = [2, 4, 1, 0, 3, 5];
const quickSort = new QuickSort();
quickSort.quickSort(nums, 0, nums.length - 1);
console.log('After quick sort, nums =', nums);
/* Quick sort (recursion depth optimization) */
const nums1 = [2, 4, 1, 0, 3, 5];
const quickSortMedian = new QuickSortMedian();
quickSortMedian.quickSort(nums1, 0, nums1.length - 1);
console.log('After quick sort (median pivot optimization), nums =', nums1);
/* Quick sort (recursion depth optimization) */
const nums2 = [2, 4, 1, 0, 3, 5];
const quickSortTailCall = new QuickSortTailCall();
quickSortTailCall.quickSort(nums2, 0, nums2.length - 1);
console.log('After quick sort (recursion depth optimization), nums =', nums2);

View File

@@ -0,0 +1,61 @@
/**
* File: radix_sort.js
* Created Time: 2023-04-08
* Author: Justin (xiefahit@gmail.com)
*/
/* Get the k-th digit of element num, where exp = 10^(k-1) */
function digit(num, exp) {
// Passing exp instead of k can avoid repeated expensive exponentiation here
return Math.floor(num / exp) % 10;
}
/* Counting sort (based on nums k-th digit) */
function countingSortDigit(nums, exp) {
// Decimal digit range is 0~9, therefore need a bucket array of length 10
const counter = new Array(10).fill(0);
const n = nums.length;
// Count the occurrence of digits 0~9
for (let i = 0; i < n; i++) {
const 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 (let i = 1; i < 10; i++) {
counter[i] += counter[i - 1];
}
// Traverse in reverse, based on bucket statistics, place each element into res
const res = new Array(n).fill(0);
for (let i = n - 1; i >= 0; i--) {
const d = digit(nums[i], exp);
const 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 (let i = 0; i < n; i++) {
nums[i] = res[i];
}
}
/* Radix sort */
function radixSort(nums) {
// Get the maximum element of the array, used to determine the maximum number of digits
let m = Math.max(... nums);
// Traverse from the lowest to the highest digit
for (let exp = 1; exp <= m; 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);
}
}
/* Driver Code */
const nums = [
10546151, 35663510, 42865989, 34862445, 81883077, 88906420, 72429244,
30524779, 82060337, 63832996,
];
radixSort(nums);
console.log('After radix sort, nums =', nums);

View File

@@ -0,0 +1,27 @@
/**
* File: selection_sort.js
* Created Time: 2023-06-04
* Author: Justin (xiefahit@gmail.com)
*/
/* Selection sort */
function selectionSort(nums) {
let n = nums.length;
// Outer loop: unsorted interval is [i, n-1]
for (let i = 0; i < n - 1; i++) {
// Inner loop: find the smallest element within the unsorted interval
let k = i;
for (let j = i + 1; j < n; j++) {
if (nums[j] < nums[k]) {
k = j; // Record the index of the smallest element
}
}
// Swap the smallest element with the first element of the unsorted interval
[nums[i], nums[k]] = [nums[k], nums[i]];
}
}
/* Driver Code */
const nums = [4, 1, 3, 1, 5, 2];
selectionSort(nums);
console.log('After selection sort, nums =', nums);