Autor Beitrag
DarkCell
Hält's aus hier
Beiträge: 9

Win Xp
Delphi 5 pro
BeitragVerfasst: So 26.03.06 13:37 
Ich habe sechs Int-Variablen: Z1, Z2, Z3, Z4, Z5, Z6 wahlweise auch ein array[1..6]
Jede dieser Variable hat eine zufällige Zahl.
Jetzt möchte ich in der ersten Variable(/Spalte des arrays) die höchste Zahl, in der zweiten die zweithöchste, und so weiter ...

Gibt es eine "schönere" bzw bequemere Möglichkeit, dass Problem zu lösen ohne ewige if-Entscheidungen?

Danke schonmal im voraus
DarkCell


Moderiert von user profile iconChristian S.: Topic aus Sonstiges (Delphi) verschoben am So 26.03.2006 um 15:20
Gausi
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
Beiträge: 8548
Erhaltene Danke: 477

Windows 7, Windows 10
D7 PE, Delphi XE3 Prof, Delphi 10.3 CE
BeitragVerfasst: So 26.03.06 13:56 
Ganz ohne Ifs wirds nicht gehen ;-)

Aber such mal nach Suche in der Entwickler-Ecke BUBBLESORT. Das dürfte für deine Zwecke am geeignetsten sein.

_________________
We are, we were and will not be.
zemy
ontopic starontopic starontopic starontopic starofftopic starofftopic starofftopic starofftopic star
Beiträge: 207

Win XP Prof.
D7
BeitragVerfasst: Di 28.03.06 21:48 
Bubblesort, Quicksort, Shellsort, Bogosort.... Wiki unter Sortieralgorythmen. Da gibtsschon was. Ist schon was zu finden. Aber ohne If? Natülich!

ausblenden Delphi-Quelltext
1:
2:
if (a[i]>a[j])
  tausche(a[i],a[j]);


wird zu
ausblenden Delphi-Quelltext
1:
2:
while (a[i]>a[j])
  tausche(a[i],a[j]);


Obn das elegant ist, wage ich zu bezweifeln :P

MfG

_________________
LifeIsToShortToThinkAboutTheShortness
Noodles
Hält's aus hier
Beiträge: 6

VS 2005

BeitragVerfasst: Di 28.03.06 22:22 
Geht es Dir darum einen Algorithmus selbst zu entwickeln, oder eine vorhandenen Lösung zu finden?
Wenn es das Letztere ist, dann sollte Dir folgendes helfen.

ausblenden C#-Quelltext
1:
2:
3:
int[] values = { 52832217 };
Array.Sort<int>(values);
Array.Reverse(values);
MightyPit
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 22

Win XP
Delphi 2005 pro, Delphi 7
BeitragVerfasst: Mi 12.04.06 09:12 
Hmm das Funktioniert in Delphi so aber nicht. Ein Array ist in Delphi kein Objekt sondern ein eigenes Sprachmittel. Wenn man eine Liste verwendet kann man sortieren lassen. aber die Vergleichsfunktion muss man selber machen, und darin sind auch wieder mindestens 2 if enthalten
Suche in: Delphi-Forum TLIST SORTIERPROBLEM
alzaimar
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 2889
Erhaltene Danke: 13

W2000, XP
D6E, BDS2006A, DevExpress
BeitragVerfasst: Mi 12.04.06 09:47 
Ohne einen Vergleich wird und kann es nicht gehen. Aber ohne 'ständige' Vergleiche schon. In Bubble/Insertionsort etc. ist sogar nur jeweils ein 'IF' zu finden, insofern kommen diese Algorithmen schon ohne ständige If's aus :wink:

_________________
Na denn, dann. Bis dann, denn.
Robert_G
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 416


Delphi32 (D2005 PE); Chrome/C# (VS2003 E/A, VS2005)
BeitragVerfasst: Mi 12.04.06 10:30 
Ohne bedingte Sprünge wird man wohl nicht sortieren können. :mrgreen:
Man kann es höchstens so deichseln, dass man sie nicht im selbst geschriebenen Code enthält sondern an TList delegiert. :lol:
ausblenden volle Höhe 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:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
uses
  Classes;

  function CompareInts(left, right : PInteger) : Integer;
  begin
    Result := left^ - right^;
  end;

  procedure Print(list : TList);
  var
    i : Integer;
  begin
    for i := 0 to list.Count - 1 do
    begin
      Writeln(PInteger(list[i])^);
    end;
  end;

var
  list : TList;

  procedure Add(const items : array of Integer);
  var
    i : Integer;
  begin
    for i := Low(items) to High(items) do
    begin
      list.Add(@items[i]);
    end;
  end;
begin
  list := TList.Create();
  try
    Add([52832217]);

    list.Sort(@CompareInts);

    Print(list);
  finally
    list.Free();
  end;
end.
Spaceguide
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 552


(D3/D7/D8) Prof.
BeitragVerfasst: Mi 12.04.06 10:34 
Geht schon ohne IF's:

ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
procedure Sortiere(var a,b : integer);
 var temp : integer;
begin
 temp := a;

 a := (-abs(a-b)+(a+b)) div 2;
 b := (abs(temp-b)+(temp+b)) div 2;
end;


*hoffentlich merkt keiner den Beschiss* ;-)
zemy
ontopic starontopic starontopic starontopic starofftopic starofftopic starofftopic starofftopic star
Beiträge: 207

Win XP Prof.
D7
BeitragVerfasst: Mi 12.04.06 17:18 
wie währe es da mit...
ausblenden Delphi-Quelltext
1:
2:
3:
4:
procedure Sortiere(int x):int
begin
result:=x;
end;

