More efficient sorting algorithm

I have been using table.sort in many of my projects and I must say it’s quite good however for the people who would like to micro-optimise this module is perfect for you as it seperates different sorting algorithms. This goes in depth of each sorting algorithm.

Get the model here

Bubble sort and Insertion Sort

Bubble sort…

  1. looks at the first two items in the list.
  2. If they’re in the right order, you don’t have to do anything. If they’re in the wrong order, swap them.
  3. Move on to the next pair of items (the 2nd and 3rd entries) and repeat step 2
  4. Repeat steps 1-4 until there are no swaps in a pass.

Example:

66, 21, 38, 15, 89, 49 Compare 66 and 21 - swap them
21, 66, 38, 15, 89, 49 Compare 66 and 38 - swap them
21, 38, 66, 15, 89, 49 Compare 66 and 15 - swap them
21, 38, 15, 66, 89, 49 Compare 66 and 89 - no swap
21, 38, 15, 66, 49, 89 End of first pass

After the 2nd pass the order of the numbers will be 21,15, 38, 49, 66, 89
After the 3rd pass the order of the numbers will be 15, 21, 38, 49, 66, 89
There are no swaps in the 4th pass so the list has been sorted 15, 21, 38, 49, 66, 89

The bubble sort is considered to be one of the simplest sorting algorithms as it only ever focuses on two items rather than the whole list of items. In this algorithm I have combined the Insertion Sort into the Bubble Sort as they both compare two values.

Pros

  • It’s an efficient way to check if a list is already in order. For a list of n items you only have to do one pass of n - 1 comparisons to check if the list is ordered or not.
  • Doesn’t use very much memory as all the sorting is done using the original list.

Cons

  • It’s an inefficient way to sort a list - for a list of n items, the worst case scenario would involve you doing n(n - 1) / 2 comparisons.
  • Due to being inefficient, the bubble sort algorithm does not cope well with a very large list of items.

Merge sort

A merge sort…

  1. Splits the list in half
  2. keep repeating step 1 on each sub-list until all the lists only contain one item.
  3. Merge pairs of sub-lists so that each sub-list has twice as many items. Each time you merge sub-lists, sort the items into the right order

Example:

  1. Split the original list of 8 items into two lists, the second list should start at the (8 + 1) / 2 = 4.5 = 5th term.
  2. Carry on splitting the sub-lists until each list only has one item in it.
  3. Merge and order the sub-lists back together.
  4. Keep merging until you only have one list

This merge writes these letters in alphabetical order in our case the algorithm will organise strings in alphabetical order based on the first letter of the string and organise them in an array.

You’ll often be unable to split or merge lists evenly. For exampe, sometimes you’ll have to merge a list containing two items with a list containing one item to make a list of three items.

Pros

  • It has a very consistent running time regardless of how ordered the items in the original array are.

Cons

  • It’s slower than other algorithms for small lists.
  • Even if the list is already sorted it still goes through the whole splitting and merging process if you call it by accident
  • It uses more memory than the other sorting algorithms in order to create the seperating lists.

Thanks for reading if you have any feedback or suggestions don’t hesitate to reply! :happy4:

2 Likes

How much faster could sorting be with this module with ~100 enteries?

1 Like

There’s no exact time, lua runs code in practically less than milliseconds but I would say this method is faster as it splits your sorting algorithm choices up in order to suit different lengthed arrays

i don’t think you’re gonna be beating the introsort (?) that table.sort uses internally, as it’s pretty fast and also using c++ internally

1 Like

Yes it’s fast however it will not be very fast on big tables unlike bubble sort.

Hate to burst your bubble but there’s just no way you can beat the engine optimized version when limited to luau.

Fun project though, and a good way to learn algorithms. I did some benchmarking for you so you can see. Tables were filled with random numbers from 1 to 10000000.

First with 10k elements, results in seconds.
0.010520299896597862 – Your merge
1.3290188000537455 – Your bubble
2.8081928002648056 – Your insertion
0.000624599866569042 – Table Sort

At 50k
– Your bubble got timeouted
– Your insertion got timeouted
0.05599609995260835 – Your merge
0.0035272003151476383 – Table Sort

At 150k
0.19749299995601177 – Your merge
0.0116670997813344 – Table Sort

And lastly at 400k
0.536470000166446 – Your merge
0.03398479986935854 – Table Sort

Tried with 800k too but neither could sort that without timing out.

Yes it’s fast however it will not be very fast on big tables unlike bubble sort.

Bubble sort is never really that fast in practice, and unlike what you said it actually scales very very poorly. The bigger the table, the longer the potential distance is, but bubble sort only moves one index at a time.

5 Likes

Ah okay, thanks for the explaination

1 Like