Download code

Jump to: navigation, search

Back to Insertion_sort_(Forth)

Download for Windows: single file, zip

Download for UNIX: single file, zip, tar.gz, tar.bz2

insertion-sort.fs

 1 (* The authors of this work have released all rights to it and placed it
 2 in the public domain under the Creative Commons CC0 1.0 waiver
 3 (http://creativecommons.org/publicdomain/zero/1.0/).
 4 
 5 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 6 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 7 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
 8 IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
 9 CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
10 TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
11 SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
12 
13 Retrieved from: http://en.literateprograms.org/Insertion_sort_(Forth)?oldid=10829
14 *)
15 
16 : cell-  1 cells - ;
17 
18 : insert ( start end -- start )
19   dup @ >r ( r: v )
20   begin
21     2dup <
22   while
23     r@ over cell- @ <
24   while
25     cell-
26     dup @ over cell+ !
27   repeat then
28   r> swap ! ;
29 
30 : sort ( array len -- )
31   1 ?do
32     dup i cells + insert
33   loop drop ;
34 
35 create test 7 , 3 , 0 , 2 , 9 , 1 , 6 , 8 , 4 , 5 ,
36 
37 : main
38   test 10 sort
39   10 0 do test i cells + @ . loop cr ;
40 
41 main bye


hijacker
hijacker
hijacker
hijacker