I agree with Kevin: a golden rule of programming is NEVER move or copy data (particularly string data) if you can possible avoid it. Try always to keep the data static and create various "views" of the data as a list or array.
I knocked out a quick Pascal program (attached) to show how much simpler it is. The source project is included, but here are the salient bits:
// key declarations:
private
{ Private declarations }
laptime : array[0..maxSlots] of single; // single precision float
drivername : array[0..maxslots] of string;
index : array[0..maxSlots] of integer; // sort the index, not the data
wait : boolean; // for stepped version
procedure Init;
procedure ResetIndex;
procedure DisplayList;
procedure DoSort;
procedure DoSortTimed;
public
{ Public declarations }
end;
I wouldn't normally do it like that, but I tried to keep it as close to the original as possible. The times and name are manually inserted into the arrays.
The index needs to be initialised to the original order:
procedure TForm_bmain.ResetIndex;
var i : cardinal;
begin
for i := Low(index) to High(index) do
index[i] := i;
end;
The actual sort routine becomes cleaner and simpler, because we are merely swapping the order of the Index array elements:
procedure TForm_bmain.DoSort;
var i, temp : integer;
Done : boolean;
begin
repeat
Done := true;
for i := Low(index) to High(index)-1 do
if laptime[index[i]] >laptime[index[i+1]] then
begin
Done := false;
temp := index[i];
index[i] :=index[i+1];
index[i+1] := temp;
end;
until Done;
end;
In case you are wondering what "low" and "high" mean, arrays in Pascal can be a subrange (for example array[-256..256] of integer). Using the low and high means you don't need to change loads of stuff if you re-define it. It also works with dynamic arrays and lists.
By separating the data from the index, we could have an array of complex structures with loads of data in. Perhaps the dates, titles and full texts of Shakespeare's plays. But using a bubble sort in this way would be just as fast. Moving megabytes of data wouldn't.
As a rule of thumb, once you get over about hundred elements the sort should be changed to a QuickSort or a ShellSort, which are faster, but slightly more complicated.
On slow phones or tablets it's highly advisable to use an indexed sort like this. Copying strings can be one of the most costly processor usages there is, involving memory allocations deallocations. If you can keep your sort elements to an integer value, that will be faster than a floating point compare.
You can also have multiple "views" over the data set at no extra cost.
Final point here: the "Step" version of the sort is just the same, but with a "wait" flag, which is reset by a one second timer. With a lot of data it's necessary to let the interface update - you can hang the system for a while if you don't take care to ensure that the display and user input get checked in a reasonable time-frame, but overdoing it can actually slow things down.
-- Jim - When is there going to be a release?