This is a read-only archive of the old Scratch 1.x Forums.
Try searching the current Scratch discussion forums.

#1 2009-10-10 16:33:37

shadow_7283
Scratcher
Registered: 2007-11-07
Posts: 1000+

Sort

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

 

#2 2009-10-10 18:36:29

The-Whiz
Scratcher
Registered: 2007-07-09
Posts: 1000+

Re: Sort

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.

Last edited by The-Whiz (2009-10-10 18:41:50)

Offline

 

#3 2009-10-10 18:50:49

archmage
Scratcher
Registered: 2007-05-18
Posts: 1000+

Re: Sort

Search projects for "bubble sort"


Hi, I am Archmage coder extraordinaire. I do Scratch,pascal,java,php,html, AS2 and AS3. Leave me a message if you want coding advice. Also check out my personal website, lots of good stuff about web development, Flash, and Scratch (v1 and v2) !

Offline

 

#4 2009-10-10 21:05:31

shadow_7283
Scratcher
Registered: 2007-11-07
Posts: 1000+

Re: Sort

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

 

#5 2009-10-10 21:06:42

TheSaint
Scratcher
Registered: 2008-11-04
Posts: 1000+

Re: Sort

Yeah, thats a good siggy.

Offline

 

#6 2009-10-11 08:51:25

The-Whiz
Scratcher
Registered: 2007-07-09
Posts: 1000+

Re: Sort

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 limessmile

Offline

 

Board footer