Discuss Scratch

BlakeMod_test
Scratcher
4 posts

Sorting Algorithm Optimization

IMAGE FILE HERE

I am currently working on a costume-based 3D Engine here on scratch.
The way the engine organizes objects close to the players vision from the ones that are behind those ones/ far ones, it uses what's called an “insertion-Point algorithm.”

It started out relatively optimized and clean; the more lists I added to the project, though, the more laggy it got. Of course, I am willing to admit that using multiple lists to store multiple points of object data is pretty redundant, but i plan on adding more lists in the future, too.
And for the record: I've already tried combining all data points into singular points of data on one list; it wasn't exactly “better” in any way…

If Anyone finds a fix to this, I will gladly credit you in all engine versions that use this fix!

Here's the project link aswl if any of you need more context (or if you want to remix it ): https://scratch.mit.edu/projects/1258783120/

Last edited by BlakeMod_test (Yesterday 00:14:03)

_Sonion_
Scratcher
39 posts

Sorting Algorithm Optimization

well i found the problem and it's that you need to compile your lists into one
if you can use an algorithm that looks something like this:
define read item [item]
set (i) to [0]
repeat (length of (item))
change [i v] by [1]
if <(letter (i) of (item))=[;]> then
next write
else
set [current write v] to (join (current write) (letter (i) of (item))
end

Last edited by _Sonion_ (Yesterday 01:43:28)

awesome-llama
Scratcher
1000+ posts

Sorting Algorithm Optimization

For insertion sort, one optimisation is to use a binary search to find where an item should be inserted. Alternatively, you can look at other sorting algorithms. Other Scratchers have needed to sort items too, and they've made some pretty fast algorithms optimized for Scratch and shared for all to benefit from.

Something else I want to mention is that reordering all lists is not the only way. You can use a list that stores pointers to items of other lists. You would only need to reorder the pointers. Optionally you could reorder distances and pointers simultaneously (might be faster, not sure). Then accessing the data can be done like this:
(item (item (i) of [pointers v]) of [x v])
_Sonion_
Scratcher
39 posts

Sorting Algorithm Optimization

_Sonion_ wrote:

well i found the problem and it's that you need to compile your lists into one
if you can use an algorithm that looks something like this:
define read item [item]
set (i) to [0]
repeat (length of (item))
change [i v] by [1]
if <(letter (i) of (item))=[;]> then
next write
else
set [current write v] to (join (current write) (letter (i) of (item))
end
Additional info: I may have found the wrong issue but you should still consider my suggestion
Not making an edit to the original because bump
BlakeMod_test
Scratcher
4 posts

Sorting Algorithm Optimization

_Sonion_ wrote:

well i found the problem and it's that you need to compile your lists into one
if you can use an algorithm that looks something like this:
define read item [item]
set (i) to [0]
repeat (length of (item))
change [i v] by [1]
if <(letter (i) of (item))=[;]> then
next write
else
set [current write v] to (join (current write) (letter (i) of (item))
end

Thank you for the advice; I'll be sure to try this out!
BlakeMod_test
Scratcher
4 posts

Sorting Algorithm Optimization

awesome-llama wrote:

For insertion sort, one optimisation is to use a binary search to find where an item should be inserted. Alternatively, you can look at other sorting algorithms. Other Scratchers have needed to sort items too, and they've made some pretty fast algorithms optimized for Scratch and shared for all to benefit from.

Something else I want to mention is that reordering all lists is not the only way. You can use a list that stores pointers to items of other lists. You would only need to reorder the pointers. Optionally you could reorder distances and pointers simultaneously (might be faster, not sure). Then accessing the data can be done like this:
(item (item (i) of [pointers v]) of [x v])

I've thought about implementing something like this before, but I didn't quite know how. Thank you for the documentation!

Powered by DjangoBB