Autor Beitrag
cool2005
Ehemaliges Mitglied
Erhaltene Danke: 1



BeitragVerfasst: Mo 07.03.05 15:36 
hi leute...

hab von einem kumpel ne lösung für BubbleSort bekommen...doch irgendwie kann ich den Quelltext nicht ganz verstehen...

wäre super wenn mir jemand hilft und mir sagt was die einzelnen zeilen bedeuten:

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:
unit uData;
interface

uses
  SysUtils, Classes;

type
  TFeld = array[1..8of integer;
  TDaten = class(TDataModule)
  private
    { Private declarations }
  public
    { Public declarations }
  A : TFeld;
  N : TFeld;
  procedure bubblesort(var A:TFeld;anzahl : integer);
  end;

var
  Daten: TDaten;

implementation

{$R *.dfm}
procedure swap(var x,y:integer);
var merker : integer;
begin
merker:=x;
x:=y;
y:=merker;
end;

procedure TDaten.bubblesort(var A:TFeld;anzahl : integer);
var i,j : integer;
begin
for i:= 2 to anzahl do
for j := anzahl downto i do
if A[j-1]>A[j] then
swap(A[j-1],A[j]);
end;


Das Programm läuft...doch verstehen tue ich zB. nicht wieso i= 2 ist.... :(

Bitte um HILFE....danke im voraus....bye!

Moderiert von user profile iconraziel: Delphi-Tags hinzugefügt.


Zuletzt bearbeitet von cool2005 am Mo 07.03.05 20:46, insgesamt 1-mal bearbeitet
wdbee
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 628
Erhaltene Danke: 1



BeitragVerfasst: Mo 07.03.05 16:33 
Die innerste Schleife läuft doch von Anzahl rückwärts bis I. Da für den Tausch (Swap) der eine Index I-1 ist, darf I nur bis I=2 vermindert werden.
Gausi
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
Beiträge: 8535
Erhaltene Danke: 473

Windows 7, Windows 10
D7 PE, Delphi XE3 Prof, Delphi 10.3 CE
BeitragVerfasst: Mo 07.03.05 17:57 
Sag doch bitte nochmal, was TFeld ist. Beginnt da die Indizierung bei 1 (statisches Array[1..xxx]), oder bei 0 (dynamisches Array)?

_________________
We are, we were and will not be.
cool2005
Ehemaliges Mitglied
Erhaltene Danke: 1



BeitragVerfasst: Mo 07.03.05 23:10 
Titel: okay
okay habs nochmal verbessert...
nun hab ich auch array drin...

nun würde ich gerne wissen, was die zeilen heißen bei bubblesort...

please help...

danke im voraus..
MrSaint
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 1033
Erhaltene Danke: 1

WinXP Pro SP2
Delphi 6 Prof.
BeitragVerfasst: Mo 07.03.05 23:27 
Das ist praktisch ein umgedrehter BubbleSort. Im "normalen" BubbleSort steigt das größte Element einer Liste (oder eines Arrays) nach oben auf, wie eine Blase (-> Name: BubbleSort). Bei diesem "Exemplar" sinkt das kleinste Element nach unten ab. Schau dir mal den Link an, dann solltest du den BubbleSort verstehen. Und nichts anderes ist eben hier implementiert...



MrSaint

_________________
"people knew how to write small, efficient programs [...], a skill that has subsequently been lost"
Andrew S. Tanenbaum - Modern Operating Systems
Gausi
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
Beiträge: 8535
Erhaltene Danke: 473

Windows 7, Windows 10
D7 PE, Delphi XE3 Prof, Delphi 10.3 CE
BeitragVerfasst: Mo 07.03.05 23:27 
na gut. Fangen wir bei i=2 an. Die innere Schleife kläuft dann von anzahl runter bis 2. Wenn nun A[j-1]>A[j] ist, dann ist ein großes Element vor einem kleinen Element. Das ist unschön, und daher werden mit swap(A[j-1],A[j]); die beiden Elemente vertauscht. Auf diese Weise wandert das kleinste Element des Arrays ganz nach vorne auf Position 1. Diese Position muss im nächsten Durchlauf nicht mehr betrachtet werden, daher können wir schon bei 3 aufhören (in diesem Durchlauf wandert dann das zweitkleinste Element auf Position 2)

Sind diese Inline-Delphi-Tags nicht klasse?

_________________
We are, we were and will not be.
MrSaint
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 1033
Erhaltene Danke: 1

WinXP Pro SP2
Delphi 6 Prof.
BeitragVerfasst: Mo 07.03.05 23:31 
Gausi hat folgendes geschrieben:
Sind diese Inline-Delphi-Tags nicht klasse?


Sehr schön 8) :dance:

*gg* Sorry für das OT ;)

_________________
"people knew how to write small, efficient programs [...], a skill that has subsequently been lost"
Andrew S. Tanenbaum - Modern Operating Systems