Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add ruby code block - heap sort #1261

Merged
merged 5 commits into from
Apr 30, 2024

Conversation

junminhong
Copy link
Contributor

@junminhong junminhong commented Apr 10, 2024

If this pull request (PR) pertains to Chinese-to-English translation, please confirm that you have read the contribution guidelines and complete the checklist below:

  • This PR represents the translation of a single, complete document, or contains only bug fixes.
  • The translation accurately conveys the original meaning and intent of the Chinese version. If deviations exist, I have provided explanatory comments to clarify the reasons.

If this pull request (PR) is associated with coding or code transpilation, please attach the relevant console outputs to the PR and complete the following checklist:

  • I have thoroughly reviewed the code, focusing on its formatting, comments, indentation, and file headers.
  • I have confirmed that the code execution outputs are consistent with those produced by the reference code (Python or Java).
  • The code is designed to be compatible on standard operating systems, including Windows, macOS, and Ubuntu.

result screenshot

Screenshot 2024-04-10 at 17 27 16

@krahets
Copy link
Owner

krahets commented Apr 10, 2024

Hi @juminhong, please complete the check list.

### 堆的长度为 n ,从节点 i 开始,从顶至底堆化 ###
def sift_down(nums, n, i)
while true
# 判断节点i, l, r中值最大的节点,记为ma
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It should be # 判断节点 i, l, r 中值最大的节点,记为 ma

Please ensure the comments are consistent with those in Python.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sorry, I forgot this guide
9801c7a

ma = i
ma = l if l < n and nums[l] > nums[ma]
ma = r if r < n and nums[r] > nums[ma]
# 若节点i最大或索引l, r越界,则无须继续堆化,跳出
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as above

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(nums.length / 2 - 1).downto(0) do |i|
sift_down(nums, nums.length, i)
end
# 从堆中提取最大元素,循环n-1轮
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Save as above

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@krahets
Copy link
Owner

krahets commented Apr 10, 2024

I'm curious why all the spaces in the comments were removed. Did you use a GPT-like tool?

### 堆的长度为 n ,从节点 i 开始,从顶至底堆化 ###
def sift_down(nums, n, i)
while true
# 判断节点 i, l, r 中值最大的节点,记为 ma
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please reduce the code to 2-space indentation

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks,
89273fa

Copy link
Contributor

@khoaxuantu khoaxuantu left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @junminhong, just a little more changes, the rest LGTM

codes/ruby/chapter_sorting/heap_sort.rb Outdated Show resolved Hide resolved
codes/ruby/chapter_sorting/heap_sort.rb Outdated Show resolved Hide resolved
junminhong and others added 2 commits April 29, 2024 09:10
Co-authored-by: khoaxuantu <68913255+khoaxuantu@users.noreply.github.com>
Co-authored-by: khoaxuantu <68913255+khoaxuantu@users.noreply.github.com>
Copy link
Contributor

@khoaxuantu khoaxuantu left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

Copy link
Owner

@krahets krahets left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the PR!

@krahets krahets merged commit 3f857db into krahets:main Apr 30, 2024
4 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants