Bay 12 Games Forum

Please login or register.

Login with username, password and session length
Advanced search  

Author Topic: Sorting arrays of numbers  (Read 893 times)

Skyrunner

  • Bay Watcher
  • ?!?!
    • View Profile
    • Portfolio
Sorting arrays of numbers
« on: December 23, 2011, 11:39:28 pm »

I am learning C++, and for fun made a bubble wrap sorting and a gnome sorting.


10000 numbers : Bubble 3.79 units of time, 24985850 swaps made.
                     : Gnome 0 units of time, 81082 swaps made.

50000 numbers : Bubble 94.22 units of time, 626088997 swaps made.
                       Gnome 0.16 units of time, 524102 swaps made.

100000 numbers : Bubble 380.88 units of time, -1802926064 swaps made?
                         Gnome 0.17 units of time, 1085957 swaps made.

...Its really astonishing, how much bubble is outclassed...

Search on Wikipedia for bubble and  gnome.
Logged

bay12 lower boards IRC:irc.darkmyst.org @ #bay12lb
"Oh, they never lie. They dissemble, evade, prevaricate, confoud, confuse, distract, obscure, subtly misrepresent and willfully misunderstand with what often appears to be a positively gleeful relish ... but they never lie" -- Look To Windward

RulerOfNothing

  • Bay Watcher
    • View Profile
Re: Sorting arrays of numbers
« Reply #1 on: December 24, 2011, 12:14:10 am »

This is to be expected since bubble sort takes quadratic time to sort a list and in fact is particularly bad even for quadratic-time sorts.
Logged

Levi

  • Bay Watcher
  • Is a fish.
    • View Profile
Re: Sorting arrays of numbers
« Reply #2 on: December 24, 2011, 11:51:27 am »

Never heard of GnomeSort before.  Kind of neat.

My favorite when I was in school was MergeSort. 
Logged
Avid Gamer | Goldfish Enthusiast | Canadian | Professional Layabout

Skyrunner

  • Bay Watcher
  • ?!?!
    • View Profile
    • Portfolio
Re: Sorting arrays of numbers
« Reply #3 on: December 29, 2011, 02:37:03 am »

Never heard of GnomeSort before.  Kind of neat.

My favorite when I was in school was MergeSort.

Gnome sort has a really tiny code and is easier to code than insertion but better than bubble (which is not saying much :P). It also has the added benefit of freezing if you try to sort more than 1000000 numbers on a 32 bit computer.
Logged

bay12 lower boards IRC:irc.darkmyst.org @ #bay12lb
"Oh, they never lie. They dissemble, evade, prevaricate, confoud, confuse, distract, obscure, subtly misrepresent and willfully misunderstand with what often appears to be a positively gleeful relish ... but they never lie" -- Look To Windward

malloc

  • Bay Watcher
    • View Profile
Re: Sorting arrays of numbers
« Reply #4 on: December 30, 2011, 10:42:04 am »

Never heard of GnomeSort before.  Kind of neat.

My favorite when I was in school was MergeSort.

Gnome sort has a really tiny code and is easier to code than insertion but better than bubble (which is not saying much :P). It also has the added benefit of freezing if you try to sort more than 1000000 numbers on a 32 bit computer.

Which does not have anything to do with the amount of bits on your computer, but the fact that gnomesort has a average time complexity of O(n2). This means, that it's on average 100 times harder for gnome sort to sort 1,000,000 numbers, than it is to sort 100,000 numbers.

You should consider looking into Quicksort, Mergesort and Heapsort, they all have O(n * log(n)) time complexities, but have different advantages, like Mergesort being extremely easy to thread (Compared to the other sorting algorithms), Heapsort being memory efficient, and Quicksort just being a little hard to implement efficiently.
Sorting data is very serious business in computer science.
Logged

Darvi

  • Bay Watcher
  • <Cript> Darvi is my wifi.
    • View Profile
Re: Sorting arrays of numbers
« Reply #5 on: December 30, 2011, 09:25:20 pm »

Your links both lead to the same page.

Thought I'd mention it.
Logged

Stargrasper

  • Bay Watcher
    • View Profile
Re: Sorting arrays of numbers
« Reply #6 on: December 31, 2011, 10:52:54 pm »

I believe the fastest sorting algorithm is supposed to be heapsort.
Logged

Thief^

  • Bay Watcher
  • Official crazy person
    • View Profile
Re: Sorting arrays of numbers
« Reply #7 on: January 03, 2012, 04:58:22 am »

I did this at some point too, my favourite was shell sort.
Logged
Dwarven blood types are not A, B, AB, O but Ale, Wine, Beer, Rum, Whisky and so forth.
It's not an embark so much as seven dwarves having a simultaneous strange mood and going off to build an artifact fortress that menaces with spikes of awesome and hanging rings of death.

RulerOfNothing

  • Bay Watcher
    • View Profile
Re: Sorting arrays of numbers
« Reply #8 on: January 03, 2012, 05:44:28 am »

I did this at some point too, my favourite was shell sort.
Heh, shell sort is my favourite sort as well.
Logged

Stargrasper

  • Bay Watcher
    • View Profile
Re: Sorting arrays of numbers
« Reply #9 on: January 03, 2012, 11:57:39 am »

I suppose it's worth noting that a large number of the sorting algorithms do have a runtime of O(n log n).  Which algorithm you use depends a lot of the data you're expecting to see.

Also, I've never actually seen shell sort.  I'll have to look into it.  And I still need to figure out heapsort or I'm never going to survive some of my later classes.
Logged

Nadaka

  • Bay Watcher
    • View Profile
    • http://www.nadaka.us
Re: Sorting arrays of numbers
« Reply #10 on: January 03, 2012, 04:37:54 pm »

You may also have occasion to use a bucket or counting sort. With the right data, you are looking at nearly O(n).
Logged
Take me out to the black, tell them I ain't comin' back...
I don't care cause I'm still free, you can't take the sky from me...

I turned myself into a monster, to fight against the monsters of the world.