Author Topic: Ordering variables based on numerical value  (Read 872 times)

What's the best way to arrange values of variables from greatest to lowest? Let's say that it's for the purpose of writing to a file.

The variables would be in the following format:
Code: [Select]
$var[a] = 41;
$var[b] = 32;
$var[c] = 54;

and the written file would look something like this:
Code: [Select]
c - 54
a - 41
b - 32
And etc.

Any help would be appreciated. I do know how to do basic File I/O and string manipulation, so you guys don't have to explain that in depth.

There are several different ways of doing this numerically, the best of them (in my opinion) being a method called Heap Sort
I'm not exactly sure how to code it in torque, but i'm sure there are plenty of resources online in easily-translated languages like C#.

What I really need help with is getting all of my values into a heap to be able to do that

The heapsort would do that for you.
You just put everything in a array, and have it run on that array in the same function.

Some actual code would be helpful

if you can't figure heap sort out, bubble sort would work too

I'm sure someone has written a TorqueScript sort algorithm. If you can't find one you could find one written in another language (Here's an insertion sort that I used in my programming class) and do some work to rewrite it in TorqueScript (I'd try it but I have a lot of school work that I need to do)

if you can't figure heap sort out, bubble sort would work too
As long as he doesn't need to sort a very large amount with it

The amount should be about 200 - 300 each time


Also, I found a copy of a quicksort algorithm here:
http://www.codecodex.com/wiki/Quicksort#TorqueScript

However, I don't really understand how to use it. If someone could maybe use with an example set, that could help me.

A really odd way of doing it, but looks like you would do something like this

Code: [Select]
//Create a SimSet
%unsorted = new SimSet();

//Add the values from the array to the SimSet
for(%i=0;%i<%arrayLength;%i++)
{
    %unsorted.add(%t = new ScriptObject());
    %t.myValue = %array[%i]
}

//Sort it
%sorted = quicksort(%unsorted);

//Echo the sorted values
for(%i=0;%i<%sorted.getCount();%i++)
{
    echo(%sorted.getObject(%i).myValue);
}
« Last Edit: February 27, 2012, 07:26:41 PM by Headcrab Zombie »

if it's that big, try shell sort?

Also keep in mind Torque does not automatically delete simsets and scriptobjects, you'll have to delete them yourself when you're done with them, or you can get a pretty bad memory leak if you're frequently sorting large amounts


if it's that big, try shell sort?
Speed isn't an issue, sorting 300 numbers between 0 and 10000 only took me 31ms

Are you serious? You're going to create a massive influx of script objects because you're too lazy/unknowledgeable to do a bubble sort? Do you know how stupid that is?

31ms for 300 numbers is not a bragging right. It's a liability.

I didn't even write the sort, he found it and I showed him how to use it. I even addressed that issue.
31ms was for numbers between 0 and 10000. I don't know how big the range of numbers he's sorting, but it will take less time for a smaller range. Whether it's a liability depends on how often and quickly it needs to be done.

bubble sort
liability
>Implying bubblesort is faster

Also, you're in a help thread, how about contributing instead of simply calling people stupid?
« Last Edit: February 29, 2012, 02:38:55 AM by Headcrab Zombie »

>Implying bubblesort is faster
>Implying creating 10,001 console objects is as fast as bubble-sort...

Also, you're in a help thread, how about contributing instead of simply calling people stupid?
I did help. I gave him an actual algorithm name that will actually sort actual lists numerically without resorting to stupid hacks that have the same end result.

Code: [Select]
for (%i = (%length / 2) - 1; %i >= 0; %i--)
{
%root = %i;
%bottom = %x;

%done = false;

while ((%root * 2 <= %bottom) && (!%done))
{
if (%root * 2 == %bottom)
%maxChild = %root * 2;
else if (%a[%root * 2] > %a[%root * 2 + 1])
%maxChild = %root * 2;
else
%maxChild = %root * 2 + 1;

if (%a[%root] < %a[%maxChild])
{
%temp = %a[%root];
%a[%root] = %a[%maxChild];
%a[%maxChild] = %temp;
%root = %maxChild;
}
else
{
%done = true;
}
}
}

for (%i = %length - 1; %i >= 1; %i--)
{
%temp = %a[0];
%a[0] = %a[%i];
%a[%i] = %temp;

%root = 0;
%bottom = %i - 1;

%done = false;

while ((%root * 2 <= %bottom) && (!%done))
{
if (%root * 2 == %bottom)
%maxChild = %root * 2;
else if (%a[%root * 2] > %a[%root * 2 + 1])
%maxChild = %root * 2;
else
%maxChild = %root * 2 + 1;

if (%a[%root] < %a[%maxChild])
{
%temp = %a[%root];
%a[%root] = %a[%maxChild];
%a[%maxChild] = %temp;
%root = %maxChild;
}
else
{
%done = true;
}
}
}
Where %a is the array of numbers to be sorted, and %length is the length of the array.
Just insert it into whatever function that requires a list of numbers to be sorted.
I just quickly translated this from C#, so check for errors before you use it.