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

@@ -10,33 +10,33 @@
void bubbleSort(vector<int> &nums) {
// Outer loop: unsorted range is [0, i]
for (int i = nums.size() - 1; i > 0; i--) {
// Inner loop: swap the largest element in the unsorted range [0, i] to the right end of the range
// Inner loop: swap the largest element in the unsorted range [0, i] to the rightmost end of that range
for (int j = 0; j < i; j++) {
if (nums[j] > nums[j + 1]) {
// Swap nums[j] and nums[j + 1]
// Here, the std
// Using std::swap() function here
swap(nums[j], nums[j + 1]);
}
}
}
}
/* Bubble sort (optimized with flag)*/
/* Bubble sort (flag optimization)*/
void bubbleSortWithFlag(vector<int> &nums) {
// Outer loop: unsorted range is [0, i]
for (int i = nums.size() - 1; i > 0; i--) {
bool flag = false; // Initialize flag
// Inner loop: swap the largest element in the unsorted range [0, i] to the right end of the range
// Inner loop: swap the largest element in the unsorted range [0, i] to the rightmost end of that range
for (int j = 0; j < i; j++) {
if (nums[j] > nums[j + 1]) {
// Swap nums[j] and nums[j + 1]
// Here, the std
// Using std::swap() function here
swap(nums[j], nums[j + 1]);
flag = true; // Record swapped elements
flag = true; // Record element swap
}
}
if (!flag)
break; // If no elements were swapped in this round of "bubbling", exit
break; // No elements were swapped in this round of "bubbling", exit directly
}
}
@@ -44,12 +44,12 @@ void bubbleSortWithFlag(vector<int> &nums) {
int main() {
vector<int> nums = {4, 1, 3, 1, 5, 2};
bubbleSort(nums);
cout << "After bubble sort, nums = ";
cout << "After bubble sort completes, nums = ";
printVector(nums);
vector<int> nums1 = {4, 1, 3, 1, 5, 2};
bubbleSortWithFlag(nums1);
cout << "After bubble sort, nums1 = ";
cout << "After bubble sort completes, nums1 = ";
printVector(nums1);
return 0;

View File

@@ -15,7 +15,7 @@ void bucketSort(vector<float> &nums) {
for (float num : nums) {
// Input data range is [0, 1), use num * k to map to index range [0, k-1]
int i = num * k;
// Add number to bucket_idx
// Add num to bucket bucket_idx
buckets[i].push_back(num);
}
// 2. Sort each bucket
@@ -34,10 +34,10 @@ void bucketSort(vector<float> &nums) {
/* Driver Code */
int main() {
// Assume input data is floating point, range [0, 1)
// Assume input data is floating point, interval [0, 1)
vector<float> nums = {0.49f, 0.96f, 0.82f, 0.09f, 0.57f, 0.43f, 0.91f, 0.75f, 0.15f, 0.37f};
bucketSort(nums);
cout << "After bucket sort, nums = ";
cout << "After bucket sort completes, nums = ";
printVector(nums);
return 0;

View File

@@ -14,7 +14,7 @@ void countingSortNaive(vector<int> &nums) {
for (int num : nums) {
m = max(m, num);
}
// 2. Count the occurrence of each digit
// 2. Count the occurrence of each number
// counter[num] represents the occurrence of num
vector<int> counter(m + 1, 0);
for (int num : nums) {
@@ -37,7 +37,7 @@ void countingSort(vector<int> &nums) {
for (int num : nums) {
m = max(m, num);
}
// 2. Count the occurrence of each digit
// 2. Count the occurrence of each number
// counter[num] represents the occurrence of num
vector<int> counter(m + 1, 0);
for (int num : nums) {
@@ -65,12 +65,12 @@ void countingSort(vector<int> &nums) {
int main() {
vector<int> nums = {1, 0, 1, 2, 0, 4, 0, 2, 2, 4};
countingSortNaive(nums);
cout << "After count sort (unable to sort objects), nums = ";
cout << "After counting sort (cannot sort objects) completes, nums = ";
printVector(nums);
vector<int> nums1 = {1, 0, 1, 2, 0, 4, 0, 2, 2, 4};
countingSort(nums1);
cout << "After count sort, nums1 = ";
cout << "After counting sort completes, nums1 = ";
printVector(nums1);
return 0;

View File

@@ -9,7 +9,7 @@
/* Heap length is n, start heapifying node i, from top to bottom */
void siftDown(vector<int> &nums, int n, int i) {
while (true) {
// Determine the largest node among i, l, r, noted as ma
// If node i is largest or indices l, r are out of bounds, no need to continue heapify, break
int l = 2 * i + 1;
int r = 2 * i + 2;
int ma = i;
@@ -17,7 +17,7 @@ void siftDown(vector<int> &nums, int n, int i) {
ma = l;
if (r < n && nums[r] > nums[ma])
ma = r;
// If node i is the largest or indices l, r are out of bounds, no further heapification needed, break
// Swap two nodes
if (ma == i) {
break;
}
@@ -36,7 +36,7 @@ void heapSort(vector<int> &nums) {
}
// Extract the largest element from the heap and repeat for n-1 rounds
for (int i = nums.size() - 1; i > 0; --i) {
// Swap the root node with the rightmost leaf node (swap the first element with the last element)
// Delete node
swap(nums[0], nums[i]);
// Start heapifying the root node, from top to bottom
siftDown(nums, i, 0);
@@ -47,7 +47,7 @@ void heapSort(vector<int> &nums) {
int main() {
vector<int> nums = {4, 1, 3, 1, 5, 2};
heapSort(nums);
cout << "After heap sort, nums = ";
cout << "After heap sort completes, nums = ";
printVector(nums);
return 0;

View File

@@ -8,10 +8,10 @@
/* Insertion sort */
void insertionSort(vector<int> &nums) {
// Outer loop: sorted range is [0, i-1]
// Outer loop: sorted interval is [0, i-1]
for (int i = 1; i < nums.size(); i++) {
int base = nums[i], j = i - 1;
// Inner loop: insert base into the correct position within the sorted range [0, 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--;
@@ -24,7 +24,7 @@ void insertionSort(vector<int> &nums) {
int main() {
vector<int> nums = {4, 1, 3, 1, 5, 2};
insertionSort(nums);
cout << "After insertion sort, nums = ";
cout << "After insertion sort completes, nums = ";
printVector(nums);
return 0;

View File

@@ -38,7 +38,7 @@ void mergeSort(vector<int> &nums, int left, int right) {
// Termination condition
if (left >= right)
return; // Terminate recursion when subarray length is 1
// Partition stage
// Divide and conquer stage
int mid = 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
@@ -51,7 +51,7 @@ int main() {
/* Merge sort */
vector<int> nums = {7, 3, 2, 6, 0, 1, 5, 4};
mergeSort(nums, 0, nums.size() - 1);
cout << "After merge sort, nums = ";
cout << "After merge sort completes, nums = ";
printVector(nums);
return 0;

View File

@@ -9,26 +9,19 @@
/* Quick sort class */
class QuickSort {
private:
/* Swap elements */
static void swap(vector<int> &nums, int i, int j) {
int tmp = nums[i];
nums[i] = nums[j];
nums[j] = tmp;
}
/* Partition */
/* Sentinel partition */
static int partition(vector<int> &nums, int left, int right) {
// Use nums[left] as the pivot
int 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
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
swap(nums, i, j); // Swap these two elements
i++; // Search from left to right for the first element greater than the pivot
swap(nums[i], nums[j]); // Swap these two elements
}
swap(nums, i, left); // Swap the pivot to the boundary between the two subarrays
return i; // Return the index of the pivot
swap(nums[i], nums[left]); // Swap the pivot to the boundary between the two subarrays
return i; // Return the index of the pivot
}
public:
@@ -37,7 +30,7 @@ class QuickSort {
// Terminate recursion when subarray length is 1
if (left >= right)
return;
// Partition
// Sentinel partition
int pivot = partition(nums, left, right);
// Recursively process the left subarray and right subarray
quickSort(nums, left, pivot - 1);
@@ -48,13 +41,6 @@ class QuickSort {
/* Quick sort class (median pivot optimization) */
class QuickSortMedian {
private:
/* Swap elements */
static void swap(vector<int> &nums, int i, int j) {
int tmp = nums[i];
nums[i] = nums[j];
nums[j] = tmp;
}
/* Select the median of three candidate elements */
static int medianThree(vector<int> &nums, int left, int mid, int right) {
int l = nums[left], m = nums[mid], r = nums[right];
@@ -65,23 +51,23 @@ class QuickSortMedian {
return right;
}
/* Partition (median of three) */
/* Sentinel partition (median of three) */
static int partition(vector<int> &nums, int left, int right) {
// Select the median of three candidate elements
int med = medianThree(nums, left, (left + right) / 2, right);
// Swap the median to the array's leftmost position
swap(nums, left, med);
swap(nums[left], nums[med]);
// Use nums[left] as the pivot
int 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
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
swap(nums, i, j); // Swap these two elements
i++; // Search from left to right for the first element greater than the pivot
swap(nums[i], nums[j]); // Swap these two elements
}
swap(nums, i, left); // Swap the pivot to the boundary between the two subarrays
return i; // Return the index of the pivot
swap(nums[i], nums[left]); // Swap the pivot to the boundary between the two subarrays
return i; // Return the index of the pivot
}
public:
@@ -90,7 +76,7 @@ class QuickSortMedian {
// Terminate recursion when subarray length is 1
if (left >= right)
return;
// Partition
// Sentinel partition
int pivot = partition(nums, left, right);
// Recursively process the left subarray and right subarray
quickSort(nums, left, pivot - 1);
@@ -98,37 +84,30 @@ class QuickSortMedian {
}
};
/* Quick sort class (tail recursion optimization) */
/* Quick sort class (recursion depth optimization) */
class QuickSortTailCall {
private:
/* Swap elements */
static void swap(vector<int> &nums, int i, int j) {
int tmp = nums[i];
nums[i] = nums[j];
nums[j] = tmp;
}
/* Partition */
/* Sentinel partition */
static int partition(vector<int> &nums, int left, int right) {
// Use nums[left] as the pivot
int 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
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
swap(nums, i, j); // Swap these two elements
i++; // Search from left to right for the first element greater than the pivot
swap(nums[i], nums[j]); // Swap these two elements
}
swap(nums, i, left); // Swap the pivot to the boundary between the two subarrays
return i; // Return the index of the pivot
swap(nums[i], nums[left]); // Swap the pivot to the boundary between the two subarrays
return i; // Return the index of the pivot
}
public:
/* Quick sort (tail recursion optimization) */
/* Quick sort (recursion depth optimization) */
static void quickSort(vector<int> &nums, int left, int right) {
// Terminate when subarray length is 1
while (left < right) {
// Partition operation
// Sentinel partition operation
int pivot = partition(nums, left, right);
// Perform quick sort on the shorter of the two subarrays
if (pivot - left < right - pivot) {
@@ -147,19 +126,19 @@ int main() {
/* Quick sort */
vector<int> nums{2, 4, 1, 0, 3, 5};
QuickSort::quickSort(nums, 0, nums.size() - 1);
cout << "After quick sort, nums = ";
cout << "After quick sort completes, nums = ";
printVector(nums);
/* Quick sort (median pivot optimization) */
/* Quick sort (recursion depth optimization) */
vector<int> nums1 = {2, 4, 1, 0, 3, 5};
QuickSortMedian::quickSort(nums1, 0, nums1.size() - 1);
cout << "Quick sort (median pivot optimization) completed, nums = ";
cout << "After quick sort (median pivot optimization), nums = ";
printVector(nums1);
/* Quick sort (tail recursion optimization) */
/* Quick sort (recursion depth optimization) */
vector<int> nums2 = {2, 4, 1, 0, 3, 5};
QuickSortTailCall::quickSort(nums2, 0, nums2.size() - 1);
cout << "Quick sort (tail recursion optimization) completed, nums = ";
cout << "After quick sort (recursion depth optimization), nums = ";
printVector(nums2);
return 0;

View File

@@ -58,7 +58,7 @@ int main() {
vector<int> nums = {10546151, 35663510, 42865989, 34862445, 81883077,
88906420, 72429244, 30524779, 82060337, 63832996};
radixSort(nums);
cout << "After radix sort, nums = ";
cout << "After radix sort completes, nums = ";
printVector(nums);
return 0;

View File

@@ -9,15 +9,15 @@
/* Selection sort */
void selectionSort(vector<int> &nums) {
int n = nums.size();
// Outer loop: unsorted range is [i, n-1]
// Outer loop: unsorted interval is [i, n-1]
for (int i = 0; i < n - 1; i++) {
// Inner loop: find the smallest element within the unsorted range
// Inner loop: find the smallest element within the unsorted interval
int k = i;
for (int 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 range
// Swap the smallest element with the first element of the unsorted interval
swap(nums[i], nums[k]);
}
}
@@ -27,7 +27,7 @@ int main() {
vector<int> nums = {4, 1, 3, 1, 5, 2};
selectionSort(nums);
cout << "After selection sort, nums = ";
cout << "After selection sort completes, nums = ";
printVector(nums);
return 0;