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.