Skip to content

feat(gslice): Add partial sort for gslice#19

Merged
XQ-Gang merged 4 commits into
bytedance:mainfrom
limits220284:feature/gslice/partial_sort
Aug 29, 2025
Merged

feat(gslice): Add partial sort for gslice#19
XQ-Gang merged 4 commits into
bytedance:mainfrom
limits220284:feature/gslice/partial_sort

Conversation

@limits220284
Copy link
Copy Markdown
Contributor

@limits220284 limits220284 commented Jul 15, 2025

What

PartialSort sorts only the first k smallest elements of a slice in ascending order. The rest of the elements are left unordered.

  • If k is 0 or less, nothing happens.
  • If k is greater than or equal to the slice length, the whole slice is sorted.

Why

This is useful when you only need the smallest k elements in order, instead of sorting the entire slice. It’s more efficient for large datasets when you only care about the top results.

Motivation

Real World Example

  • In a batch processing system, may want to select the next k shortest jobs to run, without needing to sort all pending jobs.

Aligns with similar patterns in other languages

  • std::partial_sort in Cpp
  • heapq.nsmallest in Python

@CLAassistant
Copy link
Copy Markdown

CLAassistant commented Jul 15, 2025

CLA assistant check
All committers have signed the CLA.

@codecov
Copy link
Copy Markdown

codecov Bot commented Jul 15, 2025

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 95.79%. Comparing base (f5f07b9) to head (eee3c16).
⚠️ Report is 2 commits behind head on main.

Additional details and impacted files
@@           Coverage Diff           @@
##             main      #19   +/-   ##
=======================================
  Coverage   95.79%   95.79%           
=======================================
  Files          34       34           
  Lines        4443     4449    +6     
=======================================
+ Hits         4256     4262    +6     
  Misses        144      144           
  Partials       43       43           
Flag Coverage Δ
go-1.18.x 94.92% <100.00%> (+<0.01%) ⬆️
go-1.19.x 94.65% <100.00%> (-0.27%) ⬇️
go-1.20.x 95.25% <100.00%> (-0.07%) ⬇️
go-1.21.x 95.25% <100.00%> (-0.40%) ⬇️
go-1.22.x 95.32% <100.00%> (+<0.01%) ⬆️
go-1.23.x 95.32% <100.00%> (+0.07%) ⬆️
go-1.24.x 95.25% <100.00%> (+0.07%) ⬆️
unittests 95.79% <100.00%> (+<0.01%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@XQ-Gang XQ-Gang requested review from SilverRainZ and XQ-Gang July 15, 2025 15:59
@limits220284 limits220284 force-pushed the feature/gslice/partial_sort branch from 2a810cc to cb39bfc Compare July 15, 2025 16:27
@SilverRainZ
Copy link
Copy Markdown
Collaborator

Hello, thank you for your contribution! Can you please give some real-world cases in the description?

@XQ-Gang, I have been away from Go's code for a long time, and I don't know the current internal situation of ByteDance. I am not sure whether a function should be added (of course I will try to give suggestions). I think this decision should be made by you.

@limits220284
Copy link
Copy Markdown
Contributor Author

Hello, thank you for your contribution! Can you please give some real-world cases in the description?

@XQ-Gang, I have been away from Go's code for a long time, and I don't know the current internal situation of ByteDance. I am not sure whether a function should be added (of course I will try to give suggestions). I think this decision should be made by you.

I have updated the description. If anything else is needed, please let me know.

@XQ-Gang
Copy link
Copy Markdown
Collaborator

XQ-Gang commented Aug 5, 2025

Hello, thank you for your contribution! Can you please give some real-world cases in the description?
@XQ-Gang, I have been away from Go's code for a long time, and I don't know the current internal situation of ByteDance. I am not sure whether a function should be added (of course I will try to give suggestions). I think this decision should be made by you.

I have updated the description. If anything else is needed, please let me know.

@limits220284 有几个问题可以讨论

  1. 如果想要获取topk元素,为什么不直接提供一个类似nlargest/nsmallest函数,而是使用partial_sort,还需要二次操作才能获取。
  2. func PartialSort[T constraints.Ordered](s []T, k int) 的语法只能处理升序排序一种场景,不具备通用性,如果想要使用,建议多提供PartialSortBy有less函数输入,或者提供明确的取最大N个或者最小N个。

@limits220284 limits220284 force-pushed the feature/gslice/partial_sort branch 2 times, most recently from ad8eda5 to 54f88d2 Compare August 13, 2025 07:33
@limits220284 limits220284 force-pushed the feature/gslice/partial_sort branch from 54f88d2 to 97771ed Compare August 13, 2025 08:51
@XQ-Gang XQ-Gang merged commit 81a0093 into bytedance:main Aug 29, 2025
11 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.

5 participants