mirror of
https://github.com/krahets/hello-algo.git
synced 2026-08-13 20:20:58 +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,51 @@
|
||||
/**
|
||||
* File: bubble_sort.swift
|
||||
* Created Time: 2023-01-29
|
||||
* Author: nuomi1 (nuomi1@qq.com)
|
||||
*/
|
||||
|
||||
/* Bubble sort */
|
||||
func bubbleSort(nums: inout [Int]) {
|
||||
// Outer loop: unsorted range is [0, i]
|
||||
for i in nums.indices.dropFirst().reversed() {
|
||||
// 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]
|
||||
nums.swapAt(j, j + 1)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* Bubble sort (flag optimization) */
|
||||
func bubbleSortWithFlag(nums: inout [Int]) {
|
||||
// Outer loop: unsorted range is [0, i]
|
||||
for i in nums.indices.dropFirst().reversed() {
|
||||
var flag = false // Initialize flag
|
||||
for j in 0 ..< i {
|
||||
if nums[j] > nums[j + 1] {
|
||||
// Swap nums[j] and nums[j + 1]
|
||||
nums.swapAt(j, j + 1)
|
||||
flag = true // Record element swap
|
||||
}
|
||||
}
|
||||
if !flag { // No elements were swapped in this round of "bubbling", exit directly
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@main
|
||||
enum BubbleSort {
|
||||
/* Driver Code */
|
||||
static func main() {
|
||||
var nums = [4, 1, 3, 1, 5, 2]
|
||||
bubbleSort(nums: &nums)
|
||||
print("After bubble sort, nums = \(nums)")
|
||||
|
||||
var nums1 = [4, 1, 3, 1, 5, 2]
|
||||
bubbleSortWithFlag(nums: &nums1)
|
||||
print("After bubble sort, nums1 = \(nums1)")
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,43 @@
|
||||
/**
|
||||
* File: bucket_sort.swift
|
||||
* Created Time: 2023-03-27
|
||||
* Author: nuomi1 (nuomi1@qq.com)
|
||||
*/
|
||||
|
||||
/* Bucket sort */
|
||||
func bucketSort(nums: inout [Double]) {
|
||||
// Initialize k = n/2 buckets, expected to allocate 2 elements per bucket
|
||||
let k = nums.count / 2
|
||||
var buckets = (0 ..< k).map { _ in [Double]() }
|
||||
// 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]
|
||||
let i = Int(num * Double(k))
|
||||
// Add num to bucket i
|
||||
buckets[i].append(num)
|
||||
}
|
||||
// 2. Sort each bucket
|
||||
for i in buckets.indices {
|
||||
// Use built-in sorting function, can also replace with other sorting algorithms
|
||||
buckets[i].sort()
|
||||
}
|
||||
// 3. Traverse buckets to merge results
|
||||
var i = nums.startIndex
|
||||
for bucket in buckets {
|
||||
for num in bucket {
|
||||
nums[i] = num
|
||||
i += 1
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@main
|
||||
enum BucketSort {
|
||||
/* Driver Code */
|
||||
static func main() {
|
||||
// Assume input data is floating point, interval [0, 1)
|
||||
var nums = [0.49, 0.96, 0.82, 0.09, 0.57, 0.43, 0.91, 0.75, 0.15, 0.37]
|
||||
bucketSort(nums: &nums)
|
||||
print("After bucket sort, nums = \(nums)")
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,70 @@
|
||||
/**
|
||||
* File: counting_sort.swift
|
||||
* Created Time: 2023-03-22
|
||||
* Author: nuomi1 (nuomi1@qq.com)
|
||||
*/
|
||||
|
||||
/* Counting sort */
|
||||
// Simple implementation, cannot be used for sorting objects
|
||||
func countingSortNaive(nums: inout [Int]) {
|
||||
// 1. Count the maximum element m in the array
|
||||
let m = nums.max()!
|
||||
// 2. Count the occurrence of each number
|
||||
// counter[num] represents the occurrence of num
|
||||
var counter = Array(repeating: 0, count: m + 1)
|
||||
for num in nums {
|
||||
counter[num] += 1
|
||||
}
|
||||
// 3. Traverse counter, filling each element back into the original array nums
|
||||
var i = 0
|
||||
for num in 0 ..< m + 1 {
|
||||
for _ in 0 ..< counter[num] {
|
||||
nums[i] = num
|
||||
i += 1
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* Counting sort */
|
||||
// Complete implementation, can sort objects and is a stable sort
|
||||
func countingSort(nums: inout [Int]) {
|
||||
// 1. Count the maximum element m in the array
|
||||
let m = nums.max()!
|
||||
// 2. Count the occurrence of each number
|
||||
// counter[num] represents the occurrence of num
|
||||
var counter = Array(repeating: 0, count: m + 1)
|
||||
for num in nums {
|
||||
counter[num] += 1
|
||||
}
|
||||
// 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
|
||||
var res = Array(repeating: 0, count: nums.count)
|
||||
for i in nums.indices.reversed() {
|
||||
let num = nums[i]
|
||||
res[counter[num] - 1] = num // Place num at the corresponding index
|
||||
counter[num] -= 1 // 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 nums.indices {
|
||||
nums[i] = res[i]
|
||||
}
|
||||
}
|
||||
|
||||
@main
|
||||
enum CountingSort {
|
||||
/* Driver Code */
|
||||
static func main() {
|
||||
var nums = [1, 0, 1, 2, 0, 4, 0, 2, 2, 4]
|
||||
countingSortNaive(nums: &nums)
|
||||
print("After counting sort (cannot sort objects), nums = \(nums)")
|
||||
|
||||
var nums1 = [1, 0, 1, 2, 0, 4, 0, 2, 2, 4]
|
||||
countingSort(nums: &nums1)
|
||||
print("After counting sort, nums1 = \(nums1)")
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,55 @@
|
||||
/**
|
||||
* File: heap_sort.swift
|
||||
* Created Time: 2023-05-28
|
||||
* Author: nuomi1 (nuomi1@qq.com)
|
||||
*/
|
||||
|
||||
/* Heap length is n, start heapifying node i, from top to bottom */
|
||||
func siftDown(nums: inout [Int], n: Int, i: Int) {
|
||||
var i = 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
|
||||
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
|
||||
nums.swapAt(i, ma)
|
||||
// Loop downwards heapification
|
||||
i = ma
|
||||
}
|
||||
}
|
||||
|
||||
/* Heap sort */
|
||||
func heapSort(nums: inout [Int]) {
|
||||
// Build heap operation: heapify all nodes except leaves
|
||||
for i in stride(from: nums.count / 2 - 1, through: 0, by: -1) {
|
||||
siftDown(nums: &nums, n: nums.count, i: i)
|
||||
}
|
||||
// Extract the largest element from the heap and repeat for n-1 rounds
|
||||
for i in nums.indices.dropFirst().reversed() {
|
||||
// Delete node
|
||||
nums.swapAt(0, i)
|
||||
// Start heapifying the root node, from top to bottom
|
||||
siftDown(nums: &nums, n: i, i: 0)
|
||||
}
|
||||
}
|
||||
|
||||
@main
|
||||
enum HeapSort {
|
||||
/* Driver Code */
|
||||
static func main() {
|
||||
var nums = [4, 1, 3, 1, 5, 2]
|
||||
heapSort(nums: &nums)
|
||||
print("After heap sort, nums = \(nums)")
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
/**
|
||||
* File: insertion_sort.swift
|
||||
* Created Time: 2023-01-29
|
||||
* Author: nuomi1 (nuomi1@qq.com)
|
||||
*/
|
||||
|
||||
/* Insertion sort */
|
||||
func insertionSort(nums: inout [Int]) {
|
||||
// Outer loop: sorted interval is [0, i-1]
|
||||
for i in nums.indices.dropFirst() {
|
||||
let 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 -= 1
|
||||
}
|
||||
nums[j + 1] = base // Assign base to the correct position
|
||||
}
|
||||
}
|
||||
|
||||
@main
|
||||
enum InsertionSort {
|
||||
/* Driver Code */
|
||||
static func main() {
|
||||
var nums = [4, 1, 3, 1, 5, 2]
|
||||
insertionSort(nums: &nums)
|
||||
print("After insertion sort, nums = \(nums)")
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,65 @@
|
||||
/**
|
||||
* File: merge_sort.swift
|
||||
* Created Time: 2023-01-29
|
||||
* Author: nuomi1 (nuomi1@qq.com)
|
||||
*/
|
||||
|
||||
/* Merge left subarray and right subarray */
|
||||
func merge(nums: inout [Int], 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
|
||||
var tmp = Array(repeating: 0, count: right - left + 1)
|
||||
// Initialize the start indices of the left and right subarrays
|
||||
var 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]
|
||||
i += 1
|
||||
} else {
|
||||
tmp[k] = nums[j]
|
||||
j += 1
|
||||
}
|
||||
k += 1
|
||||
}
|
||||
// Copy the remaining elements of the left and right subarrays into the temporary array
|
||||
while i <= mid {
|
||||
tmp[k] = nums[i]
|
||||
i += 1
|
||||
k += 1
|
||||
}
|
||||
while j <= right {
|
||||
tmp[k] = nums[j]
|
||||
j += 1
|
||||
k += 1
|
||||
}
|
||||
// Copy the elements from the temporary array tmp back to the original array nums at the corresponding interval
|
||||
for k in tmp.indices {
|
||||
nums[left + k] = tmp[k]
|
||||
}
|
||||
}
|
||||
|
||||
/* Merge sort */
|
||||
func mergeSort(nums: inout [Int], left: Int, right: Int) {
|
||||
// Termination condition
|
||||
if left >= right { // Terminate recursion when subarray length is 1
|
||||
return
|
||||
}
|
||||
// Divide and conquer stage
|
||||
let mid = left + (right - left) / 2 // Calculate midpoint
|
||||
mergeSort(nums: &nums, left: left, right: mid) // Recursively process the left subarray
|
||||
mergeSort(nums: &nums, left: mid + 1, right: right) // Recursively process the right subarray
|
||||
// Merge stage
|
||||
merge(nums: &nums, left: left, mid: mid, right: right)
|
||||
}
|
||||
|
||||
@main
|
||||
enum MergeSort {
|
||||
/* Driver Code */
|
||||
static func main() {
|
||||
/* Merge sort */
|
||||
var nums = [7, 3, 2, 6, 0, 1, 5, 4]
|
||||
mergeSort(nums: &nums, left: nums.startIndex, right: nums.endIndex - 1)
|
||||
print("After merge sort, nums = \(nums)")
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,114 @@
|
||||
/**
|
||||
* File: quick_sort.swift
|
||||
* Created Time: 2023-01-29
|
||||
* Author: nuomi1 (nuomi1@qq.com)
|
||||
*/
|
||||
|
||||
/* Quick sort class */
|
||||
/* Sentinel partition */
|
||||
func partition(nums: inout [Int], 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 -= 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
|
||||
}
|
||||
nums.swapAt(i, j) // Swap these two elements
|
||||
}
|
||||
nums.swapAt(i, left) // Swap the pivot to the boundary between the two subarrays
|
||||
return i // Return the index of the pivot
|
||||
}
|
||||
|
||||
/* Quick sort */
|
||||
func quickSort(nums: inout [Int], left: Int, right: Int) {
|
||||
// Terminate recursion when subarray length is 1
|
||||
if left >= right {
|
||||
return
|
||||
}
|
||||
// Sentinel partition
|
||||
let pivot = partition(nums: &nums, left: left, right: right)
|
||||
// Recursively process the left subarray and right subarray
|
||||
quickSort(nums: &nums, left: left, right: pivot - 1)
|
||||
quickSort(nums: &nums, left: pivot + 1, right: right)
|
||||
}
|
||||
|
||||
/* Quick sort class (median pivot optimization) */
|
||||
/* Select the median of three candidate elements */
|
||||
func medianThree(nums: [Int], left: Int, mid: Int, right: Int) -> Int {
|
||||
let l = nums[left]
|
||||
let m = nums[mid]
|
||||
let r = nums[right]
|
||||
if (l <= m && m <= r) || (r <= m && m <= l) {
|
||||
return mid // m is between l and r
|
||||
}
|
||||
if (m <= l && l <= r) || (r <= l && l <= m) {
|
||||
return left // l is between m and r
|
||||
}
|
||||
return right
|
||||
}
|
||||
|
||||
/* Sentinel partition (median of three) */
|
||||
func partitionMedian(nums: inout [Int], left: Int, right: Int) -> Int {
|
||||
// Select the median of three candidate elements
|
||||
let med = medianThree(nums: nums, left: left, mid: left + (right - left) / 2, right: right)
|
||||
// Swap the median to the array's leftmost position
|
||||
nums.swapAt(left, med)
|
||||
return partition(nums: &nums, left: left, right: right)
|
||||
}
|
||||
|
||||
/* Quick sort (recursion depth optimization) */
|
||||
func quickSortMedian(nums: inout [Int], left: Int, right: Int) {
|
||||
// Terminate recursion when subarray length is 1
|
||||
if left >= right {
|
||||
return
|
||||
}
|
||||
// Sentinel partition
|
||||
let pivot = partitionMedian(nums: &nums, left: left, right: right)
|
||||
// Recursively process the left subarray and right subarray
|
||||
quickSortMedian(nums: &nums, left: left, right: pivot - 1)
|
||||
quickSortMedian(nums: &nums, left: pivot + 1, right: right)
|
||||
}
|
||||
|
||||
/* Quick sort (recursion depth optimization) */
|
||||
func quickSortTailCall(nums: inout [Int], left: Int, right: Int) {
|
||||
var left = left
|
||||
var right = right
|
||||
// Terminate when subarray length is 1
|
||||
while left < right {
|
||||
// Sentinel partition operation
|
||||
let pivot = partition(nums: &nums, left: left, right: right)
|
||||
// Perform quick sort on the shorter of the two subarrays
|
||||
if (pivot - left) < (right - pivot) {
|
||||
quickSortTailCall(nums: &nums, left: left, right: pivot - 1) // Recursively sort the left subarray
|
||||
left = pivot + 1 // Remaining unsorted interval is [pivot + 1, right]
|
||||
} else {
|
||||
quickSortTailCall(nums: &nums, left: pivot + 1, right: right) // Recursively sort the right subarray
|
||||
right = pivot - 1 // Remaining unsorted interval is [left, pivot - 1]
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@main
|
||||
enum QuickSort {
|
||||
/* Driver Code */
|
||||
static func main() {
|
||||
/* Quick sort */
|
||||
var nums = [2, 4, 1, 0, 3, 5]
|
||||
quickSort(nums: &nums, left: nums.startIndex, right: nums.endIndex - 1)
|
||||
print("After quick sort, nums = \(nums)")
|
||||
|
||||
/* Quick sort (recursion depth optimization) */
|
||||
var nums1 = [2, 4, 1, 0, 3, 5]
|
||||
quickSortMedian(nums: &nums1, left: nums1.startIndex, right: nums1.endIndex - 1)
|
||||
print("After quick sort (median pivot optimization), nums1 = \(nums1)")
|
||||
|
||||
/* Quick sort (recursion depth optimization) */
|
||||
var nums2 = [2, 4, 1, 0, 3, 5]
|
||||
quickSortTailCall(nums: &nums2, left: nums2.startIndex, right: nums2.endIndex - 1)
|
||||
print("After quick sort (recursion depth optimization), nums2 = \(nums2)")
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,79 @@
|
||||
/**
|
||||
* File: radix_sort.swift
|
||||
* Created Time: 2023-01-29
|
||||
* Author: nuomi1 (nuomi1@qq.com)
|
||||
*/
|
||||
|
||||
/* Get the k-th digit of element num, where exp = 10^(k-1) */
|
||||
func digit(num: Int, exp: Int) -> Int {
|
||||
// Passing exp instead of k can avoid repeated expensive exponentiation here
|
||||
(num / exp) % 10
|
||||
}
|
||||
|
||||
/* Counting sort (based on nums k-th digit) */
|
||||
func countingSortDigit(nums: inout [Int], exp: Int) {
|
||||
// Decimal digit range is 0~9, therefore need a bucket array of length 10
|
||||
var counter = Array(repeating: 0, count: 10)
|
||||
// Count the occurrence of digits 0~9
|
||||
for i in nums.indices {
|
||||
let d = digit(num: nums[i], exp: exp) // Get the k-th digit of nums[i], noted as d
|
||||
counter[d] += 1 // Count the occurrence of digit d
|
||||
}
|
||||
// Calculate prefix sum, converting "occurrence count" into "array index"
|
||||
for i in 1 ..< 10 {
|
||||
counter[i] += counter[i - 1]
|
||||
}
|
||||
// Traverse in reverse, based on bucket statistics, place each element into res
|
||||
var res = Array(repeating: 0, count: nums.count)
|
||||
for i in nums.indices.reversed() {
|
||||
let d = digit(num: nums[i], exp: exp)
|
||||
let 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] -= 1 // Decrease the count of d by 1
|
||||
}
|
||||
// Use result to overwrite the original array nums
|
||||
for i in nums.indices {
|
||||
nums[i] = res[i]
|
||||
}
|
||||
}
|
||||
|
||||
/* Radix sort */
|
||||
func radixSort(nums: inout [Int]) {
|
||||
// Get the maximum element of the array, used to determine the maximum number of digits
|
||||
var m = Int.min
|
||||
for num in nums {
|
||||
if num > m {
|
||||
m = num
|
||||
}
|
||||
}
|
||||
// Traverse from the lowest to the highest digit
|
||||
for exp in sequence(first: 1, next: { m >= ($0 * 10) ? $0 * 10 : nil }) {
|
||||
// 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: &nums, exp: exp)
|
||||
}
|
||||
}
|
||||
|
||||
@main
|
||||
enum RadixSort {
|
||||
/* Driver Code */
|
||||
static func main() {
|
||||
// Radix sort
|
||||
var nums = [
|
||||
10_546_151,
|
||||
35_663_510,
|
||||
42_865_989,
|
||||
34_862_445,
|
||||
81_883_077,
|
||||
88_906_420,
|
||||
72_429_244,
|
||||
30_524_779,
|
||||
82_060_337,
|
||||
63_832_996,
|
||||
]
|
||||
radixSort(nums: &nums)
|
||||
print("After radix sort, nums = \(nums)")
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
/**
|
||||
* File: selection_sort.swift
|
||||
* Created Time: 2023-05-28
|
||||
* Author: nuomi1 (nuomi1@qq.com)
|
||||
*/
|
||||
|
||||
/* Selection sort */
|
||||
func selectionSort(nums: inout [Int]) {
|
||||
// Outer loop: unsorted interval is [i, n-1]
|
||||
for i in nums.indices.dropLast() {
|
||||
// Inner loop: find the smallest element within the unsorted interval
|
||||
var k = i
|
||||
for j in nums.indices.dropFirst(i + 1) {
|
||||
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.swapAt(i, k)
|
||||
}
|
||||
}
|
||||
|
||||
@main
|
||||
enum SelectionSort {
|
||||
/* Driver Code */
|
||||
static func main() {
|
||||
var nums = [4, 1, 3, 1, 5, 2]
|
||||
selectionSort(nums: &nums)
|
||||
print("After selection sort, nums = \(nums)")
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user