Jump to content

Sorting algorithms/Bubble sort: Difference between revisions

→‎[[Toka]]: Updated to work with Toka R1
(add E example)
(→‎[[Toka]]: Updated to work with Toka R1)
Line 379:
[[Category:Toka]]
 
Toka implementsdoes not have a bubble sort inpredefined, abut libraryit moduleis (bsort).easy Theto code fora simple one:
this function follows:
 
#! A simple Bubble Sort function
value array
value| array count changed |
value changed
[ ( address count -- )
to count to array
count 0
[ count 0
[ i array array.get-element i 1 + array array.get-element 2dup >
[ i array array.put-element i 1 + array array.put-element ]
[ 2drop ] ifTrueFalse
] +iteratecountedLoop
count 1 - to count
] +iteratecountedLoop
] 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 -- )
0 swap [ dup i swap array.get-element . ] +iteratecountedLoop drop cr
] is .array
Line 413 ⟶ 404:
#! Fill it with random values
20 1 foo array.put-element
50 2 foo array.put-element
650 3 foo array.put-element
120 4 foo array.put-element
110 5 foo array.put-element
101 6 foo array.put-element
1321 7 foo array.put-element
1310 8 foo array.put-element
987 9 foo array.put-element
10 10 foo array.put-element
#! Display the array, sort it, and display it again
Anonymous user
Cookies help us deliver our services. By using our services, you agree to our use of cookies.