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 Swift codes for chapter_sorting articles #313

Merged
merged 6 commits into from Jan 30, 2023

Conversation

nuomi1
Copy link
Contributor

@nuomi1 nuomi1 commented Jan 30, 2023

If this PR is related to coding or code translation, please fill out the checklist.

  • I've tested the code and ensured the outputs are the same as the outputs of reference codes.
  • I've checked the codes (formatting, comments, indentation, file header, etc) carefully.
  • The code does not rely on a particular environment or IDE and can be executed on a standard system (Win, macOS, Ubuntu).

@vercel
Copy link

vercel bot commented Jan 30, 2023

The latest updates on your projects. Learn more about Vercel for Git ↗︎

1 Ignored Deployment
Name Status Preview Comments Updated
hello-algo ⬜️ Ignored (Inspect) Jan 30, 2023 at 3:15PM (UTC)

@nuomi1
Copy link
Contributor Author

nuomi1 commented Jan 30, 2023

result of `bubble_sort.swift`

冒泡排序完成后 nums = [1, 1, 2, 3, 4, 5]
冒泡排序完成后 nums1 = [1, 1, 2, 3, 4, 5]
result of `insertion_sort.swift`

插入排序完成后 nums = [1, 1, 2, 3, 4, 5]
result of `quick_sort.swift`

快速排序完成后 nums = [0, 1, 2, 3, 4, 5]
快速排序(中位基准数优化)完成后 nums1 = [0, 1, 2, 3, 4, 5]
快速排序(尾递归优化)完成后 nums2 = [0, 1, 2, 3, 4, 5]
result of `merge_sort.swift`

归并排序完成后 nums = [0, 1, 2, 3, 4, 5, 6, 7]
result of `radix_sort.swift`

基数排序完成后 nums = [3, 4, 12, 23, 192, 788]

func quickSortMedianThree(nums: [Int], left: Int, mid: Int, right: Int) -> Int {
// 使用了异或操作来简化代码
// 异或规则为 0 ^ 0 = 1 ^ 1 = 0, 0 ^ 1 = 1 ^ 0 = 1
if (nums[left] < nums[mid]) ^ (nums[left] < nums[right]) {
Copy link
Owner

Choose a reason for hiding this comment

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

I think we could use != instead so that we don't have to define the ^ operator for bool.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done.

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.

Awesome! Please address the comments.

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!

@krahets krahets merged commit f43f7a6 into krahets:master Jan 30, 2023
@krahets krahets added the code Code-related label Jan 30, 2023
@nuomi1 nuomi1 deleted the feature/chapter_sorting-Swift branch January 31, 2023 00:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
code Code-related
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants