Author Topic: sorting array  (Read 926 times)

0 Members and 1 Guest are viewing this topic.

Offline iftach

  • Major(1)
  • Posts: 16
sorting array
« on: April 22, 2007, 10:34:22 am »
can somebody give me a working sorting algorithm. i cant seem to make any of the ones i found work.

i have this, but it doesn't work. it ain't getting past the while loop for some reason.
 
Code: [Select]
Procedure InsertionSort(size : Integer);

Var
i, j : Integer;
temp : Double;

Begin
 For i := 2 to size-1 do
  Begin
   temp := KDarray[i];
   j := i;
   While (j > 1) AND (KDarray[j-1] > temp) do
    Begin
     KDarray[j] := KDarray[j-1];
     j := j - 1;
    end;
   KDarray[j] := temp;
  end;
End;

Offline Avarax

  • Veteran
  • *****
  • Posts: 1529
    • Official Hexer & MMod site
Re: sorting array
« Reply #1 on: April 22, 2007, 03:34:21 pm »
procedure Bubblesort(ToBeSorted: array of double);
var i,j: integer;
      temp: double;
      switched: boolean;
begin
j:=ArrayHigh(ToBeSorted)-1;
switched:=true;
while (j>1) and (switched) do begin
  switched:=false;
  for i:=1 to j do
    If ToBeSorted>ToBeSorted[i+1] then begin
      temp:=ToBeSorted;
      ToBeSorted:=ToBeSorted[i+1];
      ToBeSorted[i+1]:=temp;
      switched:=true;
    end;
  end;
end;



This procedure requires the first array index to be 1 and not 0. not sure if it works, wrote it out of my memories :D
I like to have one Martini
Two at the very most
Three I'm under the table
Four I'm under the host

Offline iftach

  • Major(1)
  • Posts: 16
Re: sorting array
« Reply #2 on: April 23, 2007, 03:56:43 am »
thanks. it doesn't work (i think you need to decrease the j somewhere),
but it helped me fix mine.
btw, there is problem with the arrayhigh, it gives Type mismatch error.

Offline Avarax

  • Veteran
  • *****
  • Posts: 1529
    • Official Hexer & MMod site
Re: sorting array
« Reply #3 on: April 23, 2007, 04:27:29 am »
oh yeah, forgot decreasing j ;Q
I like to have one Martini
Two at the very most
Three I'm under the table
Four I'm under the host