Jump to content

Sorting algorithms/Heapsort: Difference between revisions

J: minor cleanup
(+Icon+Unicon)
(J: minor cleanup)
Line 738:
{{eff note|J|/:~}}
'''Translation of the pseudocode'''
<lang j>siftDownswap=: 4 : 0{`(|.@[)`]}
 
's e'=. x
siftDown=: 4 : 0
z=.y
'c e'=.s x
while. e > c=.1+2*s=.c do.
if. e > 1+c dobefore=. if. c <&({&zy) c+1 do. c=.c+1 end. end.
if. se <&({&z)> 1+c do. zc=. z {`(|.@[)`]}~c+ c,s else.before break.c+1 end.
if. s before c do. y=. (c,s) swap y else. break. end.
end.
z
y
)
 
heapSort =: 3 : 0
if. 1>: c=. # y do. y return. end.
z=. (] siftDown ~c,~[)&.>/ (<y)c,~]&.>each i.1+<.-:c-%2 ),<y NB. heapify
> (](][ siftDown {`(|.@[)`]}~) 0,[swap)&.>/ z(0,~]&each}.>1+i.c-1),z
)</lang>
'''Examples'''
6,962

edits

Cookies help us deliver our services. By using our services, you agree to our use of cookies.