mirror of
https://github.com/krahets/hello-algo.git
synced 2026-04-08 05:01:01 +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:
@@ -8,9 +8,9 @@ Author: timi (xisunyy@163.com)
|
||||
def bubble_sort(nums: list[int]):
|
||||
"""Bubble sort"""
|
||||
n = len(nums)
|
||||
# Outer loop: unsorted range is [0, i]
|
||||
# Outer loop: unsorted interval is [0, i]
|
||||
for i in range(n - 1, 0, -1):
|
||||
# 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 interval [0, i] to the rightmost end of the interval
|
||||
for j in range(i):
|
||||
if nums[j] > nums[j + 1]:
|
||||
# Swap nums[j] and nums[j + 1]
|
||||
@@ -18,27 +18,27 @@ def bubble_sort(nums: list[int]):
|
||||
|
||||
|
||||
def bubble_sort_with_flag(nums: list[int]):
|
||||
"""Bubble sort (optimized with flag)"""
|
||||
"""Bubble sort (flag optimization)"""
|
||||
n = len(nums)
|
||||
# Outer loop: unsorted range is [0, i]
|
||||
# Outer loop: unsorted interval is [0, i]
|
||||
for i in range(n - 1, 0, -1):
|
||||
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 interval [0, i] to the rightmost end of the interval
|
||||
for j in range(i):
|
||||
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 swapped elements
|
||||
flag = True # Record element swap
|
||||
if not 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
|
||||
|
||||
|
||||
"""Driver Code"""
|
||||
if __name__ == "__main__":
|
||||
nums = [4, 1, 3, 1, 5, 2]
|
||||
bubble_sort(nums)
|
||||
print("Bubble sort completed nums =", nums)
|
||||
print("After bubble sort, nums =", nums)
|
||||
|
||||
nums1 = [4, 1, 3, 1, 5, 2]
|
||||
bubble_sort_with_flag(nums1)
|
||||
print("Bubble sort completed nums =", nums1)
|
||||
print("After bubble sort, nums =", nums1)
|
||||
|
||||
@@ -29,7 +29,7 @@ def bucket_sort(nums: list[float]):
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
# Assume input data is floating point, range [0, 1)
|
||||
# Assume input data is floating point, interval [0, 1)
|
||||
nums = [0.49, 0.96, 0.82, 0.09, 0.57, 0.43, 0.91, 0.75, 0.15, 0.37]
|
||||
bucket_sort(nums)
|
||||
print("Bucket sort completed nums =", nums)
|
||||
print("After bucket sort, nums =", nums)
|
||||
|
||||
@@ -12,7 +12,7 @@ def counting_sort_naive(nums: list[int]):
|
||||
m = 0
|
||||
for num in 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
|
||||
counter = [0] * (m + 1)
|
||||
for num in nums:
|
||||
@@ -30,7 +30,7 @@ def counting_sort(nums: list[int]):
|
||||
# Complete implementation, can sort objects and is a stable sort
|
||||
# 1. Count the maximum element m in the array
|
||||
m = max(nums)
|
||||
# 2. Count the occurrence of each digit
|
||||
# 2. Count the occurrence of each number
|
||||
# counter[num] represents the occurrence of num
|
||||
counter = [0] * (m + 1)
|
||||
for num in nums:
|
||||
@@ -57,8 +57,8 @@ if __name__ == "__main__":
|
||||
nums = [1, 0, 1, 2, 0, 4, 0, 2, 2, 4]
|
||||
|
||||
counting_sort_naive(nums)
|
||||
print(f"Counting sort (unable to sort objects) completed nums = {nums}")
|
||||
print(f"After counting sort (unable to sort objects), nums = {nums}")
|
||||
|
||||
nums1 = [1, 0, 1, 2, 0, 4, 0, 2, 2, 4]
|
||||
counting_sort(nums1)
|
||||
print(f"Counting sort completed nums1 = {nums1}")
|
||||
print(f"After counting sort, nums1 = {nums1}")
|
||||
|
||||
@@ -42,4 +42,4 @@ def heap_sort(nums: list[int]):
|
||||
if __name__ == "__main__":
|
||||
nums = [4, 1, 3, 1, 5, 2]
|
||||
heap_sort(nums)
|
||||
print("Heap sort completed nums =", nums)
|
||||
print("After heap sort, nums =", nums)
|
||||
|
||||
@@ -7,11 +7,11 @@ Author: timi (xisunyy@163.com)
|
||||
|
||||
def insertion_sort(nums: list[int]):
|
||||
"""Insertion sort"""
|
||||
# Outer loop: sorted range is [0, i-1]
|
||||
# Outer loop: sorted interval is [0, i-1]
|
||||
for i in range(1, len(nums)):
|
||||
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 and nums[j] > base:
|
||||
nums[j + 1] = nums[j] # Move nums[j] to the right by one position
|
||||
j -= 1
|
||||
@@ -22,4 +22,4 @@ def insertion_sort(nums: list[int]):
|
||||
if __name__ == "__main__":
|
||||
nums = [4, 1, 3, 1, 5, 2]
|
||||
insertion_sort(nums)
|
||||
print("Insertion sort completed nums =", nums)
|
||||
print("After insertion sort, nums =", nums)
|
||||
|
||||
@@ -40,8 +40,8 @@ def merge_sort(nums: list[int], left: int, right: int):
|
||||
# Termination condition
|
||||
if left >= right:
|
||||
return # Terminate recursion when subarray length is 1
|
||||
# Partition stage
|
||||
mid = left + (right - left) // 2 # Calculate midpoint
|
||||
# Divide and conquer stage
|
||||
mid = (left + right) // 2 # Calculate midpoint
|
||||
merge_sort(nums, left, mid) # Recursively process the left subarray
|
||||
merge_sort(nums, mid + 1, right) # Recursively process the right subarray
|
||||
# Merge stage
|
||||
@@ -52,4 +52,4 @@ def merge_sort(nums: list[int], left: int, right: int):
|
||||
if __name__ == "__main__":
|
||||
nums = [7, 3, 2, 6, 0, 1, 5, 4]
|
||||
merge_sort(nums, 0, len(nums) - 1)
|
||||
print("Merge sort completed nums =", nums)
|
||||
print("After merge sort, nums =", nums)
|
||||
|
||||
@@ -9,7 +9,7 @@ class QuickSort:
|
||||
"""Quick sort class"""
|
||||
|
||||
def partition(self, nums: list[int], left: int, right: int) -> int:
|
||||
"""Partition"""
|
||||
"""Sentinel partition"""
|
||||
# Use nums[left] as the pivot
|
||||
i, j = left, right
|
||||
while i < j:
|
||||
@@ -28,7 +28,7 @@ class QuickSort:
|
||||
# Terminate recursion when subarray length is 1
|
||||
if left >= right:
|
||||
return
|
||||
# Partition
|
||||
# Sentinel partition
|
||||
pivot = self.partition(nums, left, right)
|
||||
# Recursively process the left subarray and right subarray
|
||||
self.quick_sort(nums, left, pivot - 1)
|
||||
@@ -48,7 +48,7 @@ class QuickSortMedian:
|
||||
return right
|
||||
|
||||
def partition(self, nums: list[int], left: int, right: int) -> int:
|
||||
"""Partition (median of three)"""
|
||||
"""Sentinel partition (median of three)"""
|
||||
# Use nums[left] as the pivot
|
||||
med = self.median_three(nums, left, (left + right) // 2, right)
|
||||
# Swap the median to the array's leftmost position
|
||||
@@ -71,7 +71,7 @@ class QuickSortMedian:
|
||||
# Terminate recursion when subarray length is 1
|
||||
if left >= right:
|
||||
return
|
||||
# Partition
|
||||
# Sentinel partition
|
||||
pivot = self.partition(nums, left, right)
|
||||
# Recursively process the left subarray and right subarray
|
||||
self.quick_sort(nums, left, pivot - 1)
|
||||
@@ -79,10 +79,10 @@ class QuickSortMedian:
|
||||
|
||||
|
||||
class QuickSortTailCall:
|
||||
"""Quick sort class (tail recursion optimization)"""
|
||||
"""Quick sort class (recursion depth optimization)"""
|
||||
|
||||
def partition(self, nums: list[int], left: int, right: int) -> int:
|
||||
"""Partition"""
|
||||
"""Sentinel partition"""
|
||||
# Use nums[left] as the pivot
|
||||
i, j = left, right
|
||||
while i < j:
|
||||
@@ -97,10 +97,10 @@ class QuickSortTailCall:
|
||||
return i # Return the index of the pivot
|
||||
|
||||
def quick_sort(self, nums: list[int], left: int, right: int):
|
||||
"""Quick sort (tail recursion optimization)"""
|
||||
"""Quick sort (recursion depth optimization)"""
|
||||
# Terminate when subarray length is 1
|
||||
while left < right:
|
||||
# Partition operation
|
||||
# Sentinel partition operation
|
||||
pivot = self.partition(nums, left, right)
|
||||
# Perform quick sort on the shorter of the two subarrays
|
||||
if pivot - left < right - pivot:
|
||||
@@ -116,14 +116,14 @@ if __name__ == "__main__":
|
||||
# Quick sort
|
||||
nums = [2, 4, 1, 0, 3, 5]
|
||||
QuickSort().quick_sort(nums, 0, len(nums) - 1)
|
||||
print("Quick sort completed nums =", nums)
|
||||
print("After quick sort, nums =", nums)
|
||||
|
||||
# Quick sort (median pivot optimization)
|
||||
nums1 = [2, 4, 1, 0, 3, 5]
|
||||
QuickSortMedian().quick_sort(nums1, 0, len(nums1) - 1)
|
||||
print("Quick sort (median pivot optimization) completed nums =", nums1)
|
||||
print("After quick sort (median pivot optimization), nums =", nums1)
|
||||
|
||||
# Quick sort (tail recursion optimization)
|
||||
# Quick sort (recursion depth optimization)
|
||||
nums2 = [2, 4, 1, 0, 3, 5]
|
||||
QuickSortTailCall().quick_sort(nums2, 0, len(nums2) - 1)
|
||||
print("Quick sort (tail recursion optimization) completed nums =", nums2)
|
||||
print("After quick sort (recursion depth optimization), nums =", nums2)
|
||||
|
||||
@@ -66,4 +66,4 @@ if __name__ == "__main__":
|
||||
63832996,
|
||||
]
|
||||
radix_sort(nums)
|
||||
print("Radix sort completed nums =", nums)
|
||||
print("After radix sort, nums =", nums)
|
||||
|
||||
@@ -8,14 +8,14 @@ Author: krahets (krahets@163.com)
|
||||
def selection_sort(nums: list[int]):
|
||||
"""Selection sort"""
|
||||
n = len(nums)
|
||||
# Outer loop: unsorted range is [i, n-1]
|
||||
# Outer loop: unsorted interval is [i, n-1]
|
||||
for i in range(n - 1):
|
||||
# Inner loop: find the smallest element within the unsorted range
|
||||
# Inner loop: find the smallest element within the unsorted interval
|
||||
k = i
|
||||
for j in range(i + 1, n):
|
||||
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
|
||||
nums[i], nums[k] = nums[k], nums[i]
|
||||
|
||||
|
||||
@@ -23,4 +23,4 @@ def selection_sort(nums: list[int]):
|
||||
if __name__ == "__main__":
|
||||
nums = [4, 1, 3, 1, 5, 2]
|
||||
selection_sort(nums)
|
||||
print("Selection sort completed nums =", nums)
|
||||
print("After selection sort, nums =", nums)
|
||||
|
||||
Reference in New Issue
Block a user