_________________
LifeIsToShortToThinkAboutTheShortness
Spaceguide
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 552


(D3/D7/D8) Prof.
BeitragVerfasst: Mi 12.04.06 17:24 
1) Das sortiert nix
2) Procedures geben keine Werte zurück
3) Den Datentyp int gibt es in Delphi nicht
4) Bei der Deklaration einer Variablen wird der Datentyp nachgestellt und durch einen Doppelpkt. vom Variablennamen getrennt
5) Nach der Angabe des Rückgabewerts einer Funktion muss ein Strichpkt. folgen
Aristoteles
ontopic starontopic starontopic starontopic starontopic starofftopic starofftopic starofftopic star
Beiträge: 69

SuSE Linux 10.0
Lazarus 0.9; Delphi7 PE
BeitragVerfasst: Do 13.04.06 14:24 
user profile iconDarkCell hat folgendes geschrieben:

Gibt es eine "schönere" bzw bequemere Möglichkeit, dass Problem zu lösen ohne ewige if-Entscheidungen?


Nein. Ohne zwei Zahlen miteinander zu vergleichen kann man nicht feststellen, welche die größere ist.

Auch fremde Routinen wie z.B. Bubblesort müssen auf den If-Befehl zurückgreifen.
Spaceguide
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 552


(D3/D7/D8) Prof.
BeitragVerfasst: Do 13.04.06 14:53 
Man KANN entscheiden, welches die größere/kleinere Zahl von zwei Zahlen ist, ohne eine Fallunterscheidung zu verwenden, und zwar mit der obigen Routine von mir. Abs ist zwar oft als Fallunterscheidung implementiert, aber das lässt sich ja umgehen:

Größere Zahl von a und b: (Sqrt(Sqr(a-b))+(a+b))/2
jasocul
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 6393
Erhaltene Danke: 147

Windows 7 + Windows 10
Sydney Prof + CE
BeitragVerfasst: Do 13.04.06 14:59 
Damit kannst du den Wert der größeren Zahl bestimmen (Formel habe ich nicht geprüft), aber du weißt immer noch nicht welche die größere ist.
Spaceguide
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 552


(D3/D7/D8) Prof.
BeitragVerfasst: Do 13.04.06 15:11 
Eine Zahl wird ja auch nur über ihren Wert definiert.
Horschdware
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
Beiträge: 744
Erhaltene Danke: 54

Win XP Pro, Win 7 Pro x64
Delphi 7, Delphi XE, C++ Builder 5, SAP R/3
BeitragVerfasst: Do 13.04.06 15:22 
ja gut. dann weisst du wie groß die beiden sind. aber du weisst nicht welche größer ist.
das ist wie bei kuchen: vom ansehen und der beschreibung her weiss du dass zwei kuchen lecker sind. du könntest dir sogar beschreiben lassen WIE lecker sie sind. aber ohne probiert zu haben kannst du trotzdem keinen vergleich ziehen.

btw: ...die größere zahl herausfinden ist ja ein vergleich.
und wie mache ich einen vergleich ohne einen vergleich zu machen? eigentlich nicht...

_________________
Delphi: XE - OS: Windows 7 Professional x64
Spaceguide
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 552


(D3/D7/D8) Prof.
BeitragVerfasst: Do 13.04.06 15:38 
Von was redet ihr hier eigentlich? Kuchen?!?!

Hier der Beweis:

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 Sortiere(var aNumbers : array of double);
 var temp : double;
     i,j : integer;
begin
 for i:=Low(aNumbers) to High(aNumbers)-1 do
  for j:=i+1 to High(aNumbers) do
  begin
   temp := aNumbers[i];
   aNumbers[i] := (-Sqrt(Sqr(aNumbers[i]-aNumbers[j]))+
                           (aNumbers[i]+aNumbers[j]))*0.5;
   aNumbers[j] := (Sqrt(Sqr(temp-aNumbers[j]))+
                           (temp+aNumbers[j]))*0.5;
  end;
end;

procedure TForm1.Button1Click(Sender: TObject);
 var zahlen : array of double;
     i : integer;
begin
 setlength(zahlen,100);
 for i:=Low(zahlen) to High(zahlen) do zahlen[i]:=Random*100;
 Sortiere(zahlen);
 for i:=Low(zahlen) to High(zahlen) do
  memo1.lines.add(floattostr(zahlen[i]));
end;
DaRkFiRe
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
Beiträge: 526

WinXP Home & Professional
C, C++, Delphi
BeitragVerfasst: Do 13.04.06 16:13 
Ich vermute mal, dass er das Array / die Zahlen und dessen Sortierung fest im Source verankert hatte, anstatt über Schleifen zu iterieren oder über Funktionen zu rekursieren.

_________________
Lang ist der Weg durch Lehren - kurz und wirksam durch Beispiele! Seneca
jasocul
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 6393
Erhaltene Danke: 147

Windows 7 + Windows 10
Sydney Prof + CE
BeitragVerfasst: Do 13.04.06 16:45 
@Spaceguide:
Das Verfahren kann ich nachvollziehen. Über den Sinn lässt sich streiten. :wink:
delfiphan
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 2684
Erhaltene Danke: 32



BeitragVerfasst: Fr 14.04.06 11:17 
Robert_G
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 416


Delphi32 (D2005 PE); Chrome/C# (VS2003 E/A, VS2005)
BeitragVerfasst: Fr 14.04.06 12:46 
user profile icondelfiphan hat folgendes geschrieben:
=> [url=de.wikipedia.org/wik...ort]Bucketsort[/url]
Benutzt ebenfalls bedingte Sprpünge. ;)