Delphi-PRAXiS
Seite 1 von 2  1 2      

Delphi-PRAXiS (https://www.delphipraxis.net/forum.php)
-   Algorithmen, Datenstrukturen und Klassendesign (https://www.delphipraxis.net/78-algorithmen-datenstrukturen-und-klassendesign/)
-   -   BubbleSort Problem (https://www.delphipraxis.net/177955-bubblesort-problem.html)

Chiqupon 7. Dez 2013 22:39

BubbleSort Problem
 
Hallo :)
Code:
unit Unit1;

{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, StdCtrls;

type

  { TForm1 }

  TForm1 = class(TForm)
    Button1: TButton;
    Button2: TButton;
    ListBox1: TListBox;
    ListBox2: TListBox;
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure FormCreate(Sender: TObject);
  private
        procedure BubbleSort(Items: TStrings);
    { private declarations }
  public
    { public declarations }
  end;

var
  Form1: TForm1;

implementation
procedure TForm1.BubbleSort(Items: TStrings); var done: boolean; i, n: integer;
Dummy: string;
begin
 n := Items.Count;
 repeat
  done := true;
  for i := 0 to n - 2 do
  if StrToInt(Items[i]) > StrToInt(Items[i - 1]) then
   begin
   Dummy := Items[i];
   Items[i] := Items[i - 1];
   Items[i - 1] := Dummy;
   done := false;
  end;
  until done;
 end;
{$R *.lfm}

{ TForm1 }

procedure TForm1.FormCreate(Sender: TObject);
begin
randomize;
end;

procedure TForm1.Button1Click(Sender: TObject);
var zahl: integer;
begin
 zahl:=random(100)-1;
 ListBox1.Items.add(IntToStr(zahl));
 listbox2.items:=ListBox1.Items;
 listbox2.sorted:=true;
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
  ListBox1.Caption := '';
  ListBox2.Caption := '';
end;

end.
Er erstellt zwar Zufalls Zahlen und sortiert sie aber er sortiert nur nach der 1 Zahl also z.B 2 ist angeblich größer als 14 oder 7 ist größer als 67. Kann mir da jemand helfen? :/ Wäre echt nett :)

himitsu 7. Dez 2013 22:45

AW: BubbleSort Problem
 
Da du "Strings" vergleichst, ist das natürlich korrekt.

Wenn du die Werte beim Sortieren in "Zahlen" verwandelt und Diese vergleichst, dann wird es deinen Wünschen entsprechen.


PS: Wozu hast du eigentlich eine "BubbleSort"-Sortier-Funktion, die das gewünschte Verhalten beinhaltet, wenn du sie nicht verwendest?

Chiqupon 8. Dez 2013 00:59

AW: BubbleSort Problem
 
Also es geht darum ich schreibe am Montag eine Klausur und ich muss folgendes können:
2 Listboxen in der 1 sollen Zufallszahlen kommen und in der 2 sollen diese sortiert wiedergegeben werden mit
einem BubbleSort

Volker Z. 8. Dez 2013 01:31

AW: BubbleSort Problem
 
Hallo,

Zitat:

Also es geht darum ich schreibe am Montag eine Klausur und ich muss folgendes können:
2 Listboxen in der 1 sollen Zufallszahlen kommen und in der 2 sollen diese sortiert wiedergegeben werden mit
einem BubbleSort
Bubble-Sort funktioniert so:
Delphi-Quellcode:
procedure TForm1.BubbleSort;
var
  i, j : Integer;

  procedure Swap (const Index0, Index1 : Integer);
  var
    i0, i1 : Integer;
    s     : string;
  begin
    i0 := StrToInt (ListBox1.Items [Index0]);
    i1 := StrToInt (ListBox1.Items [Index1]);
    if i0 > i1 then
      begin
        s := ListBox1.Items [Index0];

        ListBox1.Items [Index0] := ListBox1.Items [Index1];
        ListBox1.Items [Index1] := s
      end
  end;

begin
  for i := 0 to ListBox1.Items.Count - 1 do
    for j := ListBox1.Items.Count - 1 downto i do
      Swap (i, j)
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  ListBox1.Items.Add (IntToStr (Random (100) - 1));
  BubbleSort
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
  Randomize
end;
Jetzt musst Du Dir nur noch darüber klar werden was da so passiert und die Geschichte in die ListBox2 bringen.

Gruß

Chiqupon 8. Dez 2013 02:20

AW: BubbleSort Problem
 
Danke Es funktioniert soweit nur egal was ich versuche ich kriege es nur hin das Listbox2 unsortiert ist und Listbox 1 sortiert :/

Volker Z. 8. Dez 2013 02:35

AW: BubbleSort Problem
 
Hallo,

schon mal ein:
Delphi-Quellcode:
ListBox2.Items.Assign (ListBox1.Items);
versucht?

Gruß

Chiqupon 8. Dez 2013 02:55

AW: BubbleSort Problem
 
Klappt auch nicht hier mal mein Quellcode:
Code:
unit Unit1;

{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, StdCtrls;

type

  { TForm1 }

  TForm1 = class(TForm)
    Button1: TButton;
    ListBox1: TListBox;
    ListBox2: TListBox;
    procedure Button1Click(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure BubbleSort;
  private
    { private declarations }
  public
    { public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.lfm}
procedure TForm1.BubbleSort;
var
  i, j : Integer;

  procedure Swap (const Index0, Index1 : Integer);
  var
    i0, i1 : Integer;
    s : string;
  begin
    i0 := StrToInt (ListBox1.Items [Index0]);
    i1 := StrToInt (ListBox1.Items [Index1]);
    if i0 > i1 then
      begin
        s := ListBox1.Items [Index0];

        ListBox1.Items [Index0] := ListBox1.Items [Index1];
        ListBox1.Items [Index1] := s
      end
  end;

begin
  for i := 0 to ListBox1.Items.Count - 1 do
    for j := ListBox1.Items.Count - 1 downto i do
      Swap (i, j)
end;
{ TForm1 }

procedure TForm1.Button1Click(Sender: TObject);
begin
  ListBox1.Items.Add (IntToStr (Random (100) - 1));
  ListBox2.Items.Assign (ListBox1.Items);
  Bubblesort
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
  Randomize;
end;

end.

Volker Z. 8. Dez 2013 03:22

AW: BubbleSort Problem
 
Hallo,

naja, das ist schon klar, wenn Du denn BubbleSort erst nach der Zuweisung an ListBox1 ausführst, dann kann das natürlich nicht funktionieren. Klatsch Dir doch mal noch ein weiteren TButton auf Dein Formular und versuch es so:
Delphi-Quellcode:
procedure TForm1.BubbleSort;
var
 i, j : Integer;

 procedure Swap (const Index0, Index1 : Integer);
 var
   i0, i1 : Integer;
   s : string;
 begin
   i0 := StrToInt (ListBox1.Items [Index0]);
   i1 := StrToInt (ListBox1.Items [Index1]);
   if i0 > i1 then
     begin
       s := ListBox1.Items [Index0];

       ListBox1.Items [Index0] := ListBox1.Items [Index1];
       ListBox1.Items [Index1] := s
     end
 end;

begin
 for i := 0 to ListBox1.Items.Count - 1 do
   for j := ListBox1.Items.Count - 1 downto i do
     Swap (i, j)
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  ListBox1.Items.Add (IntToStr (Random (100) - 1));
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
  BubbleSort;
  ListBox2.Items.Assign (ListBox1.Items)
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
  Randomize
end;
Gruß

himitsu 8. Dez 2013 09:44

AW: BubbleSort Problem
 
Wie gesagt, du hast zwar eine Methode BubbleSort implementiert, sie aber nicht verwendet, sondern stattdessen die eigene Sortierung der ListBox (
Delphi-Quellcode:
ListBox2.Sorted:=True;
) verwendet.



@Volker Z.: Wenn man davon ausgeht, daß ListBox1 die unsortierte Liste darstellt und ListBox2 die Sortierte, dann ist bei dir ein Fehler drin, denn du sortierrst Beides.
Erst Die ListBox1 in die ListBox2 kopieren und dann die ListBox2 sortieren.

Außerdem wäre es nicht schlecht, wenn man die zu sortierende Liste ListBox als Parameter an die Methode BubbleSort übergeben hätte, dann müßte man jetzt nicht die komplette BubbleSort-Methode umbauen, sondern bräuchte nur Button2Click "reparieren".

Außerdem macht deine Prozedur Swap etwas total Falsches, denn eigentlich müßßte sie CompareAndSwap heißen, da sie sonst nicht das macht, was sie behauptet,
oder man muß den Vergleich da rausnehmen.

PS: Ja, man kann zwar das Befehlsabschlußzeichen ; weglassen, aber ich empfehle die es dennoch zu schreiben, wenn Eines hingehören würde.
- erstmal sieht der Code dann einheitlicher aus
- und dann schreib mal einen weiteren Befehl dahinter und freu dich, wie der Compiler erstmal rummeckert.

Sir Rufo 8. Dez 2013 11:18

AW: BubbleSort Problem
 
Zitat:

Zitat von Chiqupon (Beitrag 1238924)
2 Listboxen in der 1 sollen Zufallszahlen kommen und in der 2 sollen diese sortiert wiedergegeben werden mit
einem BubbleSort

Wenn ich mir mal diese Anforderung durchlese, dann kann man diese Anforderung auch wie folgt interpretieren:
Code:
ZeigeListeInListBox( Liste, ListBox )
// Eine Liste wird in einer Listbox angezeigt
SortiereList( Liste )
// Sortiert eine Liste
FülleZufallsListe( Liste, Anzahl )
// Füllt eine Liste mit Anzahl zufällig ausgewählten Elementen
Der grundlegende Ablauf sieht dann wie folgt aus
Code:
MeineListe : Liste

FülleZufallsListe( MeineListe, 20 )
ZeigeListeInListBox( MeineListe, ListBox1 )
SortiereListe( MeineListe )
ZeigeListeInListBox( MeineListe, ListBox2 )
Und der fertige Code sieht dann auch ganz übersichtlich aus:
Delphi-Quellcode:
unit ViewFormMain;

interface

  uses
    Winapi.Windows,
    Winapi.Messages,
    System.SysUtils,
    System.Variants,
    System.Classes,
    Vcl.Graphics,
    Vcl.Controls,
    Vcl.Forms,
    Vcl.Dialogs,
    Vcl.StdCtrls;

  type
    TListType = array of Integer;

    TMainFormView = class( TForm )
      Unsorted_ListBox : TListBox;
      Unsorted_GroupBox : TGroupBox;
      Sorted_GroupBox : TGroupBox;
      Sorted_ListBox : TListBox;
    DoWork_Button: TButton;
      procedure DoWork_ButtonClick( Sender : TObject );
    private
      { Private-Deklarationen }
    public
      { Public-Deklarationen }
    end;

  var
    MainFormView : TMainFormView;

implementation

{$R *.dfm}

  procedure Swap( var Left, Right : Integer );
    var
      LTemp : Integer;
    begin
      LTemp := Left;
      Left := Right;
      Right := LTemp;
    end;

  procedure SortList( var AList : TListType );
    var
      LIdx, LCompareIdx : Integer;
    begin
      for LIdx := low( AList ) to high( AList ) - 1 do
        begin
          for LCompareIdx := high( AList ) downto LIdx do
            begin
              if AList[LIdx] > AList[LCompareIdx]
              then
                Swap( AList[LIdx], AList[LCompareIdx] );
            end;
        end;
    end;

  procedure FillRandomList( var AList : TListType; ACount : Integer );
    var
      LIdx : Integer;
    begin
      SetLength( AList, ACount );
      for LIdx := low( AList ) to high( AList ) do
        begin
          AList[LIdx] := Random( 100 );
        end;
    end;

  procedure ShowListInListBox( const AList : TListType; AListBox : TListBox );
    var
      LIdx : Integer;
    begin
      AListBox.Items.BeginUpdate;
      try
        AListBox.Clear;

        for LIdx := low( AList ) to high( AList ) do
          begin
            AListBox.Items.Add( IntToStr( AList[LIdx] ) );
          end;

      finally
        AListBox.Items.EndUpdate;
      end;
    end;

  procedure TMainFormView.DoWork_ButtonClick( Sender : TObject );
    var
      LList : TListType;
    begin
      FillRandomList( LList, 20 );
      ShowListInListBox( LList, Unsorted_ListBox );
      SortList( LList );
      ShowListInListBox( LList, Sorted_ListBox );
    end;

end.


Alle Zeitangaben in WEZ +1. Es ist jetzt 19:11 Uhr.
Seite 1 von 2  1 2      

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024 by Thomas Breitkreuz