Sorting an array

[b][red]This message was edited by sarkiemarkie at 2006-2-15 15:39:8[/red][/b][hr]
[b][red]This message was edited by sarkiemarkie at 2006-2-15 15:29:42[/red][/b][hr]
[code]repeat
sorted:=true;
for i:=1 to filesize(form3.vidvdfile) do
if videos[i]<=videos[i+i] then
begin
sorted:=false;

tempvideo:=videos[i];
videos[i]:=videos[i+1];
videos[i+1]:=tempvideo;

temppointer:=pointers[i];
pointers[i]:=pointers[i+1];
pointers[i+1]:=temppointer;
end;
until sorted=true;[/code]

the problem is that some of the array values contain a 0 and when the array compares a 0 with a 0 it keeps going as 0 does indeed equal 0. Also, if i have out the = in "<=" if the first two values in the array are 0 and 0 then they are not less than each other so the array ends immediately.






Comments


  • : [code]repeat
    : sorted:=true;
    : for i:=1 to filesize(form3.vidvdfile) do
    : [/code]

    Are you sure filesize(form3.vidvdfile) return always the maximum index of videos - 1?
    if it doesn't, there could be problems in the next line...

    : [code]
    : if videos[i]<=videos[i+i] then
    : begin
    : sorted:=false;
    :
    : tempvideo:=videos[i];
    : videos[i]:=videos[i+1];
    : videos[i+1]:=tempvideo;
    :
    : temppointer:=pointers[i];
    : pointers[i]:=pointers[i+1];
    : pointers[i+1]:=temppointer;
    : end;
    : until sorted=true;[/code]

    This is some kind of bubble-Sort. Sorting algorithms are well researched, I would suggest you search the Internet for it.

    : the problem is that some of the array values contain a 0 and when the array compares a 0 with a 0 it keeps going.

    Yes, your algorithm cannot deal with equal values... They get swapped over and over.
    Easy fix: replace "less or equal" with "less than" in the comparison.

    mfg

    Thinker
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

In this Discussion