mirror of
https://github.com/krahets/hello-algo.git
synced 2025-07-06 14:27:26 +08:00
translation: Add Python and Java code for EN version (#1345)
* Add the intial translation of code of all the languages * test * revert * Remove * Add Python and Java code for EN version
This commit is contained in:
44
en/codes/python/chapter_sorting/bubble_sort.py
Normal file
44
en/codes/python/chapter_sorting/bubble_sort.py
Normal file
@ -0,0 +1,44 @@
|
||||
"""
|
||||
File: bubble_sort.py
|
||||
Created Time: 2022-11-25
|
||||
Author: timi (xisunyy@163.com)
|
||||
"""
|
||||
|
||||
|
||||
def bubble_sort(nums: list[int]):
|
||||
"""Bubble sort"""
|
||||
n = len(nums)
|
||||
# Outer loop: unsorted range 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
|
||||
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]
|
||||
|
||||
|
||||
def bubble_sort_with_flag(nums: list[int]):
|
||||
"""Bubble sort (optimized with flag)"""
|
||||
n = len(nums)
|
||||
# Outer loop: unsorted range 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
|
||||
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
|
||||
if not flag:
|
||||
break # If no elements were swapped in this round of "bubbling", exit
|
||||
|
||||
|
||||
"""Driver Code"""
|
||||
if __name__ == "__main__":
|
||||
nums = [4, 1, 3, 1, 5, 2]
|
||||
bubble_sort(nums)
|
||||
print("Bubble sort completed nums =", nums)
|
||||
|
||||
nums1 = [4, 1, 3, 1, 5, 2]
|
||||
bubble_sort_with_flag(nums1)
|
||||
print("Bubble sort completed nums =", nums1)
|
35
en/codes/python/chapter_sorting/bucket_sort.py
Normal file
35
en/codes/python/chapter_sorting/bucket_sort.py
Normal file
@ -0,0 +1,35 @@
|
||||
"""
|
||||
File: bucket_sort.py
|
||||
Created Time: 2023-03-30
|
||||
Author: krahets (krahets@163.com)
|
||||
"""
|
||||
|
||||
|
||||
def bucket_sort(nums: list[float]):
|
||||
"""Bucket sort"""
|
||||
# Initialize k = n/2 buckets, expected to allocate 2 elements per bucket
|
||||
k = len(nums) // 2
|
||||
buckets = [[] for _ in range(k)]
|
||||
# 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]
|
||||
i = int(num * k)
|
||||
# Add num to bucket i
|
||||
buckets[i].append(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
|
||||
i = 0
|
||||
for bucket in buckets:
|
||||
for num in bucket:
|
||||
nums[i] = num
|
||||
i += 1
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
# Assume input data is floating point, range [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)
|
64
en/codes/python/chapter_sorting/counting_sort.py
Normal file
64
en/codes/python/chapter_sorting/counting_sort.py
Normal file
@ -0,0 +1,64 @@
|
||||
"""
|
||||
File: counting_sort.py
|
||||
Created Time: 2023-03-21
|
||||
Author: krahets (krahets@163.com)
|
||||
"""
|
||||
|
||||
|
||||
def counting_sort_naive(nums: list[int]):
|
||||
"""Counting sort"""
|
||||
# Simple implementation, cannot be used for sorting objects
|
||||
# 1. Count the maximum element m in the array
|
||||
m = 0
|
||||
for num in nums:
|
||||
m = max(m, num)
|
||||
# 2. Count the occurrence of each digit
|
||||
# counter[num] represents the occurrence of num
|
||||
counter = [0] * (m + 1)
|
||||
for num in nums:
|
||||
counter[num] += 1
|
||||
# 3. Traverse counter, filling each element back into the original array nums
|
||||
i = 0
|
||||
for num in range(m + 1):
|
||||
for _ in range(counter[num]):
|
||||
nums[i] = num
|
||||
i += 1
|
||||
|
||||
|
||||
def counting_sort(nums: list[int]):
|
||||
"""Counting sort"""
|
||||
# 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
|
||||
# counter[num] represents the occurrence of num
|
||||
counter = [0] * (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 range(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
|
||||
n = len(nums)
|
||||
res = [0] * n
|
||||
for i in range(n - 1, -1, -1):
|
||||
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 range(n):
|
||||
nums[i] = res[i]
|
||||
|
||||
|
||||
"""Driver Code"""
|
||||
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}")
|
||||
|
||||
nums1 = [1, 0, 1, 2, 0, 4, 0, 2, 2, 4]
|
||||
counting_sort(nums1)
|
||||
print(f"Counting sort completed nums1 = {nums1}")
|
45
en/codes/python/chapter_sorting/heap_sort.py
Normal file
45
en/codes/python/chapter_sorting/heap_sort.py
Normal file
@ -0,0 +1,45 @@
|
||||
"""
|
||||
File: heap_sort.py
|
||||
Created Time: 2023-05-24
|
||||
Author: krahets (krahets@163.com)
|
||||
"""
|
||||
|
||||
|
||||
def sift_down(nums: list[int], n: int, i: int):
|
||||
"""Heap length is n, start heapifying node i, from top to bottom"""
|
||||
while True:
|
||||
# Determine the largest node among i, l, r, noted as ma
|
||||
l = 2 * i + 1
|
||||
r = 2 * i + 2
|
||||
ma = i
|
||||
if l < n and nums[l] > nums[ma]:
|
||||
ma = l
|
||||
if r < n and 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
|
||||
if ma == i:
|
||||
break
|
||||
# Swap two nodes
|
||||
nums[i], nums[ma] = nums[ma], nums[i]
|
||||
# Loop downwards heapification
|
||||
i = ma
|
||||
|
||||
|
||||
def heap_sort(nums: list[int]):
|
||||
"""Heap sort"""
|
||||
# Build heap operation: heapify all nodes except leaves
|
||||
for i in range(len(nums) // 2 - 1, -1, -1):
|
||||
sift_down(nums, len(nums), i)
|
||||
# Extract the largest element from the heap and repeat for n-1 rounds
|
||||
for i in range(len(nums) - 1, 0, -1):
|
||||
# Swap the root node with the rightmost leaf node (swap the first element with the last element)
|
||||
nums[0], nums[i] = nums[i], nums[0]
|
||||
# Start heapifying the root node, from top to bottom
|
||||
sift_down(nums, i, 0)
|
||||
|
||||
|
||||
"""Driver Code"""
|
||||
if __name__ == "__main__":
|
||||
nums = [4, 1, 3, 1, 5, 2]
|
||||
heap_sort(nums)
|
||||
print("Heap sort completed nums =", nums)
|
25
en/codes/python/chapter_sorting/insertion_sort.py
Normal file
25
en/codes/python/chapter_sorting/insertion_sort.py
Normal file
@ -0,0 +1,25 @@
|
||||
"""
|
||||
File: insertion_sort.py
|
||||
Created Time: 2022-11-25
|
||||
Author: timi (xisunyy@163.com)
|
||||
"""
|
||||
|
||||
|
||||
def insertion_sort(nums: list[int]):
|
||||
"""Insertion sort"""
|
||||
# Outer loop: sorted range 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]
|
||||
while j >= 0 and 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
|
||||
|
||||
|
||||
"""Driver Code"""
|
||||
if __name__ == "__main__":
|
||||
nums = [4, 1, 3, 1, 5, 2]
|
||||
insertion_sort(nums)
|
||||
print("Insertion sort completed nums =", nums)
|
55
en/codes/python/chapter_sorting/merge_sort.py
Normal file
55
en/codes/python/chapter_sorting/merge_sort.py
Normal file
@ -0,0 +1,55 @@
|
||||
"""
|
||||
File: merge_sort.py
|
||||
Created Time: 2022-11-25
|
||||
Author: timi (xisunyy@163.com), krahets (krahets@163.com)
|
||||
"""
|
||||
|
||||
|
||||
def merge(nums: list[int], left: int, mid: int, right: int):
|
||||
"""Merge left subarray and right subarray"""
|
||||
# Left subarray interval is [left, mid], right subarray interval is [mid+1, right]
|
||||
# Create a temporary array tmp to store the merged results
|
||||
tmp = [0] * (right - left + 1)
|
||||
# Initialize the start indices of the left and right subarrays
|
||||
i, j, k = left, mid + 1, 0
|
||||
# While both subarrays still have elements, compare and copy the smaller element into the temporary array
|
||||
while i <= mid and 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 range(0, len(tmp)):
|
||||
nums[left + k] = tmp[k]
|
||||
|
||||
|
||||
def merge_sort(nums: list[int], left: int, right: int):
|
||||
"""Merge sort"""
|
||||
# Termination condition
|
||||
if left >= right:
|
||||
return # Terminate recursion when subarray length is 1
|
||||
# Partition 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
|
||||
merge(nums, left, mid, right)
|
||||
|
||||
|
||||
"""Driver Code"""
|
||||
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)
|
129
en/codes/python/chapter_sorting/quick_sort.py
Normal file
129
en/codes/python/chapter_sorting/quick_sort.py
Normal file
@ -0,0 +1,129 @@
|
||||
"""
|
||||
File: quick_sort.py
|
||||
Created Time: 2022-11-25
|
||||
Author: timi (xisunyy@163.com)
|
||||
"""
|
||||
|
||||
|
||||
class QuickSort:
|
||||
"""Quick sort class"""
|
||||
|
||||
def partition(self, nums: list[int], left: int, right: int) -> int:
|
||||
"""Partition"""
|
||||
# Use nums[left] as the pivot
|
||||
i, j = left, right
|
||||
while i < j:
|
||||
while i < j and nums[j] >= nums[left]:
|
||||
j -= 1 # Search from right to left for the first element smaller than the pivot
|
||||
while i < j and nums[i] <= nums[left]:
|
||||
i += 1 # Search from left to right for the first element greater than the pivot
|
||||
# Swap elements
|
||||
nums[i], nums[j] = nums[j], nums[i]
|
||||
# Swap the pivot to the boundary between the two subarrays
|
||||
nums[i], nums[left] = nums[left], nums[i]
|
||||
return i # Return the index of the pivot
|
||||
|
||||
def quick_sort(self, nums: list[int], left: int, right: int):
|
||||
"""Quick sort"""
|
||||
# Terminate recursion when subarray length is 1
|
||||
if left >= right:
|
||||
return
|
||||
# Partition
|
||||
pivot = self.partition(nums, left, right)
|
||||
# Recursively process the left subarray and right subarray
|
||||
self.quick_sort(nums, left, pivot - 1)
|
||||
self.quick_sort(nums, pivot + 1, right)
|
||||
|
||||
|
||||
class QuickSortMedian:
|
||||
"""Quick sort class (median pivot optimization)"""
|
||||
|
||||
def median_three(self, nums: list[int], left: int, mid: int, right: int) -> int:
|
||||
"""Select the median of three candidate elements"""
|
||||
l, m, r = nums[left], nums[mid], nums[right]
|
||||
if (l <= m <= r) or (r <= m <= l):
|
||||
return mid # m is between l and r
|
||||
if (m <= l <= r) or (r <= l <= m):
|
||||
return left # l is between m and r
|
||||
return right
|
||||
|
||||
def partition(self, nums: list[int], left: int, right: int) -> int:
|
||||
"""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
|
||||
nums[left], nums[med] = nums[med], nums[left]
|
||||
# Use nums[left] as the pivot
|
||||
i, j = left, right
|
||||
while i < j:
|
||||
while i < j and nums[j] >= nums[left]:
|
||||
j -= 1 # Search from right to left for the first element smaller than the pivot
|
||||
while i < j and nums[i] <= nums[left]:
|
||||
i += 1 # Search from left to right for the first element greater than the pivot
|
||||
# Swap elements
|
||||
nums[i], nums[j] = nums[j], nums[i]
|
||||
# Swap the pivot to the boundary between the two subarrays
|
||||
nums[i], nums[left] = nums[left], nums[i]
|
||||
return i # Return the index of the pivot
|
||||
|
||||
def quick_sort(self, nums: list[int], left: int, right: int):
|
||||
"""Quick sort"""
|
||||
# Terminate recursion when subarray length is 1
|
||||
if left >= right:
|
||||
return
|
||||
# Partition
|
||||
pivot = self.partition(nums, left, right)
|
||||
# Recursively process the left subarray and right subarray
|
||||
self.quick_sort(nums, left, pivot - 1)
|
||||
self.quick_sort(nums, pivot + 1, right)
|
||||
|
||||
|
||||
class QuickSortTailCall:
|
||||
"""Quick sort class (tail recursion optimization)"""
|
||||
|
||||
def partition(self, nums: list[int], left: int, right: int) -> int:
|
||||
"""Partition"""
|
||||
# Use nums[left] as the pivot
|
||||
i, j = left, right
|
||||
while i < j:
|
||||
while i < j and nums[j] >= nums[left]:
|
||||
j -= 1 # Search from right to left for the first element smaller than the pivot
|
||||
while i < j and nums[i] <= nums[left]:
|
||||
i += 1 # Search from left to right for the first element greater than the pivot
|
||||
# Swap elements
|
||||
nums[i], nums[j] = nums[j], nums[i]
|
||||
# Swap the pivot to the boundary between the two subarrays
|
||||
nums[i], nums[left] = nums[left], nums[i]
|
||||
return i # Return the index of the pivot
|
||||
|
||||
def quick_sort(self, nums: list[int], left: int, right: int):
|
||||
"""Quick sort (tail recursion optimization)"""
|
||||
# Terminate when subarray length is 1
|
||||
while left < right:
|
||||
# Partition operation
|
||||
pivot = self.partition(nums, left, right)
|
||||
# Perform quick sort on the shorter of the two subarrays
|
||||
if pivot - left < right - pivot:
|
||||
self.quick_sort(nums, left, pivot - 1) # Recursively sort the left subarray
|
||||
left = pivot + 1 # Remaining unsorted interval is [pivot + 1, right]
|
||||
else:
|
||||
self.quick_sort(nums, pivot + 1, right) # Recursively sort the right subarray
|
||||
right = pivot - 1 # Remaining unsorted interval is [left, pivot - 1]
|
||||
|
||||
|
||||
"""Driver Code"""
|
||||
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)
|
||||
|
||||
# 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)
|
||||
|
||||
# Quick sort (tail recursion 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)
|
69
en/codes/python/chapter_sorting/radix_sort.py
Normal file
69
en/codes/python/chapter_sorting/radix_sort.py
Normal file
@ -0,0 +1,69 @@
|
||||
"""
|
||||
File: radix_sort.py
|
||||
Created Time: 2023-03-26
|
||||
Author: krahets (krahets@163.com)
|
||||
"""
|
||||
|
||||
|
||||
def digit(num: int, exp: int) -> int:
|
||||
"""Get the k-th digit of element num, where exp = 10^(k-1)"""
|
||||
# Passing exp instead of k can avoid repeated expensive exponentiation here
|
||||
return (num // exp) % 10
|
||||
|
||||
|
||||
def counting_sort_digit(nums: list[int], exp: int):
|
||||
"""Counting sort (based on nums k-th digit)"""
|
||||
# Decimal digit range is 0~9, therefore need a bucket array of length 10
|
||||
counter = [0] * 10
|
||||
n = len(nums)
|
||||
# Count the occurrence of digits 0~9
|
||||
for i in range(n):
|
||||
d = digit(nums[i], 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 range(1, 10):
|
||||
counter[i] += counter[i - 1]
|
||||
# Traverse in reverse, based on bucket statistics, place each element into res
|
||||
res = [0] * n
|
||||
for i in range(n - 1, -1, -1):
|
||||
d = digit(nums[i], exp)
|
||||
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 range(n):
|
||||
nums[i] = res[i]
|
||||
|
||||
|
||||
def radix_sort(nums: list[int]):
|
||||
"""Radix sort"""
|
||||
# Get the maximum element of the array, used to determine the maximum number of digits
|
||||
m = max(nums)
|
||||
# Traverse from the lowest to the highest digit
|
||||
exp = 1
|
||||
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)
|
||||
counting_sort_digit(nums, exp)
|
||||
exp *= 10
|
||||
|
||||
|
||||
"""Driver Code"""
|
||||
if __name__ == "__main__":
|
||||
# Radix sort
|
||||
nums = [
|
||||
10546151,
|
||||
35663510,
|
||||
42865989,
|
||||
34862445,
|
||||
81883077,
|
||||
88906420,
|
||||
72429244,
|
||||
30524779,
|
||||
82060337,
|
||||
63832996,
|
||||
]
|
||||
radix_sort(nums)
|
||||
print("Radix sort completed nums =", nums)
|
26
en/codes/python/chapter_sorting/selection_sort.py
Normal file
26
en/codes/python/chapter_sorting/selection_sort.py
Normal file
@ -0,0 +1,26 @@
|
||||
"""
|
||||
File: selection_sort.py
|
||||
Created Time: 2023-05-22
|
||||
Author: krahets (krahets@163.com)
|
||||
"""
|
||||
|
||||
|
||||
def selection_sort(nums: list[int]):
|
||||
"""Selection sort"""
|
||||
n = len(nums)
|
||||
# Outer loop: unsorted range is [i, n-1]
|
||||
for i in range(n - 1):
|
||||
# Inner loop: find the smallest element within the unsorted range
|
||||
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
|
||||
nums[i], nums[k] = nums[k], nums[i]
|
||||
|
||||
|
||||
"""Driver Code"""
|
||||
if __name__ == "__main__":
|
||||
nums = [4, 1, 3, 1, 5, 2]
|
||||
selection_sort(nums)
|
||||
print("Selection sort completed nums =", nums)
|
Reference in New Issue
Block a user