Discuss Scratch
- Discussion Forums
- » Help with Scripts
- » Sorting algorithm
- griefercube
-
Scratcher
500+ posts
Sorting algorithm
So I got this script to sort in this project:

But it ended up like this:
Any help?
(It uses division detection to sort)

But it ended up like this:

Any help?
(It uses division detection to sort)
- deck26
-
Scratcher
1000+ posts
Sorting algorithm
Please share the project rather than a script image - it's much easier to play around with the code to work out what it's doing. Also don't bump after less than 24 hours please.
- griefercube
-
Scratcher
500+ posts
Sorting algorithm
Ah yes, the project-
It’s on the top, the blue link
It’s on the top, the blue link
- deck26
-
Scratcher
1000+ posts
Sorting algorithm
So can you provide a link to this sort method? Should you be checking
if <[1] > (var)> thenwhere var is your .2 variable? Or should the item on the left of the greater than be a variable?
end
- deck26
-
Scratcher
1000+ posts
Sorting algorithm
That's a link to your project - I want a link to something describing the method.
- griefercube
-
Scratcher
500+ posts
Sorting algorithm
You meant this? https://en.m.wikipedia.org/wiki/Divide-and-conquer_algorithm
- deck26
-
Scratcher
1000+ posts
Sorting algorithm
Which links to this https://en.m.wikipedia.org/wiki/Quicksort which is what I think you're implementing. Please confirm.
- griefercube
-
Scratcher
500+ posts
Sorting algorithm
Not quite. More like https://en.m.wikipedia.org/wiki/Binary_search_algorithm
- deck26
-
Scratcher
1000+ posts
Sorting algorithm
Not quite. More like https://en.m.wikipedia.org/wiki/Binary_search_algorithmThat's for searching a sorted list, not for sorting.
- griefercube
-
Scratcher
500+ posts
Sorting algorithm
Hmm, I don’t exactly know the name but mine works somewhat like this, finding where to put the number after I have a list of other sorted numbers at the back of the list.
(Edit) I meant searching for where to place the number
(Edit) I meant searching for where to place the number
Last edited by griefercube (Sept. 27, 2021 12:39:06)
- deck26
-
Scratcher
1000+ posts
Sorting algorithm
I need to see a reference to see what you've missed in your code or done wrong. Hard to help otherwise unless you find someone who recognises the method.
Most of your ceiling/round/floor blocks are currently unnecessary as you're working with integer values. That includes where you set up the list of values.
Why do you have a custom block to do the sort but not run it? It seems to be the same code in your green flag script. This is just confusing.
Most of your ceiling/round/floor blocks are currently unnecessary as you're working with integer values. That includes where you set up the list of values.
Why do you have a custom block to do the sort but not run it? It seems to be the same code in your green flag script. This is just confusing.
- griefercube
-
Scratcher
500+ posts
Sorting algorithm
The custom block was there first, and the green flag is just for step purposes.
Found another explanation: https://www.delftstack.com/tutorial/algorithm/binary-sort/
Found another explanation: https://www.delftstack.com/tutorial/algorithm/binary-sort/
- deck26
-
Scratcher
1000+ posts
Sorting algorithm
I was going to concentrate on the green flag script since that is what is easiest to run but you've now changed that. That's making it hard to help!
The web page you linked sorts the list by taking each element in turn and inserting in the correct place within the number of elements looked at so far. So if you're sorting the third element it only cares about putting it in the right place compared to the first two that are already sorted relative to each other. It finds the correct position by using a binary search to make it more efficient than just stepping through all the values.
That shouldn't be hard to implement. My guess is that where you're going wrong is at some stage you don't set the limits for the search properly and fail to check the new value against one position in the ‘already sorted’ set.
For debugging I'd recommend either working initially with a smaller set of values - eg 15 or so - or after each item is supposedly placed correctly run a test to ensure the values you have ‘sorted’ are actually in order and stop if not.
The web page you linked sorts the list by taking each element in turn and inserting in the correct place within the number of elements looked at so far. So if you're sorting the third element it only cares about putting it in the right place compared to the first two that are already sorted relative to each other. It finds the correct position by using a binary search to make it more efficient than just stepping through all the values.
That shouldn't be hard to implement. My guess is that where you're going wrong is at some stage you don't set the limits for the search properly and fail to check the new value against one position in the ‘already sorted’ set.
For debugging I'd recommend either working initially with a smaller set of values - eg 15 or so - or after each item is supposedly placed correctly run a test to ensure the values you have ‘sorted’ are actually in order and stop if not.
- griefercube
-
Scratcher
500+ posts
Sorting algorithm
Yes, it’s using a binary search to search for the value to put it in.
And, now I’ve fixed most of it but the last value, for some reason, ALMOST ALWAYS go wrong and get stuck at the last.
And, now I’ve fixed most of it but the last value, for some reason, ALMOST ALWAYS go wrong and get stuck at the last.
- deck26
-
Scratcher
1000+ posts
Sorting algorithm
It's the first item from the unsorted list which ends up at the end.
If you reduce this to a shorter list as i suggested it is easier to debug. A list of 10 items and only running the repeat loop twice suggests the following.
First item is added to the end of the list. Second item is compared with the old last item and put in the right place relative to that but the last item is not checked. That looks to be what happens consistently. So from there you should be able to mentally work through the code and work out what list items the code actually looks at.
If you reduce this to a shorter list as i suggested it is easier to debug. A list of 10 items and only running the repeat loop twice suggests the following.
First item is added to the end of the list. Second item is compared with the old last item and put in the right place relative to that but the last item is not checked. That looks to be what happens consistently. So from there you should be able to mentally work through the code and work out what list items the code actually looks at.
- Discussion Forums
- » Help with Scripts
-
» Sorting algorithm


