[LIB] Array/Matrix Sorts

The place to discuss scripting and game modifications for X³: Terran Conflict and X³: Albion Prelude.

Moderators: Moderators for English X Forum, Scripting / Modding Moderators

Post Reply
User avatar
s9ilent
Posts: 2011
Joined: Wed, 29. Jun 05, 01:45
x4

[LIB] Array/Matrix Sorts

Post by s9ilent » Sun, 25. Jan 09, 05:23

After a fair bit of trial and error

Image array_sorts.zip
Array Sorts

array_sorts.zip


4 Script files
All sorts are numerical!!! (as opposed to X3's string based one...why?!)
  • lib.array.insertsort -Your basic insertion sort, is stable, single array
  • lib.array.quicksort -Basic quick sort, is not stable, single array, is better then insertion sort if you can pick a better pivot (apparently?)
  • lib.array.quicksort.recursive -DONT CALL THIS ONE!!! call the other one
  • lib.array.insertsort.matrix -Insertion sort, is stable, does a matrix, max sort by 2 columns (optional sort up, sort down)
Notes on the matrix sort.
Leave col2 blank to sort by just col1
Garbage in Garbage out. It returns null if your matrix is not correctly sized (all columns are equal in size)
Array[C][R], Column, Row (C-> R, Alphabetical order). Don't get them confused!!!
-Alternatively, think like this: An array is a single column, an array of an array, is a whole lot of columns, ie. a row of columns. Thus, as you go DOWN a row, your going down the Inner array, so array[][down here]. So the second one is the row.
-Or just look at excel, it uses Column Row too (A1, B3, etc)

Cycrow
Moderator (Script&Mod)
Moderator (Script&Mod)
Posts: 20544
Joined: Mon, 15. Nov 04, 00:26
x4

Post by Cycrow » Mon, 1. Jun 09, 02:02

just wandering how come you created a script to sort arrays when there is already a built in command to do it ?

especially as the built in one uses qsort, and can handle any type of arrays including numbers and strings

User avatar
s9ilent
Posts: 2011
Joined: Wed, 29. Jun 05, 01:45
x4

Post by s9ilent » Mon, 1. Jun 09, 03:04

:)

I wanted a matrix sort, but X3 only has an array sort (and yes, you can sort an array by another array, but I wanted to do it to like a 10x wide matrix, so I figured it might not be the most efficient way to do the sort 10x)

Plus, I just wanted to see if I could do it.

Tbh, I was going to use it for my SFM script (comparing wares) but in the end, I decided rather then physically sorting the matrix (which was messy... and CPU intensive), I would just search for the highest value (as I really only needed the highest, and that highest would rarely fail other checks, so there was really no need for this any more)

But hey, I made it, so I figured, I'd put it up here.


But this script isn't really efficient so I don't recommend people use it (unless they are sorting large matrices)



(Oh and also, I don't know how to do a quick sort :( Well... I'm sure I could.. it'd just be hell messy in the SE)
(Oh yeh.. and to actually answer your question, I needed to build an array sort, in order to build the matrix sort)

Another though occurs to me... Cycrow, any chance you can request a matrix quick sort command in the S.E for the next patch or w/e
(Preferably with a setup like this one/..symtec's/yours? from X3R, 4 params, two for sort by which columns, and two for sort increasing/decreasing, and on failure return the original input -where failure occurs when the input is not a matrix other wise it does its best to
sort stuff, or... maybe null, I don't know, what ever)



As a side note thou, I think I did do a test on them and my sort is like infinitely slower (like 20s compared to <2s) (But I don't remember if it was a matrix/array sort)

Post Reply

Return to “X³: Terran Conflict / Albion Prelude - Scripts and Modding”