The parity should be zero if the vector is not a true permutation, i.e.

    julia> sign([3, 4, 5, 4, 1])
    -1

would at least be misleading. Could result in an ERROR, but I still
suggest to return 0.

On Tuesday, February 10, 2015 at 3:24:19 PM UTC+1, Stefan Karpinski wrote:
>
> Thanks for the code – would you be willing to contribute it under the MIT 
> license?
>
> On Tue, Feb 10, 2015 at 9:21 AM, Dawid Crivelli <[email protected] 
> <javascript:>> wrote:
>
>> I have a short code for checking the sign of a permutation for personal 
>> use. It's O(L^2) where L is the length of the permutation vector, but it's 
>> perfectly fine for L in the few tens range.
>>
>> function sign{T<:Integer}(perm::AbstractVector{T})
>>     L = length(perm)
>>     crosses = 0
>>     for i = 1:L
>>         for j = i+1 : L
>>             crosses += perm[j] < perm[i]
>>         end
>>     end
>>     return iseven(crosses) ? 1 : -1    
>> end
>>
>>
>>
>

Reply via email to