Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

AppGameKit Classic Chat / Better Sorting Method Than Cocktail Sort

Author
Message
anwserman
12
Years of Service
User Offline
Joined: 20th May 2011
Location: Wisconsin
Posted: 10th Jan 2012 19:16
I'm currently stuck -

I implemented a Cocktail Sort into my application, to help speed up my program logic - the speed improvements are fantastic, but I know that I could get faster results than using a modified bubblesort algorithm.

Problem is, all of the more involved sorting algorithms I've looked up online are based on one field alone, when I need to sort on multiple fields.

My data type is:



I was able to get Cocktail sort working using my knowledge of overriding sort functions in Visual Basic, but I'm not sure how to do this with other ones - like Insert or Quicksort. Anyone have any ideas?

Hi there. My name is Dug. I have just met you, and I love you.
Hodgey
14
Years of Service
User Offline
Joined: 10th Oct 2009
Location: Australia
Posted: 14th Jan 2012 12:47
I love these sorts (I also love puns) of challenges.

I had a go at sorting your UDT using the "Insertion" sort. It displays an elapsed time so you can compare it with your current sorting algorithms.



anwserman
12
Years of Service
User Offline
Joined: 20th May 2011
Location: Wisconsin
Posted: 14th Jan 2012 16:53
Hey Hodgey -
Thank you for the contribution! However, I think there is a bug with your code. It isn't as apparent with a random dataset, but I came up with a sample base set.

The UDT's values are supposed to be kept grouped together. Instead, this insertion sort sorts out the fields independently of each other.

For instance, the UDT with values of:


should come out in the list before


Because the active field is of a higher value than the second one. Does that make sense? I appreciate your effort, and I'll try to look at your code to see if I can further make changes.

-Jeff



Hi there. My name is Dug. I have just met you, and I love you.
anwserman
12
Years of Service
User Offline
Joined: 20th May 2011
Location: Wisconsin
Posted: 14th Jan 2012 17:07 Edited at: 14th Jan 2012 17:13
So, I threw in my current sorting method into your framework Hodgey, just to see how fast my code was.... and I found a sorting bug - the X values weren't being sorted correctly (ascending and not descending!)

So I really thank you for attempting this, because I would have been blissfully unaware otherwise.

I have attached my Cocktail sort with your framework base, so you can see how my code sorts out the array. It's quite fast, but that could be due to the small dataset (which bubblesorts excel at completing faster.)

BTW, I'll be putting you in my "thank you's" for my game - I appreciate you pointing me in the right direction

EDIT: The items with an active value of 0 are not sorted because they're ignored by the game during gameplay anyway, and I'd rather not spend time sorting values that won't be used anyway.



Hi there. My name is Dug. I have just met you, and I love you.
Hodgey
14
Years of Service
User Offline
Joined: 10th Oct 2009
Location: Australia
Posted: 15th Jan 2012 00:00 Edited at: 15th Jan 2012 00:08
Quote: "Thank you for the contribution! However, I think there is a bug with your code. "

Quote: "The UDT's values are supposed to be kept grouped together. Instead, this insertion sort sorts out the fields independently of each other."

Actually, that's not a bug. It's me misinterpreting your intentions.

Ok answerman, this had better work because it took me ages to write and debug this (a single typo was causing the whole thing to crash!!!).



It's poorly commented because I was pretty frustrated when I decided to retype the whole thing after copy/pasting the bugged code into DBP and solving it from there.

It's hard to see that it has actually sorted the array from the results printed but run it through your tests and let me know the results.

Quote: "The items with an active value of 0 are not sorted because they're ignored by the game during gameplay anyway, and I'd rather not spend time sorting values that won't be used anyway."

I'm sure you can fix that up.

Edit: Forgot to explain what it actually does. What it does is creates a new UDT array, one field for storing the value and the other for storing the element which it came from. For example you have this:

Array[1].X = 10
Array[1].Y = 40
Array[1].Acive = 0

In the new array the above would look like this:
The X
SortArray[1].Value = 10
SortArray[1].A_Type = 1

The Y
SortArray[2].Value = 40
SortArray[2].A_Type = 2

The Active
SortArray[3].Value = 0
SortArray[3].A_Type = 3

Using the A_Type legend:
X = 1
Y = 2
Active = 3.

So all of those values are stored into the new array along with their 'types', are then sorted, and then copied back into the orginal array into their correct fields using the A_Type value.

I hope that makes sense.

anwserman
12
Years of Service
User Offline
Joined: 20th May 2011
Location: Wisconsin
Posted: 17th Jan 2012 11:44
Hey Hodgey,
I haven't looked at this yet but I'll get to it soon. Thank you once again for your assistance! If I have questions, I'll be sure to ask you!

Hi there. My name is Dug. I have just met you, and I love you.
JimHawkins
14
Years of Service
User Offline
Joined: 26th Jul 2009
Location: Hull - UK
Posted: 17th Jan 2012 11:51
It may be worth pointing out that for short lists or arrays, using QuickSort, ShellSort etc can be substantially slower than an augmented BubbleSort, because the algorithms are more complex.

-- Jim
Hodgey
14
Years of Service
User Offline
Joined: 10th Oct 2009
Location: Australia
Posted: 20th Jan 2012 11:15
Quote: "I haven't looked at this yet but I'll get to it soon. Thank you once again for your assistance!"

You're welcome mate, always happy to help.

Quote: "It may be worth pointing out that for short lists or arrays, using QuickSort, ShellSort etc can be substantially slower than an augmented BubbleSort, because the algorithms are more complex."

This is why I like insertion sort, it's pretty much an all-rounder but the more values to sort, the slower it may be compared to other algorithms sorting the same values.

http://www.sorting-algorithms.com/
A nice way to compare algorithms.

anwserman
12
Years of Service
User Offline
Joined: 20th May 2011
Location: Wisconsin
Posted: 23rd Jan 2012 00:40
That's a really cool website hodgey. As my data is mostly sorted, bubble sort actually works quite well on it. Not nearly as fast as insertion sort, but still a cool way to visualize the sorting

Hi there. My name is Dug. I have just met you, and I love you.

Login to post a reply

Server time is: 2024-04-19 04:54:47
Your offset time is: 2024-04-19 04:54:47