Autor Beitrag
elcomportal
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 48

Win XP home
D5 standard
BeitragVerfasst: Mo 23.07.07 14:11 
Hallo,
ich habe wieder ein kleines Problem.

Also ich habe 2 arrays.

a[0] := 0;
a[1] := 4;
a[2] := 0;
a[3] := 1;

harray[0]:= 1;
harray[1]:= 2;
harray[2]:= 3;
harray[3]:= 4;

So, wenn ich dann die Quicksort-Routine aufrufe sollen die Elemente von a sortiert werden.
Die Elemente von harray sollen genau so sortiert werden wie die von a. Also soll rauskommen:

a[0] := 0 vom Element a[0]
a[1] := 0 vom Element a[2]
a[2] := 1 vom Element a[3]
a[3] := 4 vom Element a[1]

und harray muss dann so sortiert sein:
harray[0]:= 1;
harray[1]:= 3;
harray[2]:= 4;
harray[3]:= 2;

Ich habe in die Quicksort-Routine einfach die Zuweisung für harray mit eingefügt.
Aber das funktioniert nicht. Ich weiss nicht wie, aber die Zuordnung für harray ist komplett durcheinander. A wird richtig sortiert. Kann mir da bitte mal jemand helfen?.#Danke
Torsten Müller

ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
 procedure TForm3.QuickSort(var A: array of Integer; iLo, iHi: Integer);
 var
   Lo, Hi, Mid, T: Integer;
 begin
   Lo := iLo;
   Hi := iHi;
   Mid := A[(Lo + Hi) div 2];
   repeat
     while A[Lo] < Mid do Inc(Lo);
     while A[Hi] > Mid do Dec(Hi);
     if Lo <= Hi then
     begin
       T := A[Lo];
       A[Lo] := A[Hi];
       A[Hi] := T;
       T := harray[Lo];
       harray[Lo] := harray[Hi];
       harray[Hi] := T;
       Inc(Lo);
       Dec(Hi);
     end;
   until Lo > Hi;
   if Hi > iLo then QuickSort(A, iLo, Hi);
   if Lo < iHi then QuickSort(A, Lo, iHi);
 end;

_________________
Starkstrom macht klein und hässlich
Narses
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Administrator
Beiträge: 10183
Erhaltene Danke: 1256

W10ent
TP3 .. D7pro .. D10.2CE
BeitragVerfasst: Mo 23.07.07 14:38 
Moin!

Ich kann im Algo keinen Fehler sehen; aber du übergibst das zweite Array nicht. :? Sicher, dass die Sichtbarkeit des Arrays für die Prozedur korrekt ist? :nixweiss:

cu
Narses

_________________
There are 10 types of people - those who understand binary and those who don´t.
elcomportal Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 48

Win XP home
D5 standard
BeitragVerfasst: Mo 23.07.07 14:47 
user profile iconNarses hat folgendes geschrieben:
Moin!
Sicher, dass die Sichtbarkeit des Arrays für die Prozedur korrekt ist?


Die harray ist global definiert und es wird ja auch was damit gemacht, nur es ist hinterher nicht so sortiert, wie das andere array.

Mfg
Torsten

_________________
Starkstrom macht klein und hässlich
BenBE
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 8721
Erhaltene Danke: 191

Win95, Win98SE, Win2K, WinXP
D1S, D3S, D4S, D5E, D6E, D7E, D9PE, D10E, D12P, DXEP, L0.9\FPC2.0
BeitragVerfasst: Di 24.07.07 00:19 
Zwei Möglichkeiten:
- Nutze einen Record um a und harry in ein Array unter zu bringen
- Lege ein zusätzliches Array ArrIndex an, in dem Du die vertauschten Elemente einträgst.

Also: A und harry bleiben unsortiert. Anfangs hat ArrIndex normal 0,1,2,3,4,5 ... Wenn Du jetzt zwei Elemente in A tauschen willst, tauscht Du einfach die Indizes in ArrIndex, anstatt die Daten in A. Um nun die korrekte Reihenfolge beim Zugriff auf die Daten zu erhalten, ist der Zugriff nicht A[Index], sondern a[ArrIndex[Index]]. Das Gleiche gilt für harry.

_________________
Anyone who is capable of being elected president should on no account be allowed to do the job.
Ich code EdgeMonkey - In dubio pro Setting.