Bubble sort list without an extension (numbers/strings)

You list length is less than 2 and therefore this error pop up. btw, your blocks looks different to mine. Let me check.

And can you show the example list?


This is the result i get. I get the result correct but that run time error pops out evrytime

Please help
It’s not working

Great post! Thanks for the tips on sorting. I came across this post looking for a way to sort a dictionary by the key. Your logic helped me put together a couple of procedures for sorting dictionaries. I’m sharing my blocks for anyone else that may be looking for more guidance on sorting and sorting dictionaries. I also made some changes to your Bubble Sort to make it more readable (at least for me :wink:).

In the bubble sort, I eliminated the global variables and the extra inputs without losing any functionality. The only input to the Bubble Sort is an unsorted list.

I started with a “foreach item in list” to see if any list item contains non-numeric data to determine if we are sorting numbers or text. This eliminated the need to pass in “itemType”.

I condensed a bunch of the if statements down to one if using" And"s and “Or” to determine if a swap of items is needed. Below is the expanded blocks to the above if statement. Basically, one side of the “or” is for numeric comparison and the other side for not numeric.

The sort was simplified more by eliminating the “order” option. The results will be ascending. If you need results in descending order, attached the results to a “reverse list” block.

blocks3

This next procedure is to sort a dictionary by the key.

It just take the keys and runs them though the Bubble Sort procedure and then rebuilds a dictionary with the sorted keys. In this procedure, I have an optional “descending” input, default will be ascending. For descending, just attach a “true” block.

blocks5

For ascending, either attached “false” to descending or leave it without anything attached.

4 Likes

Also, the “if length of list >= 2” is not needed. If the list has one item or is empty, the “for each index1” block will have a “to” value less than “from” value. This will just cause the for each to have no iterations and the returned result will just be the original list.

1 Like

Wow! That’s awesome :smiley: Thanks for the appreciation and implementation of the bubble sort logic.

I’ve always loved to learn new logic. Very clever indeed!

2 years ago I’m still a newbie to programmings. At that time I really didn’t think of optimization stuff :sweat_smile:

1 Like