Revision as of 13:28, 9 September 2017 by 79brue (talk | contribs) (←Created page with "Let's learn about sorting numbers on Scratch. == Easy: Bubble Sort == Bubble sort is the easiest way to sort numbers. However, it is very slow. For programmers, the time ...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

Let's learn about sorting numbers on Scratch.

Easy: Bubble Sort

Bubble sort is the easiest way to sort numbers. However, it is very slow. For programmers, the time complexity of this sort is O(n*n).

You can watch a video explaining Bubble Sort here.


define sort
set [i v] to (1)
repeat ( length of [numbers v] )
set [j v] to (1)
repeat ((length of [numbers v]) - (i))
if ((item (j) of [numbers v]) > (item ((j)+(1)) of [numbers v])) then
set [temp v] to (item (j) of [numbers v])
replace item (j) of [numbers v] with (item ((j)+(1)) of [numbers v])
replace item ((j)+(1)) of [numbers v] with (temp)

end
change [j v] by (1)

end
change [i v] by (1)

end