Anonymous user
Sorting algorithms/Bubble sort: Difference between revisions
m
→[[Toka]]: Added full example and further comments
(Added Toka) |
m (→[[Toka]]: Added full example and further comments) |
||
Line 352:
[[Category:Toka]]
Toka implements bubble sort in a library module (bsort). The code for
value array value count▼
this function follows:
value changed
[ ( address count -- )
to count to array
count
[ count
▲ count
▲ i array get-element i 1+ array get-element 2dup >
[ i array put-element i 1+ array put-element ]
[ 2drop ] ifTrueFalse
Line 367 ⟶ 370:
] +iterate
] is bsort
And an example of using it to sort a 10 element array:
#! Include the needed libraries
needs values
needs bsort
#! Code to display an array
[ ( array count -- )
[ dup i swap get-element . ] +iterate drop cr
] is .array
#! Create a 10-cell array
10 cells is-array foo
#! Fill it with random values
20 1 foo put-element
50 2 foo put-element
650 3 foo put-element
120 4 foo put-element
110 5 foo put-element
101 6 foo put-element
1321 7 foo put-element
1310 8 foo put-element
987 9 foo put-element
10 10 foo put-element
#! Display the array, sort it, and display it again
foo 10 .array
foo 10 bsort
foo 10 .array
|