mirror of
https://github.com/krahets/hello-algo.git
synced 2026-08-21 15:57:14 +00: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:
@@ -0,0 +1,53 @@
|
||||
/**
|
||||
* File: bubble_sort.kt
|
||||
* Created Time: 2024-01-25
|
||||
* Author: curtishd (1023632660@qq.com)
|
||||
*/
|
||||
|
||||
package chapter_sorting
|
||||
|
||||
/* Bubble sort */
|
||||
fun bubbleSort(nums: IntArray) {
|
||||
// Outer loop: unsorted range is [0, i]
|
||||
for (i in nums.size - 1 downTo 1) {
|
||||
// Inner loop: swap the largest element in the unsorted range [0, i] to the rightmost end of that range
|
||||
for (j in 0..<i) {
|
||||
if (nums[j] > nums[j + 1]) {
|
||||
// Swap nums[j] and nums[j + 1]
|
||||
val temp = nums[j]
|
||||
nums[j] = nums[j + 1]
|
||||
nums[j + 1] = temp
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* Bubble sort (flag optimization) */
|
||||
fun bubbleSortWithFlag(nums: IntArray) {
|
||||
// Outer loop: unsorted range is [0, i]
|
||||
for (i in nums.size - 1 downTo 1) {
|
||||
var 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 in 0..<i) {
|
||||
if (nums[j] > nums[j + 1]) {
|
||||
// Swap nums[j] and nums[j + 1]
|
||||
val temp = nums[j]
|
||||
nums[j] = nums[j + 1]
|
||||
nums[j + 1] = temp
|
||||
flag = true // Record element swap
|
||||
}
|
||||
}
|
||||
if (!flag) break // No elements were swapped in this round of "bubbling", exit directly
|
||||
}
|
||||
}
|
||||
|
||||
/* Driver Code */
|
||||
fun main() {
|
||||
val nums = intArrayOf(4, 1, 3, 1, 5, 2)
|
||||
bubbleSort(nums)
|
||||
println("After bubble sort, nums = ${nums.contentToString()}")
|
||||
|
||||
val nums1 = intArrayOf(4, 1, 3, 1, 5, 2)
|
||||
bubbleSortWithFlag(nums1)
|
||||
println("After bubble sort, nums1 = ${nums1.contentToString()}")
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
/**
|
||||
* File: bucket_sort.kt
|
||||
* Created Time: 2024-01-25
|
||||
* Author: curtishd (1023632660@qq.com)
|
||||
*/
|
||||
|
||||
package chapter_sorting
|
||||
|
||||
/* Bucket sort */
|
||||
fun bucketSort(nums: FloatArray) {
|
||||
// Initialize k = n/2 buckets, expected to allocate 2 elements per bucket
|
||||
val k = nums.size / 2
|
||||
val buckets = mutableListOf<MutableList<Float>>()
|
||||
for (i in 0..<k) {
|
||||
buckets.add(mutableListOf())
|
||||
}
|
||||
// 1. Distribute array elements into various buckets
|
||||
for (num in nums) {
|
||||
// Input data range is [0, 1), use num * k to map to index range [0, k-1]
|
||||
val i = (num * k).toInt()
|
||||
// Add num to bucket i
|
||||
buckets[i].add(num)
|
||||
}
|
||||
// 2. Sort each bucket
|
||||
for (bucket in buckets) {
|
||||
// Use built-in sorting function, can also replace with other sorting algorithms
|
||||
bucket.sort()
|
||||
}
|
||||
// 3. Traverse buckets to merge results
|
||||
var i = 0
|
||||
for (bucket in buckets) {
|
||||
for (num in bucket) {
|
||||
nums[i++] = num
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* Driver Code */
|
||||
fun main() {
|
||||
// Assume input data is floating point, interval [0, 1)
|
||||
val nums = floatArrayOf(0.49f, 0.96f, 0.82f, 0.09f, 0.57f, 0.43f, 0.91f, 0.75f, 0.15f, 0.37f)
|
||||
bucketSort(nums)
|
||||
println("After bucket sort, nums = ${nums.contentToString()}")
|
||||
}
|
||||
@@ -0,0 +1,80 @@
|
||||
/**
|
||||
* File: counting_sort.kt
|
||||
* Created Time: 2024-01-25
|
||||
* Author: curtishd (1023632660@qq.com)
|
||||
*/
|
||||
|
||||
package chapter_sorting
|
||||
|
||||
import kotlin.math.max
|
||||
|
||||
/* Counting sort */
|
||||
// Simple implementation, cannot be used for sorting objects
|
||||
fun countingSortNaive(nums: IntArray) {
|
||||
// 1. Count the maximum element m in the array
|
||||
var m = 0
|
||||
for (num in nums) {
|
||||
m = max(m, num)
|
||||
}
|
||||
// 2. Count the occurrence of each number
|
||||
// counter[num] represents the occurrence of num
|
||||
val counter = IntArray(m + 1)
|
||||
for (num in nums) {
|
||||
counter[num]++
|
||||
}
|
||||
// 3. Traverse counter, filling each element back into the original array nums
|
||||
var i = 0
|
||||
for (num in 0..<m + 1) {
|
||||
var j = 0
|
||||
while (j < counter[num]) {
|
||||
nums[i] = num
|
||||
j++
|
||||
i++
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* Counting sort */
|
||||
// Complete implementation, can sort objects and is a stable sort
|
||||
fun countingSort(nums: IntArray) {
|
||||
// 1. Count the maximum element m in the array
|
||||
var m = 0
|
||||
for (num in nums) {
|
||||
m = max(m, num)
|
||||
}
|
||||
// 2. Count the occurrence of each number
|
||||
// counter[num] represents the occurrence of num
|
||||
val counter = IntArray(m + 1)
|
||||
for (num in 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 in 0..<m) {
|
||||
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
|
||||
val n = nums.size
|
||||
val res = IntArray(n)
|
||||
for (i in n - 1 downTo 0) {
|
||||
val 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 (i in 0..<n) {
|
||||
nums[i] = res[i]
|
||||
}
|
||||
}
|
||||
|
||||
/* Driver Code */
|
||||
fun main() {
|
||||
val nums = intArrayOf(1, 0, 1, 2, 0, 4, 0, 2, 2, 4)
|
||||
countingSortNaive(nums)
|
||||
println("After counting sort (cannot sort objects), nums = ${nums.contentToString()}")
|
||||
|
||||
val nums1 = intArrayOf(1, 0, 1, 2, 0, 4, 0, 2, 2, 4)
|
||||
countingSort(nums1)
|
||||
println("After counting sort, nums1 = ${nums1.contentToString()}")
|
||||
}
|
||||
@@ -0,0 +1,55 @@
|
||||
/**
|
||||
* File: heap_sort.kt
|
||||
* Created Time: 2024-01-25
|
||||
* Author: curtishd (1023632660@qq.com)
|
||||
*/
|
||||
|
||||
package chapter_sorting
|
||||
|
||||
/* Heap length is n, start heapifying node i, from top to bottom */
|
||||
fun siftDown(nums: IntArray, n: Int, li: Int) {
|
||||
var i = li
|
||||
while (true) {
|
||||
// If node i is largest or indices l, r are out of bounds, no need to continue heapify, break
|
||||
val l = 2 * i + 1
|
||||
val r = 2 * i + 2
|
||||
var 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
|
||||
val temp = nums[i]
|
||||
nums[i] = nums[ma]
|
||||
nums[ma] = temp
|
||||
// Loop downwards heapification
|
||||
i = ma
|
||||
}
|
||||
}
|
||||
|
||||
/* Heap sort */
|
||||
fun heapSort(nums: IntArray) {
|
||||
// Build heap operation: heapify all nodes except leaves
|
||||
for (i in nums.size / 2 - 1 downTo 0) {
|
||||
siftDown(nums, nums.size, i)
|
||||
}
|
||||
// Extract the largest element from the heap and repeat for n-1 rounds
|
||||
for (i in nums.size - 1 downTo 1) {
|
||||
// Delete node
|
||||
val temp = nums[0]
|
||||
nums[0] = nums[i]
|
||||
nums[i] = temp
|
||||
// Start heapifying the root node, from top to bottom
|
||||
siftDown(nums, i, 0)
|
||||
}
|
||||
}
|
||||
|
||||
/* Driver Code */
|
||||
fun main() {
|
||||
val nums = intArrayOf(4, 1, 3, 1, 5, 2)
|
||||
heapSort(nums)
|
||||
println("After heap sort, nums = ${nums.contentToString()}")
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
/**
|
||||
* File: insertion_sort.kt
|
||||
* Created Time: 2024-01-25
|
||||
* Author: curtishd (1023632660@qq.com)
|
||||
*/
|
||||
|
||||
package chapter_sorting
|
||||
|
||||
/* Insertion sort */
|
||||
fun insertionSort(nums: IntArray) {
|
||||
// Outer loop: sorted elements are 1, 2, ..., n
|
||||
for (i in nums.indices) {
|
||||
val base = nums[i]
|
||||
var 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 */
|
||||
fun main() {
|
||||
val nums = intArrayOf(4, 1, 3, 1, 5, 2)
|
||||
insertionSort(nums)
|
||||
println("After insertion sort, nums = ${nums.contentToString()}")
|
||||
}
|
||||
@@ -0,0 +1,56 @@
|
||||
/**
|
||||
* File: merge_sort.kt
|
||||
* Created Time: 2024-01-25
|
||||
* Author: curtishd (1023632660@qq.com)
|
||||
*/
|
||||
|
||||
package chapter_sorting
|
||||
|
||||
/* Merge left subarray and right subarray */
|
||||
fun merge(nums: IntArray, left: Int, mid: Int, 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
|
||||
val tmp = IntArray(right - left + 1)
|
||||
// Initialize the start indices of the left and right subarrays
|
||||
var i = left
|
||||
var j = mid + 1
|
||||
var 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 (l in tmp.indices) {
|
||||
nums[left + l] = tmp[l]
|
||||
}
|
||||
}
|
||||
|
||||
/* Merge sort */
|
||||
fun mergeSort(nums: IntArray, left: Int, right: Int) {
|
||||
// Termination condition
|
||||
if (left >= right) return // Terminate recursion when subarray length is 1
|
||||
// Divide and conquer stage
|
||||
val 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
|
||||
// Merge stage
|
||||
merge(nums, left, mid, right)
|
||||
}
|
||||
|
||||
/* Driver Code */
|
||||
fun main() {
|
||||
/* Merge sort */
|
||||
val nums = intArrayOf(7, 3, 2, 6, 0, 1, 5, 4)
|
||||
mergeSort(nums, 0, nums.size - 1)
|
||||
println("After merge sort, nums = ${nums.contentToString()}")
|
||||
}
|
||||
@@ -0,0 +1,121 @@
|
||||
/**
|
||||
* File: quick_sort.kt
|
||||
* Created Time: 2024-01-25
|
||||
* Author: curtishd (1023632660@qq.com)
|
||||
*/
|
||||
|
||||
package chapter_sorting
|
||||
|
||||
/* Swap elements */
|
||||
fun swap(nums: IntArray, i: Int, j: Int) {
|
||||
val temp = nums[i]
|
||||
nums[i] = nums[j]
|
||||
nums[j] = temp
|
||||
}
|
||||
|
||||
/* Sentinel partition */
|
||||
fun partition(nums: IntArray, left: Int, right: Int): Int {
|
||||
// Use nums[left] as the pivot
|
||||
var i = left
|
||||
var 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
|
||||
swap(nums, i, 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
|
||||
}
|
||||
|
||||
/* Quick sort */
|
||||
fun quickSort(nums: IntArray, left: Int, right: Int) {
|
||||
// Terminate recursion when subarray length is 1
|
||||
if (left >= right) return
|
||||
// Sentinel partition
|
||||
val pivot = partition(nums, left, right)
|
||||
// Recursively process the left subarray and right subarray
|
||||
quickSort(nums, left, pivot - 1)
|
||||
quickSort(nums, pivot + 1, right)
|
||||
}
|
||||
|
||||
/* Select the median of three candidate elements */
|
||||
fun medianThree(nums: IntArray, left: Int, mid: Int, right: Int): Int {
|
||||
val l = nums[left]
|
||||
val m = nums[mid]
|
||||
val r = nums[right]
|
||||
if ((m in l..r) || (m in r..l))
|
||||
return mid // m is between l and r
|
||||
if ((l in m..r) || (l in r..m))
|
||||
return left // l is between m and r
|
||||
return right
|
||||
}
|
||||
|
||||
/* Sentinel partition (median of three) */
|
||||
fun partitionMedian(nums: IntArray, left: Int, right: Int): Int {
|
||||
// Select the median of three candidate elements
|
||||
val med = medianThree(nums, left, (left + right) / 2, right)
|
||||
// Swap the median to the array's leftmost position
|
||||
swap(nums, left, med)
|
||||
// Use nums[left] as the pivot
|
||||
var i = left
|
||||
var 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
|
||||
swap(nums, i, 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
|
||||
}
|
||||
|
||||
/* Quick sort */
|
||||
fun quickSortMedian(nums: IntArray, left: Int, right: Int) {
|
||||
// Terminate recursion when subarray length is 1
|
||||
if (left >= right) return
|
||||
// Sentinel partition
|
||||
val pivot = partitionMedian(nums, left, right)
|
||||
// Recursively process the left subarray and right subarray
|
||||
quickSort(nums, left, pivot - 1)
|
||||
quickSort(nums, pivot + 1, right)
|
||||
}
|
||||
|
||||
/* Quick sort (recursion depth optimization) */
|
||||
fun quickSortTailCall(nums: IntArray, left: Int, right: Int) {
|
||||
// Terminate when subarray length is 1
|
||||
var l = left
|
||||
var r = right
|
||||
while (l < r) {
|
||||
// Sentinel partition operation
|
||||
val pivot = partition(nums, l, r)
|
||||
// Perform quick sort on the shorter of the two subarrays
|
||||
if (pivot - l < r - pivot) {
|
||||
quickSort(nums, l, pivot - 1) // Recursively sort the left subarray
|
||||
l = pivot + 1 // Remaining unsorted interval is [pivot + 1, right]
|
||||
} else {
|
||||
quickSort(nums, pivot + 1, r) // Recursively sort the right subarray
|
||||
r = pivot - 1 // Remaining unsorted interval is [left, pivot - 1]
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* Driver Code */
|
||||
fun main() {
|
||||
/* Quick sort */
|
||||
val nums = intArrayOf(2, 4, 1, 0, 3, 5)
|
||||
quickSort(nums, 0, nums.size - 1)
|
||||
println("After quick sort, nums = ${nums.contentToString()}")
|
||||
|
||||
/* Quick sort (recursion depth optimization) */
|
||||
val nums1 = intArrayOf(2, 4, 1, 0, 3, 5)
|
||||
quickSortMedian(nums1, 0, nums1.size - 1)
|
||||
println("After quick sort (median pivot optimization), nums1 = ${nums1.contentToString()}")
|
||||
|
||||
/* Quick sort (recursion depth optimization) */
|
||||
val nums2 = intArrayOf(2, 4, 1, 0, 3, 5)
|
||||
quickSortTailCall(nums2, 0, nums2.size - 1)
|
||||
println("After quick sort (recursion depth optimization), nums2 = ${nums2.contentToString()}")
|
||||
}
|
||||
@@ -0,0 +1,68 @@
|
||||
/**
|
||||
* File: radix_sort.kt
|
||||
* Created Time: 2024-01-25
|
||||
* Author: curtishd (1023632660@qq.com)
|
||||
*/
|
||||
|
||||
package chapter_sorting
|
||||
|
||||
/* Get the k-th digit of element num, where exp = 10^(k-1) */
|
||||
fun digit(num: Int, 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) */
|
||||
fun countingSortDigit(nums: IntArray, exp: Int) {
|
||||
// Decimal digit range is 0~9, therefore need a bucket array of length 10
|
||||
val counter = IntArray(10)
|
||||
val n = nums.size
|
||||
// Count the occurrence of digits 0~9
|
||||
for (i in 0..<n) {
|
||||
val 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 in 1..9) {
|
||||
counter[i] += counter[i - 1]
|
||||
}
|
||||
// Traverse in reverse, based on bucket statistics, place each element into res
|
||||
val res = IntArray(n)
|
||||
for (i in n - 1 downTo 0) {
|
||||
val d = digit(nums[i], exp)
|
||||
val 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 in 0..<n)
|
||||
nums[i] = res[i]
|
||||
}
|
||||
|
||||
/* Radix sort */
|
||||
fun radixSort(nums: IntArray) {
|
||||
// Get the maximum element of the array, used to determine the maximum number of digits
|
||||
var m = Int.MIN_VALUE
|
||||
for (num in nums) if (num > m) m = num
|
||||
var exp = 1
|
||||
// Traverse from the lowest to the highest digit
|
||||
while (exp <= m) {
|
||||
// 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)
|
||||
exp *= 10
|
||||
}
|
||||
}
|
||||
|
||||
/* Driver Code */
|
||||
fun main() {
|
||||
// Radix sort
|
||||
val nums = intArrayOf(
|
||||
10546151, 35663510, 42865989, 34862445, 81883077,
|
||||
88906420, 72429244, 30524779, 82060337, 63832996
|
||||
)
|
||||
radixSort(nums)
|
||||
println("After radix sort, nums = ${nums.contentToString()}")
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
/**
|
||||
* File: selection_sort.kt
|
||||
* Created Time: 2024-01-25
|
||||
* Author: curtishd (1023632660@qq.com)
|
||||
*/
|
||||
|
||||
package chapter_sorting
|
||||
|
||||
/* Selection sort */
|
||||
fun selectionSort(nums: IntArray) {
|
||||
val n = nums.size
|
||||
// Outer loop: unsorted interval is [i, n-1]
|
||||
for (i in 0..<n - 1) {
|
||||
var k = i
|
||||
// Inner loop: find the smallest element within the unsorted interval
|
||||
for (j in 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 interval
|
||||
val temp = nums[i]
|
||||
nums[i] = nums[k]
|
||||
nums[k] = temp
|
||||
}
|
||||
}
|
||||
|
||||
/* Driver Code */
|
||||
fun main() {
|
||||
val nums = intArrayOf(4, 1, 3, 1, 5, 2)
|
||||
selectionSort(nums)
|
||||
println("After selection sort, nums = ${nums.contentToString()}")
|
||||
}
|
||||
Reference in New Issue
Block a user