mirror of
https://github.com/krahets/hello-algo.git
synced 2026-04-15 02:40:58 +08:00
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:
@@ -13,7 +13,7 @@ public class bubble_sort {
|
||||
static void bubbleSort(int[] nums) {
|
||||
// Outer loop: unsorted range is [0, i]
|
||||
for (int i = nums.length - 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]
|
||||
@@ -25,33 +25,33 @@ public class bubble_sort {
|
||||
}
|
||||
}
|
||||
|
||||
/* Bubble sort (optimized with flag) */
|
||||
/* Bubble sort (flag optimization) */
|
||||
static void bubbleSortWithFlag(int[] nums) {
|
||||
// Outer loop: unsorted range is [0, i]
|
||||
for (int i = nums.length - 1; i > 0; i--) {
|
||||
boolean 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]
|
||||
int tmp = nums[j];
|
||||
nums[j] = nums[j + 1];
|
||||
nums[j + 1] = tmp;
|
||||
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
|
||||
}
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
int[] nums = { 4, 1, 3, 1, 5, 2 };
|
||||
bubbleSort(nums);
|
||||
System.out.println("After bubble sort, nums = " + Arrays.toString(nums));
|
||||
System.out.println("After bubble sort completes, nums = " + Arrays.toString(nums));
|
||||
|
||||
int[] nums1 = { 4, 1, 3, 1, 5, 2 };
|
||||
bubbleSortWithFlag(nums1);
|
||||
System.out.println("After bubble sort, nums1 = " + Arrays.toString(nums1));
|
||||
System.out.println("After bubble sort completes, nums1 = " + Arrays.toString(nums1));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -39,9 +39,9 @@ public class bucket_sort {
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
// Assume input data is floating point, range [0, 1)
|
||||
// Assume input data is floating point, interval [0, 1)
|
||||
float[] nums = { 0.49f, 0.96f, 0.82f, 0.09f, 0.57f, 0.43f, 0.91f, 0.75f, 0.15f, 0.37f };
|
||||
bucketSort(nums);
|
||||
System.out.println("After bucket sort, nums = " + Arrays.toString(nums));
|
||||
System.out.println("After bucket sort completes, nums = " + Arrays.toString(nums));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -17,7 +17,7 @@ public class counting_sort {
|
||||
for (int num : nums) {
|
||||
m = Math.max(m, num);
|
||||
}
|
||||
// 2. Count the occurrence of each digit
|
||||
// 2. Count the occurrence of each number
|
||||
// counter[num] represents the occurrence of num
|
||||
int[] counter = new int[m + 1];
|
||||
for (int num : nums) {
|
||||
@@ -40,7 +40,7 @@ public class counting_sort {
|
||||
for (int num : nums) {
|
||||
m = Math.max(m, num);
|
||||
}
|
||||
// 2. Count the occurrence of each digit
|
||||
// 2. Count the occurrence of each number
|
||||
// counter[num] represents the occurrence of num
|
||||
int[] counter = new int[m + 1];
|
||||
for (int num : nums) {
|
||||
@@ -69,10 +69,10 @@ public class counting_sort {
|
||||
public static void main(String[] args) {
|
||||
int[] nums = { 1, 0, 1, 2, 0, 4, 0, 2, 2, 4 };
|
||||
countingSortNaive(nums);
|
||||
System.out.println("After count sort (unable to sort objects), nums = " + Arrays.toString(nums));
|
||||
System.out.println("After counting sort (cannot sort objects) completes, nums = " + Arrays.toString(nums));
|
||||
|
||||
int[] nums1 = { 1, 0, 1, 2, 0, 4, 0, 2, 2, 4 };
|
||||
countingSort(nums1);
|
||||
System.out.println("After count sort, nums1 = " + Arrays.toString(nums1));
|
||||
System.out.println("After counting sort completes, nums1 = " + Arrays.toString(nums1));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -12,7 +12,7 @@ public class heap_sort {
|
||||
/* Heap length is n, start heapifying node i, from top to bottom */
|
||||
public static void siftDown(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;
|
||||
@@ -20,7 +20,7 @@ public class heap_sort {
|
||||
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;
|
||||
// Swap two nodes
|
||||
@@ -40,7 +40,7 @@ public class heap_sort {
|
||||
}
|
||||
// Extract the largest element from the heap and repeat for n-1 rounds
|
||||
for (int i = nums.length - 1; i > 0; i--) {
|
||||
// Swap the root node with the rightmost leaf node (swap the first element with the last element)
|
||||
// Delete node
|
||||
int tmp = nums[0];
|
||||
nums[0] = nums[i];
|
||||
nums[i] = tmp;
|
||||
@@ -52,6 +52,6 @@ public class heap_sort {
|
||||
public static void main(String[] args) {
|
||||
int[] nums = { 4, 1, 3, 1, 5, 2 };
|
||||
heapSort(nums);
|
||||
System.out.println("After heap sort, nums = " + Arrays.toString(nums));
|
||||
System.out.println("After heap sort completes, nums = " + Arrays.toString(nums));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -11,10 +11,10 @@ import java.util.*;
|
||||
public class insertion_sort {
|
||||
/* Insertion sort */
|
||||
static void insertionSort(int[] nums) {
|
||||
// Outer loop: sorted range is [0, i-1]
|
||||
// Outer loop: sorted interval is [0, i-1]
|
||||
for (int i = 1; i < nums.length; 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--;
|
||||
@@ -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("After insertion sort, nums = " + Arrays.toString(nums));
|
||||
System.out.println("After insertion sort completes, nums = " + Arrays.toString(nums));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -41,7 +41,7 @@ public class merge_sort {
|
||||
// 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
|
||||
@@ -53,6 +53,6 @@ public class merge_sort {
|
||||
/* Merge sort */
|
||||
int[] nums = { 7, 3, 2, 6, 0, 1, 5, 4 };
|
||||
mergeSort(nums, 0, nums.length - 1);
|
||||
System.out.println("After merge sort, nums = " + Arrays.toString(nums));
|
||||
System.out.println("After merge sort completes, nums = " + Arrays.toString(nums));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -17,7 +17,7 @@ class QuickSort {
|
||||
nums[j] = tmp;
|
||||
}
|
||||
|
||||
/* Partition */
|
||||
/* Sentinel partition */
|
||||
static int partition(int[] nums, int left, int right) {
|
||||
// Use nums[left] as the pivot
|
||||
int i = left, j = right;
|
||||
@@ -37,7 +37,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);
|
||||
@@ -64,7 +64,7 @@ class QuickSortMedian {
|
||||
return right;
|
||||
}
|
||||
|
||||
/* Partition (median of three) */
|
||||
/* Sentinel partition (median of three) */
|
||||
static int partition(int[] nums, int left, int right) {
|
||||
// Select the median of three candidate elements
|
||||
int med = medianThree(nums, left, (left + right) / 2, right);
|
||||
@@ -88,7 +88,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);
|
||||
@@ -96,7 +96,7 @@ class QuickSortMedian {
|
||||
}
|
||||
}
|
||||
|
||||
/* Quick sort class (tail recursion optimization) */
|
||||
/* Quick sort class (recursion depth optimization) */
|
||||
class QuickSortTailCall {
|
||||
/* Swap elements */
|
||||
static void swap(int[] nums, int i, int j) {
|
||||
@@ -105,7 +105,7 @@ class QuickSortTailCall {
|
||||
nums[j] = tmp;
|
||||
}
|
||||
|
||||
/* Partition */
|
||||
/* Sentinel partition */
|
||||
static int partition(int[] nums, int left, int right) {
|
||||
// Use nums[left] as the pivot
|
||||
int i = left, j = right;
|
||||
@@ -120,11 +120,11 @@ class QuickSortTailCall {
|
||||
return i; // Return the index of the pivot
|
||||
}
|
||||
|
||||
/* Quick sort (tail recursion optimization) */
|
||||
/* Quick sort (recursion depth optimization) */
|
||||
public static void quickSort(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) {
|
||||
@@ -143,16 +143,16 @@ public class quick_sort {
|
||||
/* Quick sort */
|
||||
int[] nums = { 2, 4, 1, 0, 3, 5 };
|
||||
QuickSort.quickSort(nums, 0, nums.length - 1);
|
||||
System.out.println("After quick sort, nums = " + Arrays.toString(nums));
|
||||
System.out.println("After quick sort completes, nums = " + Arrays.toString(nums));
|
||||
|
||||
/* Quick sort (median pivot optimization) */
|
||||
/* Quick sort (recursion depth optimization) */
|
||||
int[] nums1 = { 2, 4, 1, 0, 3, 5 };
|
||||
QuickSortMedian.quickSort(nums1, 0, nums1.length - 1);
|
||||
System.out.println("After quick sort with median pivot optimization, nums1 = " + Arrays.toString(nums1));
|
||||
System.out.println("After quick sort (median pivot optimization) completes, nums1 = " + Arrays.toString(nums1));
|
||||
|
||||
/* Quick sort (tail recursion optimization) */
|
||||
/* Quick sort (recursion depth optimization) */
|
||||
int[] nums2 = { 2, 4, 1, 0, 3, 5 };
|
||||
QuickSortTailCall.quickSort(nums2, 0, nums2.length - 1);
|
||||
System.out.println("After quick sort with tail recursion optimization, nums2 = " + Arrays.toString(nums2));
|
||||
System.out.println("After quick sort (recursion depth optimization) completes, nums2 = " + Arrays.toString(nums2));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -64,6 +64,6 @@ public class radix_sort {
|
||||
int[] nums = { 10546151, 35663510, 42865989, 34862445, 81883077,
|
||||
88906420, 72429244, 30524779, 82060337, 63832996 };
|
||||
radixSort(nums);
|
||||
System.out.println("After radix sort, nums = " + Arrays.toString(nums));
|
||||
System.out.println("After radix sort completes, nums = " + Arrays.toString(nums));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -12,15 +12,15 @@ public class selection_sort {
|
||||
/* Selection sort */
|
||||
public static void selectionSort(int[] nums) {
|
||||
int n = nums.length;
|
||||
// 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
|
||||
int temp = nums[i];
|
||||
nums[i] = nums[k];
|
||||
nums[k] = temp;
|
||||
@@ -30,6 +30,6 @@ public class selection_sort {
|
||||
public static void main(String[] args) {
|
||||
int[] nums = { 4, 1, 3, 1, 5, 2 };
|
||||
selectionSort(nums);
|
||||
System.out.println("After selection sort, nums = " + Arrays.toString(nums));
|
||||
System.out.println("After selection sort completes, nums = " + Arrays.toString(nums));
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user