Hey! So I am new to scratch, and working on a project to create a function "bubble sort" which sorts any given list L, it should be able to sort any list of any random length.
I am having some problems with this and I was wondering if anyone had any ideas that might be helpful?
Offline
[scratchblocks]
when gf clicked
set [pass v] to [0]
set [swaps v] to [0]
repeat until <<(pass) > [0]> and <(swaps) = [0]>>
set [item v] to [0]
change [pass v] by (1)
set [swaps v] to [0]
repeat ((length of [data v]) - (1))
change [item v] by (1)
if <(item ((item) + (1)) of [data v]) < (item (item) of [data v])>
set [value v] to (item ((item) + (1)) of [data v])
replace item ((item) + (1)) of [data v] with (item (item) of [data v])
replace item (item) of [data v] with (value)
change [swaps v] by (1)
end
end
end
Offline
lolakg wrote:
when gf clicked set [pass v] to [0] set [swaps v] to [0] repeat until <<(pass) > [0]> and <(swaps) = [0]>> set [item v] to [0] change [pass v] by (1) set [swaps v] to [0] repeat ((length of [data v]) - (1)) change [item v] by (1) if <(item ((item) + (1)) of [data v]) < (item (item) of [data v])> set [value v] to (item ((item) + (1)) of [data v]) replace item ((item) + (1)) of [data v] with (item (item) of [data v]) replace item (item) of [data v] with (value) change [swaps v] by (1) end end end
Fixed.
Offline
Bubble sort is here using the List Bubble sort. not sure you can make it take a list as a variable even in Scratch 2.0 where the variables seem to be numerics or text rather than objects.
http://scratch.mit.edu/projects/SarkyKeshy/3190837
Offline