This little bucket sorter I used in helping somebody a while back. I find it entertaining to make it execute organizing like 100,000 numbers. (Pretty sure 600K numbers is 40 something billion repetitions of the for loop xD)
Anyways, enjoy.
Turing: | % ________ Record _________
type BucketData :
record
num, name : string
end record
% ________ Get the Input _________
var n : int
put "How many numbers would you like to organize?"
get n
var number : array 1 .. n of int
var buckets : flexible array 1 .. 0 of BucketData
View.Set ("offscreenonly")
% ________ How many buckets we will have ________
new buckets, ceil (n / 10)
% _________ Intialize ________
for i : 1 .. upper (number )
% Random number between 1 and whatever n is
number (i ) := Rand.Int (1, n )
end for
for i : 1 .. upper (buckets )
buckets (i ).num := ""
buckets (i ).name := "Bucket " + intstr ((i * 10) - 9) + " - " + intstr (i * 10)
end for
% ________ Organize the Bucket (by 10s) ________
% Go through by 10s
for j : 10 .. upper (number ) + 10 by 10
% Go through the array
for i : 1 .. upper (number )
if number (i ) > (j - 10) and number (i ) <= j then
% We are using a string so we don't add our numbers, and we
% use | so we can tell the numbers apart
buckets ((j div 10)).num + = intstr (number (i )) + "|"
end if
end for
cls
put "Loading: ", (j/ (upper(number ) + 10)) * 100, "%"
View.Update
end for
View.Set ("text")
% _________ Show the buckets __________
for i : 1 .. upper (buckets )
put buckets (i ).name + ": " + buckets (i ).num
end for
View.Update
|
|