Sorting algorithms/Shell sort: Difference between revisions
Content added Content deleted
(added ocaml) |
(Added support for J language) |
||
Line 316: | Line 316: | ||
END PROGRAM Shellsort</lang> |
END PROGRAM Shellsort</lang> |
||
=={{header|J}}== |
|||
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> |
|||
To validate: |
|||
<lang j>NB. We generate 10 random numbers between 0 and 99 and assign to a variable |
|||
[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> |
|||
=={{header|Java}}== |
=={{header|Java}}== |