-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
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
partialsort! is broken on 1.6 #40338
Comments
Naively, reading the docs:
And trying this out: for k in 1:length(v)
vc = copy(v)
partialsort!(vc, k, rev=true)
@assert vc[k] == sort(v; rev=true)[k]
end looks like it is ok. So, could you be a bit more specific about what the bug is? Maybe |
Look at the difference between Both are in reverse, but:
Your assert just happens to be for Look at the outputs in this sequence: for k in 7:10
vc = copy(v)
partialsort!(vc, k, rev=true)
@show k
display(vc)
println()
end There is a break between 8 and 9 where the behaviour switches. The problem is the docs don't say which way it should be sorted! My assumption was from the start of the vector. Probably it's being tested with |
Heres the output, to save you the hassle k = 7
25-element Vector{Float32}:
2.6137104
1.0
1.0
0.70710677
0.5
0.5
0.4472136
0.4472136
0.35355338
0.24253564
0.31622776
0.25
0.33333334
0.2773501
0.2236068
0.33333334
0.31622776
0.2773501
0.23570228
0.2
0.25
0.24253564
0.2236068
0.0
0.17677669
k = 8
25-element Vector{Float32}:
2.6137104
1.0
1.0
0.70710677
0.5
0.5
0.4472136
0.4472136
0.35355338
0.24253564
0.31622776
0.25
0.33333334
0.2773501
0.2236068
0.33333334
0.31622776
0.2773501
0.23570228
0.2
0.25
0.24253564
0.2236068
0.0
0.17677669
k = 9
25-element Vector{Float32}:
0.5
1.0
0.5
2.6137104
0.4472136
1.0
0.70710677
0.4472136
0.35355338
0.33333334
0.33333334
0.31622776
0.31622776
0.2773501
0.2773501
0.25
0.25
0.24253564
0.24253564
0.23570228
0.2236068
0.2236068
0.2
0.17677669
0.0
k = 10
25-element Vector{Float32}:
0.5
1.0
0.5
2.6137104
0.4472136
1.0
0.70710677
0.4472136
0.35355338
0.33333334
0.33333334
0.31622776
0.31622776
0.2773501
0.2773501
0.25
0.25
0.24253564
0.24253564
0.23570228
0.2236068
0.2236068
0.2
0.17677669
0.0 |
Again, looking at the docs (as I wrote in #40338 (comment)) the function only seems to make a claim about what will happen at |
You want the e.g. highest That's how it worked previously - I know because 1.6 has broken a published algorithm. |
Just so I understand, could you point out to me what the result is that is contrary to the documentation. |
The docs were always bad - this has changed the behaviour. I assume what we want is: https://en.wikipedia.org/wiki/Partial_sorting We don't have that anymore, but we used to |
This algorithm now gives either the smallest sorted over k:end or the largest sorted over 1:k, depending on an arbitrary cutoff. Thats not what a partial sort is. It should be that |
Yeah, if you pass in julia> for k in 1:length(v)
vc = copy(v)
partialsort!(vc, 1:k, rev=true)
@assert vc[1:k] == sort(v; rev=true)[1:k]
end |
Ehhhh ok. The docs specify what is returned, and don't really mention what happens to the vector. I've never actually used the return value... instead reading the sorted section of the underlying array. I guess my misconception of that behavior was reinforced by it actually working how I thought it should for a few years... - it just sorted up to k if you passed k (not 1:k), and whatever the return value was wasn't that relevant. We should probably make it clear that the algorithm is free to sort the array any way that it likes to get that return value. Its also odd that it's sorting the long end of the array now, but maybe there's a reason |
Maybe, to clarify:
|
And @KristofferC apologies for just not getting the range part!! Somehow it couldn't click that it was just luck that the algorithm had been passing tests for a few years, not actually design. This was the end of a long afternoon just trying to find how this had broken... |
Just as a point of reference: With the original name, the meaning of |
Using Julia 1.6.0
partialsort!
sorting withrev
breaks above a certain vector length >= ~22 and value ofk
>= 9, although these may interact.It's not always clearly reproducible with
rand
, so I'm spelling this out with the data that triggered it.With
k = 9
and above, the vector is sorted from the end back to 9. Atk = 8
, it is sorted from the start to 8.The text was updated successfully, but these errors were encountered: