Delphi and Kylix

Moderators: pritaeas
Number of posts: 19051

This Forum Only

Sorting an array Posted by sarkiemarkie on 15 Feb 2006 at 2:56 PM
This message was edited by sarkiemarkie at 2006-2-15 15:39:8

This message was edited by sarkiemarkie at 2006-2-15 15:29:42

```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;```

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.

Re: Sorting an array Posted by thinker on 15 Feb 2006 at 3:53 PM

:
```repeat
: sorted:=true;
: for i:=1 to filesize(form3.vidvdfile) do
: ```

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

:
```:   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;```

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

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic