Skip to content

Commit

Permalink
feat: add ruby code block - merge sort (#1260)
Browse files Browse the repository at this point in the history
* feat(codes/ruby/chapter_sorting/merge_sort.rb): add merge_sort ruby file

* feat(codes/ruby/chapter_sorting/merge_sort.rb): modify comment space

* feat(codes/ruby/chapter_sorting/merge_sort.rb): set array default value

Co-authored-by: khoaxuantu <[email protected]>

* feat(codes/ruby/chapter_sorting/merge_sort.rb): driver code add if logic

Co-authored-by: khoaxuantu <[email protected]>

* feat(codes/ruby/chapter_sorting/merge_sort.rb): modify comment style

Co-authored-by: khoaxuantu <[email protected]>

* feat(codes/ruby/chapter_sorting/merge_sort.rb): modify logic instruction

Co-authored-by: khoaxuantu <[email protected]>

---------

Co-authored-by: khoaxuantu <[email protected]>
  • Loading branch information
junminhong and khoaxuantu authored Apr 30, 2024
1 parent 3f857db commit 68f3a45
Showing 1 changed file with 60 additions and 0 deletions.
60 changes: 60 additions & 0 deletions codes/ruby/chapter_sorting/merge_sort.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
=begin
File: merge_sort.rb
Created Time: 2024-04-10
Author: junminhong ([email protected])
=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

0 comments on commit 68f3a45

Please sign in to comment.