Simple array sort

Forums for specific tips, techniques and example code
ConvertFromOldNGs
Posts: 5321
Joined: Wed Aug 05, 2009 5:19 pm

Simple array sort

Postby ConvertFromOldNGs » Fri Aug 07, 2009 2:11 pm

by JADE News Administrator >> Thu, 29 Oct 1998 5:12:29 GMT

Hi
I have a simple IntegerArray with 5 elements each containing a number.

How do I easily sort the array such that the elements contain the highest numbers i.e. a descending sort??

Currently should become

x[1] = 5 x[1] = 764
x[2] = 65 x[2] = 65
x[3] = 3 x[3] = 52
x[4] = 764 x[4] = 5
x[5] = 52 x[5] = 3

--
Ciao .................
Alan J.Thomson
ph...Auckland 09-277-7961 x 744
http://home.clear.net.nz/pages/athomson

ConvertFromOldNGs
Posts: 5321
Joined: Wed Aug 05, 2009 5:19 pm

Re: Simple array sort

Postby ConvertFromOldNGs » Fri Aug 07, 2009 2:11 pm

by JADE News Administrator >> Thu, 29 Oct 1998 5:13:00 GMT

I have a Jade sript which i wrote a while back to create a small sort routine for decimals to sort from lowest to highest.

I have given it a quick test and it should work, although i can't remember how much testing i have done with it before.

Play around with it and it should give you a base to start with.

morrisSort();

vars
array : DecimalArray;
i,x,y : Integer;
begin

create array transient;
array[1] := 17.2;
array[2] := 29.1;
array[3] := 28.9;
array[4] := 29.0;
array[5] := 29.1;
i := 1;
x := 1;

while i <= 4 do
if array[i + x] < array then
array[6] := array;
array.removeAt(i);
x := 1;
else
x := x + 1;
y := x + i;
if y > 5 then
i := i + 1;
x := 1;
endif;
endif;
endwhile;

write array[1];
write array[2];
write array[3];
write array[4];
write array[5];

delete array;

end;

ConvertFromOldNGs
Posts: 5321
Joined: Wed Aug 05, 2009 5:19 pm

Re: Simple array sort

Postby ConvertFromOldNGs » Fri Aug 07, 2009 2:11 pm

by JADE News Administrator >> Thu, 29 Oct 1998 5:13:37 GMT

Hi ho there Silver,

Here's a very crude bubble sort algorithm in Jadescript that does the job - but only practical if your array is small, and you want to sort "in situ". If you have more than a handful of values to sort, don't use this method for goodness' sake - it's very inefficient.
With this one, 100 entries will sort in 7 secs, but 200 takes half a minute. And please don't tell the gang in the Engineering Centre you got it from me, either!

bubbleSortArray();
// Uses the most basic bubble or shuffle-sort algorithm to sort an Integer Array, in situ...
// highest value to go to index[1], lowest to bottom
// DISCLAIMER : very inefficient once you've got more than a handful of elements
// Author : Too embarrassed to own up to it
vars
a : IntegerArray;
i1, i2, i3, iTemp : Integer;beginbegin
Transaction;
a := IntegerArray.firstInstance;
i1 := 1;
i2 := 1;
i3 := a.indexOf(a.last);

while i1 < i3 do // i1 is the slot in the array we're going to fill
i2 := i1 + 1;
while i2 <= i3 do // roll through the rest, looking for anything bigger if a[i2] > a[i1] then // hey, found a bigger one. Make it the new benchmark
iTemp := a[i2];
a[i2] := a[i1];
a[i1] := iTemp;
endif;
i2 := i2 + 1;
endwhile;
i1 := i1 + 1;
endwhile;
commitTransaction;
end;

Regards, Stuart.


Return to “Tips and Techniques”

Who is online

Users browsing this forum: No registered users and 2 guests