Page 1 of 1

[Library scripts] Sorts and strings - updated 2007-03-05

Posted: Mon, 8. Jan 07, 01:02
by B-O'F

While writing the scripts for 'Remote Docked Freight/Upgrade Transfer', I needed a good sort script, but did not have access to this forum to find one... so I had to write my own.

I have books for many computer languages, and they had programs listed, so I converted them to X3 scripting language.

The files in the package are the result of this work, and are being made freely available for anyone to use as they wish.

The .zip file consists of the .xml files themselves, and a document containing listings of the files - together with the original c code, or pascal code. This file is not formatted for printing, just for looking at onscreen - there are 28 pages, over 1500 lines.

The .xml files were converted to text files using the very useful "XML to plain text" program by Bunny.

Updated 2007-03-05
Added formattime and csv from a t-file


- Bubble - alpha and numeric
- Insertion - alpha and numeric
- Selection - alpha and numeric
- Shell - alpha and numeric

And the best sort in this collection:-
- Improved Shell - alpha and numeric plus 2-linked array alpha and numeric, and a 2d-array alpha where you select the element to sort on.

After sorts

- Merge sorted alpha
- Remove duplicates from a sorted array


- Convert case to upper or lower
- Format a number with commas - or you choose
- Isalpha - check if a string consists solely of alpha characters
- Isnumeric - check if a string consists solely of numeric characters
- Isnumericwithsign - check if a string consists solely of numeric characters after a leading + or - character
- Splitstring - splits a string at the space character and returns an array of the parts
- FormatTime - formats game time to dd-hh:mm:ss
- Splitcsvstring - splits a string at the comma character and returns an array of the parts


Getarray.myshipsdockedat - returns an array of player-owned ships docked at a location.
Readincsv - reads in csv data from a t-file

Version 2
.zip file - courtesy of Xai Corp.

[Edit] If you do want to use these scripts in your own packages - copy and rename to whatever you want, as some of them are used in the Remote Docked Freight/Upgrade Transfer - and changes to the library scripts might break it. [/Edit]

Use as you wish,

Boron - Ol Fh'art

Posted: Mon, 8. Jan 07, 01:34
by Klyith
Sorts are something that I've been considering myself. Though what I am thinking of doing is more X3 contextual (stuff like sort by distance & sort by class). So I just now sent moggy a pm that mentions this, among other things:
Anyways, I've been doing some experimenting and I think that recursive functions are actually bad in X3... Calling a new script is a much longer delay than a tight loop inside a single script. I'm doing the experiments because I may start a project to do some array sorting. If recursive is generally worse then the usual "best" algos like quicksort are not the best to use. I think combsort might be the best X3 sort script.
Sent pm, refresh S&M page, and you've posted this topic! Wacky.

Anyways, I think that the best sort function for X3 is going to be Combsort. It's faster than Shellsort in big-O terms... Shellsort is the fastest of the sorts you've implemented. For small arrays shellsort may be faster in X3 than combsort, because of the tighter loops of the insert-based shellsort.

You've done a hell of a lot of work there. Though I'm not sure why you bothered with bubble sort...;) But shellsort is good, and the strings scripts are golden. I may borrow the convert case scripts to use in my RSwE package.

Posted: Mon, 8. Jan 07, 02:17
by B-O'F
Hi Klyith,

I think that the sort I have called Improved Shell sort is possibly a Combsort, as it uses smaller and smaller 'gaps'. I have a copy of the Byte article mentioned, and it uses a divisor of 1.3 between combs - iirc. The version in my pack uses a divisor of 2.

Anyway, it seems to work pretty fast.

Why bother with bubble sort - cos that was the first one I tried, then went on to try all the others in the books - apart from Quicksort, which I didn't try because of it's recursiveness - just to see if I could do it, and it was fun.

By all means use - or modify - any that you want to. They are there to be used - no credit needed - just save yourself some work.


Boron - Ol Fh'art

Posted: Mon, 8. Jan 07, 14:02
by Cycrow
u should be able to create quicksort in X Scripting, but i doubt it'll be very efficent, so i really wouldn't bother.

bubble sort can still be useful tohu,its a fairly simple algorithm, and works ok for small sets of data

Posted: Mon, 5. Mar 07, 18:55
by B-O'F

This package is now updated, I have added:-

FormatTime - formats time in dd-hh:mm:ss (actually an old one that I did not include originally)
splitcsvstring - splits a string at a comma, allowing csv format input.

readincsvdata - allows csv data to be read in from a t-file. This has it's own .rtf document with details of t-file and how to use it.. you can now read in hundreds of lines of comma separated data - if you want to type it in.. or do it the easy way, and export to a log file.

As ever - modify, use or ignore as you wish,
but enjoy..

Boron - Ol Fh'art