I know this is common knowledge in Scratch, but I didn't want to take scripts from someone else so . . .
Here is my code:
set check to 0
repeat until check > than length of list
change check by 1
set possible to check + 1
if item check of list > than item possible of list
insert item possible of list at item 1 of list
delete possible of list
Can someone help me fix it?
**NOTE: I am using decimals AND am ordering it from LEAST to GREATEST.
Offline
You only had a couple of blocks wrong:
[when flag clicked] [set [check] to [0]] [repeat until <(check) > (length of [numbers])>] [][change [check] by (1)] [][set [possible] to ((check) + (1))] [][if <(item (check) of [numbers]) > (item (possible) of [numbers])>] [][][insert (item (possible) of [numbers]) at (1) of [numbers]] [][][delete ((possible) + (1) of [numbers]] [][][set [check] to [0]] [][_________________________________________________________________] [______________________________________________]
1. You need to delete item possible + 1 of the list, as you just added another item to the list before item possible.
2. You need to set check to 0, otherwise you may make 5 go to the beginning of the list, then 6.
It takes a while sometimes, but it always works in the end.
Last edited by The-Whiz (2009-10-10 18:41:50)
Offline
Search projects for "bubble sort"
Offline
The-Whiz wrote:
You only had a couple of blocks wrong:
Code:
[when flag clicked] [set [check] to [0]] [repeat until <(check) > (length of [numbers])>] [][change [check] by (1)] [][set [possible] to ((check) + (1))] [][if <(item (check) of [numbers]) > (item (possible) of [numbers])>] [][][insert (item (possible) of [numbers]) at (1) of [numbers]] [][][delete ((possible) + (1) of [numbers]] [][][set [check] to [0]] [][_________________________________________________________________] [______________________________________________]1. You need to delete item possible + 1 of the list, as you just added another item to the list before item possible.
2. You need to set check to 0, otherwise you may make 5 go to the beginning of the list, then 6.
It takes a while sometimes, but it always works in the end.
Thanks! You always know what to do!
Do you like my new signature? I finally got around to changing it.
Offline
Offline
shadow_7283 wrote:
The-Whiz wrote:
You only had a couple of blocks wrong:
Code:
[when flag clicked] [set [check] to [0]] [repeat until <(check) > (length of [numbers])>] [][change [check] by (1)] [][set [possible] to ((check) + (1))] [][if <(item (check) of [numbers]) > (item (possible) of [numbers])>] [][][insert (item (possible) of [numbers]) at (1) of [numbers]] [][][delete ((possible) + (1) of [numbers]] [][][set [check] to [0]] [][_________________________________________________________________] [______________________________________________]1. You need to delete item possible + 1 of the list, as you just added another item to the list before item possible.
2. You need to set check to 0, otherwise you may make 5 go to the beginning of the list, then 6.
It takes a while sometimes, but it always works in the end.Thanks! You always know what to do!
Do you like my new signature? I finally got around to changing it.
Yeah, although I'd rather have limes.
Offline