Bug fixes and improvements (#1348)

* Add "reference" for EN version. Bug fixes.

* Unify the figure reference as "the figure below" and "the figure above".
Bug fixes.

* Format the EN markdown files.

* Replace "" with <u></u> for EN version and bug fixes

* Fix biary_tree_dfs.png

* Fix biary_tree_dfs.png

* Fix zh-hant/biary_tree_dfs.png

* Fix heap_sort_step1.png

* Sync zh and zh-hant versions.

* Bug fixes

* Fix EN figures

* Bug fixes

* Fix the figure labels for EN version
This commit is contained in:
Yudong Jin
2024-05-06 14:44:48 +08:00
committed by GitHub
parent 8e60d12151
commit c4a7966882
99 changed files with 615 additions and 259 deletions

View File

@ -15,6 +15,7 @@ func backtrack(row, n int, state *[][]string, res *[][][]string, cols, diags1, d
}
*res = append(*res, newState)
return
}
// 走訪所有列
for col := 0; col < n; col++ {

View File

@ -23,7 +23,7 @@ func backtrackII(state *[]int, choices *[]int, selected *[]bool, res *[][]int) {
(*selected)[i] = true
*state = append(*state, choice)
// 進行下一輪選擇
backtrackI(state, choices, selected, res)
backtrackII(state, choices, selected, res)
// 回退:撤銷選擇,恢復到之前的狀態
(*selected)[i] = false
*state = (*state)[:len(*state)-1]

View File

@ -0,0 +1,51 @@
=begin
File: bubble_sort.rb
Created Time: 2024-05-02
Author: Xuan Khoa Tu Nguyen (ngxktuzkai2000@gmail.com)
=end
### 泡沫排序 ###
def bubble_sort(nums)
n = nums.length
# 外迴圈:未排序區間為 [0, i]
for i in (n - 1).downto(1)
# 內迴圈:將未排序區間 [0, i] 中的最大元素交換至該區間的最右端
for j in 0...i
if nums[j] > nums[j + 1]
# 交換 nums[j] 與 nums[j + 1]
nums[j], nums[j + 1] = nums[j + 1], nums[j]
end
end
end
end
### 泡沫排序(標誌最佳化)###
def bubble_sort_with_flag(nums)
n = nums.length
# 外迴圈:未排序區間為 [0, i]
for i in (n - 1).downto(1)
flag = false # 初始化標誌位
# 內迴圈:將未排序區間 [0, i] 中的最大元素交換至該區間的最右端
for j in 0...i
if nums[j] > nums[j + 1]
# 交換 nums[j] 與 nums[j + 1]
nums[j], nums[j + 1] = nums[j + 1], nums[j]
flag = true # 記錄交換元素
end
end
break unless flag # 此輪“冒泡”未交換任何元素,直接跳出
end
end
### Driver Code ###
if __FILE__ == $0
nums = [4, 1, 3, 1, 5, 2]
bubble_sort(nums)
puts "泡沫排序完成後 nums = #{nums}"
nums1 = [4, 1, 3, 1, 5, 2]
bubble_sort_with_flag(nums1)
puts "泡沫排序完成後 nums = #{nums1}"
end

View File

@ -0,0 +1,43 @@
=begin
File: bucket_sort.rb
Created Time: 2024-04-17
Author: Martin Xu (martin.xus@gmail.com)
=end
### 桶排序 ###
def bucket_sort(nums)
# 初始化 k = n/2 個桶,預期向每個桶分配 2 個元素
k = nums.length / 2
buckets = Array.new(k) { [] }
# 1. 將陣列元素分配到各個桶中
nums.each do |num|
# 輸入資料範圍為 [0, 1),使用 num * k 對映到索引範圍 [0, k-1]
i = (num * k).to_i
# 將 num 新增進桶 i
buckets[i] << num
end
# 2. 對各個桶執行排序
buckets.each do |bucket|
# 使用內建排序函式,也可以替換成其他排序演算法
bucket.sort!
end
# 3. 走訪桶合併結果
i = 0
buckets.each do |bucket|
bucket.each do |num|
nums[i] = num
i += 1
end
end
end
### Driver Code ###
if __FILE__ == $0
# 設輸入資料為浮點數,範圍為 [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)
puts "桶排序完成後 nums = #{nums}"
end

View File

@ -0,0 +1,62 @@
=begin
File: counting_sort.rb
Created Time: 2024-05-02
Author: Xuan Khoa Tu Nguyen (ngxktuzkai2000@gmail.com)
=end
### 計數排序 ###
def counting_sort_naive(nums)
# 簡單實現,無法用於排序物件
# 1. 統計陣列最大元素 m
m = 0
nums.each { |num| m = [m, num].max }
# 2. 統計各數字的出現次數
# counter[num] 代表 num 的出現次數
counter = Array.new(m + 1, 0)
nums.each { |num| counter[num] += 1 }
# 3. 走訪 counter ,將各元素填入原陣列 nums
i = 0
for num in 0...(m + 1)
(0...counter[num]).each do
nums[i] = num
i += 1
end
end
end
### 計數排序 ###
def counting_sort(nums)
# 完整實現,可排序物件,並且是穩定排序
# 1. 統計陣列最大元素 m
m = nums.max
# 2. 統計各數字的出現次數
# counter[num] 代表 num 的出現次數
counter = Array.new(m + 1, 0)
nums.each { |num| counter[num] += 1 }
# 3. 求 counter 的前綴和,將“出現次數”轉換為“尾索引”
# 即 counter[num]-1 是 num 在 res 中最後一次出現的索引
(0...m).each { |i| counter[i + 1] += counter[i] }
# 4. 倒序走訪 nums, 將各元素填入結果陣列 res
# 初始化陣列 res 用於記錄結果
n = nums.length
res = Array.new(n, 0)
(n - 1).downto(0).each do |i|
num = nums[i]
res[counter[num] - 1] = num # 將 num 放置到對應索引處
counter[num] -= 1 # 令前綴和自減 1 ,得到下次放置 num 的索引
end
# 使用結果陣列 res 覆蓋原陣列 nums
(0...n).each { |i| nums[i] = res[i] }
end
### Driver Code ###
if __FILE__ == $0
nums = [1, 0, 1, 2, 0, 4, 0, 2, 2, 4]
counting_sort_naive(nums)
puts "計數排序(無法排序物件)完成後 nums = #{nums}"
nums1 = [1, 0, 1, 2, 0, 4, 0, 2, 2, 4]
counting_sort(nums1)
puts "計數排序完成後 nums1 = #{nums1}"
end

View File

@ -0,0 +1,45 @@
=begin
File: heap_sort.rb
Created Time: 2024-04-10
Author: junminhong (junminhong1110@gmail.com)
=end
### 堆積的長度為 n ,從節點 i 開始,從頂至底堆積化 ###
def sift_down(nums, n, i)
while true
# 判斷節點 i, l, r 中值最大的節點,記為 ma
l = 2 * i + 1
r = 2 * i + 2
ma = i
ma = l if l < n && nums[l] > nums[ma]
ma = r if r < n && nums[r] > nums[ma]
# 若節點 i 最大或索引 l, r 越界,則無須繼續堆積化,跳出
break if ma == i
# 交換兩節點
nums[i], nums[ma] = nums[ma], nums[i]
# 迴圈向下堆積化
i = ma
end
end
### 堆積排序 ###
def heap_sort(nums)
# 建堆積操作:堆積化除葉節點以外的其他所有節點
(nums.length / 2 - 1).downto(0) do |i|
sift_down(nums, nums.length, i)
end
# 從堆積中提取最大元素,迴圈 n-1 輪
(nums.length - 1).downto(1) do |i|
# 交換根節點與最右葉節點(交換首元素與尾元素)
nums[0], nums[i] = nums[i], nums[0]
# 以根節點為起點,從頂至底進行堆積化
sift_down(nums, i, 0)
end
end
### Driver Code ###
if __FILE__ == $0
nums = [4, 1, 3, 1, 5, 2]
heap_sort(nums)
puts "堆積排序完成後 nums = #{nums.inspect}"
end

View File

@ -0,0 +1,60 @@
=begin
File: merge_sort.rb
Created Time: 2024-04-10
Author: junminhong (junminhong1110@gmail.com)
=end
### 合併左子陣列和右子陣列 ###
def merge(nums, left, mid, right)
# 左子陣列區間為 [left, mid], 右子陣列區間為 [mid+1, right]
# 建立一個臨時陣列 tmp用於存放合併後的結果
tmp = Array.new(right - left + 1, 0)
# 初始化左子陣列和右子陣列的起始索引
i, j, k = left, mid + 1, 0
# 當左右子陣列都還有元素時,進行比較並將較小的元素複製到臨時陣列中
while i <= mid && j <= right
if nums[i] <= nums[j]
tmp[k] = nums[i]
i += 1
else
tmp[k] = nums[j]
j += 1
end
k += 1
end
# 將左子陣列和右子陣列的剩餘元素複製到臨時陣列中
while i <= mid
tmp[k] = nums[i]
i += 1
k += 1
end
while j <= right
tmp[k] = nums[j]
j += 1
k += 1
end
# 將臨時陣列 tmp 中的元素複製回原陣列 nums 的對應區間
(0...tmp.length).each do |k|
nums[left + k] = tmp[k]
end
end
### 合併排序 ###
def merge_sort(nums, left, right)
# 終止條件
# 當子陣列長度為 1 時終止遞迴
return if left >= right
# 劃分階段
mid = (left + right) / 2 # 計算中點
merge_sort(nums, left, mid) # 遞迴左子陣列
merge_sort(nums, mid + 1, right) # 遞迴右子陣列
# 合併階段
merge(nums, left, mid, right)
end
### Driver Code ###
if __FILE__ == $0
nums = [7, 3, 2, 6, 0, 1, 5, 4]
merge_sort(nums, 0, nums.length - 1)
puts "合併排序完成後 nums = #{nums.inspect}"
end

View File

@ -9,7 +9,6 @@ class QuickSort
class << self
### 哨兵劃分 ###
def partition(nums, left, right)
# 以 nums[left] 為基準數
i, j = left, right
while i < j
@ -116,7 +115,7 @@ class QuickSortTailCall
i # 返回基準數的索引
end
### 快速排序(尾遞迴最佳化)
### 快速排序(尾遞迴最佳化)###
def quick_sort(nums, left, right)
# 子陣列長度不為 1 時遞迴
while left < right

View File

@ -0,0 +1,70 @@
=begin
File: radix_sort.rb
Created Time: 2024-05-03
Author: Xuan Khoa Tu Nguyen (ngxktuzkai2000@gmail.com)
=end
### 獲取元素 num 的第 k 位,其中 exp = 10^(k-1) ###
def digit(num, exp)
# 轉入 exp 而非 k 可以避免在此重複執行昂貴的次方計算
(num / exp) % 10
end
### 計數排序(根據 nums 第 k 位排序)###
def counting_sort_digit(nums, exp)
# 十進位制的位範圍為 0~9 ,因此需要長度為 10 的桶陣列
counter = Array.new(10, 0)
n = nums.length
# 統計 0~9 各數字的出現次數
for i in 0...n
d = digit(nums[i], exp) # 獲取 nums[i] 第 k 位,記為 d
counter[d] += 1 # 統計數字 d 的出現次數
end
# 求前綴和,將“出現個數”轉換為“陣列索引”
(1...10).each { |i| counter[i] += counter[i - 1] }
# 倒序走訪,根據桶內統計結果,將各元素填入 res
res = Array.new(n, 0)
for i in (n - 1).downto(0)
d = digit(nums[i], exp)
j = counter[d] - 1 # 獲取 d 在陣列中的索引 j
res[j] = nums[i] # 將當前元素填入索引 j
counter[d] -= 1 # 將 d 的數量減 1
end
# 使用結果覆蓋原陣列 nums
(0...n).each { |i| nums[i] = res[i] }
end
### 基數排序 ###
def radix_sort(nums)
# 獲取陣列的最大元素,用於判斷最大位數
m = nums.max
# 按照從低位到高位的順序走訪
exp = 1
while exp <= m
# 對陣列元素的第 k 位執行計數排序
# k = 1 -> exp = 1
# k = 2 -> exp = 10
# 即 exp = 10^(k-1)
counting_sort_digit(nums, exp)
exp *= 10
end
end
### Driver Code ###
if __FILE__ == $0
# 基數排序
nums = [
10546151,
35663510,
42865989,
34862445,
81883077,
88906420,
72429244,
30524779,
82060337,
63832996,
]
radix_sort(nums)
puts "基數排序完成後 nums = #{nums}"
end

View File

@ -0,0 +1,29 @@
=begin
File: selection_sort.rb
Created Time: 2024-05-03
Author: Xuan Khoa Tu Nguyen (ngxktuzkai2000@gmail.com)
=end
### 選擇排序 ###
def selection_sort(nums)
n = nums.length
# 外迴圈:未排序區間為 [i, n-1]
for i in 0...(n - 1)
# 內迴圈:找到未排序區間內的最小元素
k = i
for j in (i + 1)...n
if nums[j] < nums[k]
k = j # 記錄最小元素的索引
end
end
# 將該最小元素與未排序區間的首個元素交換
nums[i], nums[k] = nums[k], nums[i]
end
end
### Driver Code ###
if __FILE__ == $0
nums = [4, 1, 3, 1, 5, 2]
selection_sort(nums)
puts "選擇排序完成後 nums = #{nums}"
end