Jump to content




A Few Sorting Algorithms

utility

  • You cannot reply to this topic
11 replies to this topic

#1 HPWebcamAble

  • Members
  • 933 posts
  • LocationWeb Development

Posted 27 June 2015 - 01:10 AM

Just a few sorting algoritims to make your life easier!

Things you should know first:
- Basic Lua
- Tables ( <-- Major part of this )




There are 2 types of sorting algorithms that I've provided (But many more exist)
Each has advantages and disadvantages in certain situations


I've added a special 'compare' function that lets you decided
how data is compared, it is required for all of the provided sorting
algorithms to work!

compare Function
Spoiler

How to make tables that can be sorted
Spoiler

The Sorting Algorithms

Merge Sort
Most efficient when...
You don't know if the table is sorted or not
OR
The table isn't sorted yet
Spoiler


Insertion Sort
Most efficient when...
Data is sorted, but a new entry was added and needs sorting
Spoiler

Sorry for the long post :P
If this was helpful, how about a +1 :)

If any of my info is incorrect, or you need help, leave a comment!

Spoiler

Edited by HPWebcamAble, 27 June 2015 - 01:17 AM.


#2 クデル

  • Members
  • 349 posts

Posted 02 July 2015 - 09:47 AM

oooh fancyy :P

#3 HPWebcamAble

  • Members
  • 933 posts
  • LocationWeb Development

Posted 02 July 2015 - 06:31 PM

View PostSandstorm, on 02 July 2015 - 09:47 AM, said:

oooh fancyy :P

Maybe a bit TOO fancy for a Minecraft mod, but oh well, I had fun researching and writing this.

#4 クデル

  • Members
  • 349 posts

Posted 03 July 2015 - 05:18 AM

View PostHPWebcamAble, on 02 July 2015 - 06:31 PM, said:

View PostSandstorm, on 02 July 2015 - 09:47 AM, said:

oooh fancyy :P/>

Maybe a bit TOO fancy for a Minecraft mod, but oh well, I had fun researching and writing this.

Doesn't matter, its awesome either way :P

#5 Konlab

  • Members
  • 595 posts
  • LocationKerbin

Posted 06 September 2015 - 06:46 PM

Bubblesort?

#6 HPWebcamAble

  • Members
  • 933 posts
  • LocationWeb Development

Posted 06 September 2015 - 06:51 PM

View PostKonlab, on 06 September 2015 - 06:46 PM, said:

Bubblesort?

Not that efficient, merge sort is a lot better with log(n) efficiency

I was actually going to include one like that, but I couldn't get it working, and it wasn't very efficient anyway

#7 Konlab

  • Members
  • 595 posts
  • LocationKerbin

Posted 06 September 2015 - 07:04 PM

Oh idk the efficiencies, but i did know that bubblesort is a sorting algorithm. :D

#8 HPWebcamAble

  • Members
  • 933 posts
  • LocationWeb Development

Posted 06 September 2015 - 07:07 PM

View PostKonlab, on 06 September 2015 - 07:04 PM, said:

Oh idk the efficiencies

Ah, these are designed for a sorting a lot of data, so efficiency is important :)

#9 Konlab

  • Members
  • 595 posts
  • LocationKerbin

Posted 06 September 2015 - 07:22 PM

License?
anyways +1
edit:
ik isnt the best place to put this but:
how to calculate efficiency?

Edited by Konlab, 06 September 2015 - 07:24 PM.


#10 HPWebcamAble

  • Members
  • 933 posts
  • LocationWeb Development

Posted 06 September 2015 - 07:43 PM

View PostKonlab, on 06 September 2015 - 07:22 PM, said:

License?

None. Not my all my code, I just got it working in CC. I think it was from Wikipedia or something.


View PostKonlab, on 06 September 2015 - 07:22 PM, said:

ik isnt the best place to put this but:
how to calculate efficiency?

Its the number of passes it takes to sort the data.
Each scenario (Best case, worst case, and average) defines how the data was sorted before the algorithm was ran
(Best is already sorted, worst is sorted in REVERSE, and average is random)


View PostKonlab, on 06 September 2015 - 07:22 PM, said:

anyways +1

Thanks :)

#11 supernicejohn

  • Members
  • 98 posts
  • LocationSweden

Posted 29 August 2017 - 07:59 AM

What about counting sort? I'd like to see that implemented here too, just cause I think it's cool.

Also it's one of the fastest

#12 Xella

  • Members
  • 137 posts
  • LocationOn Earth

Posted 30 August 2017 - 05:26 PM

View Postsupernicejohn, on 29 August 2017 - 07:59 AM, said:

What about counting sort? I'd like to see that implemented here too, just cause I think it's cool.

Also it's one of the fastest

It's not really worth it since there's table.sort(), but here's quicksort (fastest in most cases):

function quickSort(arrayToSort)
  local n = table.getn(arrayToSort)
  if (n <= 1) then
    return arrayToSort
  else
    local pivot = math.random(1, n)
    local pivotValue = arrayToSort[pivot].value
    local smallerArray = {}
    local equalArray = {}
    local biggerArray = {}
    for k, v in pairs(arrayToSort) do
      if (v.value < pivotValue) then
        table.insert(smallerArray, v)
      elseif (v.value == pivotValue) then
        table.insert(equalArray, v)
      else
        table.insert(biggerArray, v)
      end
    end
    local sortedSmallerArray = quickSort(smallerArray)
    local sortedBiggerArray = quickSort(biggerArray)
    local sortedArray = sortedBiggerArray
    for k, v in pairs(equalArray) do
      table.insert(sortedArray, v)
    end
    for k, v in pairs(sortedSmallerArray) do
      table.insert(sortedArray, v)
    end
    return sortedArray
  end
end

Hope this satisfies you enough :P





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users