Sorting an array - Programmers Heaven

Howdy, Stranger!

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

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

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

  • thinkerthinker Posts: 7Member

    : [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.