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 / Help needed with adapted Bubble Sort for Driver Laptimes

Author
Message
DemonHill
16
Years of Service
User Offline
Joined: 20th Mar 2008
Location:
Posted: 14th Sep 2013 00:19
Trying to get my head round a simple "bubble Sort" of laptimes and ranking with associated driver names.

Any help appreciated with code



JimHawkins
14
Years of Service
User Offline
Joined: 26th Jul 2009
Location: Hull - UK
Posted: 14th Sep 2013 01:52
I don't think you're swapping the driver names!

-- Jim - When is there going to be a release?
Ancient Lady
Valued Member
20
Years of Service
User Offline
Joined: 17th Mar 2004
Location: Anchorage, Alaska, USA
Posted: 14th Sep 2013 04:01 Edited at: 14th Sep 2013 17:33
A bubble sort has two loops. The outer with var_i from 1 to x-1 (one based, x being the size of the loop) and the inner from var_i to 1-1. That is what bubbles it up. With the check for no changes on any given loop (inner or outer).

I am not where I can do the full algorithm. But it is something that I have dealt with since 1978. (And couldn't believe that the ditz who had no business in the computer science department where I was acting as a consultant couldn't understand after three times of going through a working example of 15 assembly language level commands just couldn't understand.)

But Jimkawkins is right. This command "(DriverName$[d]) = (DriverName$[ i ])" is doing absolutely nothing. The variable 'd' is not set anywhere.

EDIT: I am not trying to imply that you are a ditz in any way. Just giving an example of my experience with the Bubble Sort algorithm. She took insult to my saying "You can't possibly be that stupid" and looked at the schedule on the door and complained to my supervisor about somebody else. Proving my point about her. My supervisor came down and said "You aren't supposed to say that. No matter how dumb they are." This story is one my children heard/learned early and took to heart when they were in the service industry.

Cheers,
Ancient Lady
AGK Community Tester and AppGameKit Master
Ancient Lady
Valued Member
20
Years of Service
User Offline
Joined: 17th Mar 2004
Location: Anchorage, Alaska, USA
Posted: 14th Sep 2013 05:21 Edited at: 14th Sep 2013 05:30
Edit, sort of. I am supposed to be kept from posting anything anywhere after a few beers, bottles of champagne, or multiple shots of tequila. Please understand that I really have not meant any offense.

This is a basic reference for doing the bubble sort algorithm (backing up my assertion that it is a double loop).

EDIT: Tonight was a night of several beers. We just brought home our new RV last night and tonight we christened it (imbibing the drinkables, not smashing them on our poor RV, Freddy).

EDIT 2: And I actually looked at the reference I gave in more detail and it doesn't do the bubble sort the way I was taught 30+ years ago (double loop with break outs if no swap made). If I find the right reference for what I was taught (and chastised the ditz about), I will post it.

EDIT 3: Okay, it does show my version, sort of. It uses a repeat until loop, surrounding a straight for loop with the updated end value, which does what I suggested. (I am vindicated? and I will stay away from the computer when mildly to seriously intoxicated in the future. If I remember, in time, before hitting whatever button commits my post.)

Cheers,
Ancient Lady
AGK Community Tester and AppGameKit Master
Markus
Valued Member
20
Years of Service
User Offline
Joined: 10th Apr 2004
Location: Germany
Posted: 14th Sep 2013 09:50 Edited at: 14th Sep 2013 09:53
i useing this for my hiscore table.
(Player should be a parameter.)

Kevin Picone
21
Years of Service
User Offline
Joined: 27th Aug 2002
Location: Australia
Posted: 14th Sep 2013 10:11 Edited at: 31st May 2014 10:33
DemonHill,

Here's a bit of a rehash of the sort and swap functions. As has been mentioned, the 'swap' function just needs to flip the names around as well as the lap times. Providing the list of drivers is kept small, then it should work OK for your needs.

There's two obvious bottle necks in it, the sort logic isn't particularly clever and swapping strings creates a lot of extra memory thrashing. So if you need to sort a longer list, then you should into changing/tweaking algorithm, and/or look into using a reference(order) array, to remove the need to physically swap the driver info. You can possibly get around that with a typed array, depending upon AGK's implementation.




JimHawkins
14
Years of Service
User Offline
Joined: 26th Jul 2009
Location: Hull - UK
Posted: 14th Sep 2013 14:15 Edited at: 14th Sep 2013 14:27
I agree with Kevin: a golden rule of programming is NEVER move or copy data (particularly string data) if you can possible avoid it. Try always to keep the data static and create various "views" of the data as a list or array.

I knocked out a quick Pascal program (attached) to show how much simpler it is. The source project is included, but here are the salient bits:



I wouldn't normally do it like that, but I tried to keep it as close to the original as possible. The times and name are manually inserted into the arrays.

The index needs to be initialised to the original order:



The actual sort routine becomes cleaner and simpler, because we are merely swapping the order of the Index array elements:



In case you are wondering what "low" and "high" mean, arrays in Pascal can be a subrange (for example array[-256..256] of integer). Using the low and high means you don't need to change loads of stuff if you re-define it. It also works with dynamic arrays and lists.

By separating the data from the index, we could have an array of complex structures with loads of data in. Perhaps the dates, titles and full texts of Shakespeare's plays. But using a bubble sort in this way would be just as fast. Moving megabytes of data wouldn't.

As a rule of thumb, once you get over about hundred elements the sort should be changed to a QuickSort or a ShellSort, which are faster, but slightly more complicated.

On slow phones or tablets it's highly advisable to use an indexed sort like this. Copying strings can be one of the most costly processor usages there is, involving memory allocations deallocations. If you can keep your sort elements to an integer value, that will be faster than a floating point compare.

You can also have multiple "views" over the data set at no extra cost.

Final point here: the "Step" version of the sort is just the same, but with a "wait" flag, which is reset by a one second timer. With a lot of data it's necessary to let the interface update - you can hang the system for a while if you don't take care to ensure that the display and user input get checked in a reasonable time-frame, but overdoing it can actually slow things down.

-- Jim - When is there going to be a release?

Attachments

Login to view attachments
Hodgey
14
Years of Service
User Offline
Joined: 10th Oct 2009
Location: Australia
Posted: 15th Sep 2013 01:24
Quote: "swapping strings creates a lot of extra memory thrashing. "

Are the strings actually being copied char by char behind the scenes or is it just a pointer swap?

JimHawkins
14
Years of Service
User Offline
Joined: 26th Jul 2009
Location: Hull - UK
Posted: 15th Sep 2013 01:53 Edited at: 15th Sep 2013 02:04
Quote: "Are the strings actually being copied char by char behind the scenes or is it just a pointer swap?"


That's pretty much implementation dependent. If you're doing a string swap some memory has to be allocated during the process, because at least one string has to be replicated in a temporary variable:

temp$ = stringarray[x] // new allocation definitely needed
stringarray[x] = stringarray[x+1] // probably here
stringarray[x+1] = temp$ // probably here

If the string technology is using reference-counted strings and no change is made it might merely use the pointers and adjust the reference count. I suspect that AppGameKit Basic is not an optimising compiler capable of working that out from the context.

Normally I would use pointers into the list or array - but you can't do that in T1, so I used an indexing array model. The sort above can be translated into T1 very easily.

With the example data, nine swaps are needed to sort the eleven elements. If we moved the data rather than the index, at least 9, and possibly 27 string operations would be needed. That's going to be invisible on a fast PC, but has quite a lot of impact on a small phone.

-- Jim - When is there going to be a release?
Ancient Lady
Valued Member
20
Years of Service
User Offline
Joined: 17th Mar 2004
Location: Anchorage, Alaska, USA
Posted: 15th Sep 2013 02:06
Quote: "Are the strings actually being copied char by char behind the scenes or is it just a pointer swap?"

Definitely not just a pointer swap for the AppGameKit interpreter.

I looked at the interpreter.cpp code to get the following information.

In the AppGameKit interpreter, for a string variable to string variable assignment (not in arrays), if the string assigned to is not empty, its string is deleted. Then, in either case, a new string is created as array of char of the source length plus one and a strcpy command is used to copy the source. The new pointer is then assigned to the target string.

If either source or target string is in an array, other things are done. I didn't want to look too deeply at this. But, I'm betting there are a lot of deletes and creations and copies going on again.

Cheers,
Ancient Lady
AGK Community Tester and AppGameKit Master
JimHawkins
14
Years of Service
User Offline
Joined: 26th Jul 2009
Location: Hull - UK
Posted: 15th Sep 2013 02:26
As always, thanks AL. All of this CPU-costly stuff is eliminated by sorting on an index array (or a pointer list) which takes an overview of the data.

If you have 1000 "spaceships" (in a UDT or Record or struct) with names, images, crew list, date of manufacture) etc, don't move all the data - if you want to sort by ship name, proximity to star Zelda, or whatever, make an index array and sort that rather than shovelling all the data around.

-- Jim - When is there going to be a release?
Hodgey
14
Years of Service
User Offline
Joined: 10th Oct 2009
Location: Australia
Posted: 15th Sep 2013 02:39
I had a feeling that was going to be the case. Thanks Jim and AL.

Quote: "But, I'm betting there are a lot of deletes and creations and copies going on again."

Sounds like it given your description.

Login to post a reply

Server time is: 2024-05-09 19:06:42
Your offset time is: 2024-05-09 19:06:42