Sorting algorithms/Shell sort: Difference between revisions

Removed comments line in J language
(Added support for J language)
(Removed comments line in J language)
Line 320:
Create the following script and load it to a J session.
<lang j>shellSort=: verb define
NB. Loop through all values
data=. y
for_xyz. y do.
NB. remove the already sorted values
temp=. xyz_index }. data
NB. Find the index position of the smallest value in the set
nvidx=. xyz_index + temp i. <./ temp
NB. Now switch the values
data=. ((xyz_index, nvidx) { data) (nvidx, xyz_index) } data
end.
NB. Return the sorted data
data
)</lang>
Line 338 ⟶ 333:
[data=. 10 ? 100
51 18 81 46 11 54 74 63 56 76
NB. Now execute the script passing the generated data
shellSort data
11 18 46 51 54 56 63 74 76 81
</lang>
 
Note that J already has a very efficient sorting command and using the code above is not using J effectively.
<lang j> /:~ :/: data
11 18 46 51 54 56 63 74 76 81
</lang>
Anonymous user