AGB  ·  Datenschutz  ·  Impressum  







Anmelden
Nützliche Links
Registrieren
Thema durchsuchen
Ansicht
Themen-Optionen

BubbleSort Problem

Ein Thema von Chiqupon · begonnen am 7. Dez 2013 · letzter Beitrag vom 8. Dez 2013
Antwort Antwort
Seite 1 von 2  1 2      
Chiqupon

Registriert seit: 7. Okt 2013
6 Beiträge
 
#1

BubbleSort Problem

  Alt 7. Dez 2013, 22:39
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

Geändert von Chiqupon ( 8. Dez 2013 um 12:15 Uhr)
  Mit Zitat antworten Zitat
Benutzerbild von himitsu
himitsu

Registriert seit: 11. Okt 2003
Ort: Elbflorenz
43.139 Beiträge
 
Delphi 12 Athens
 
#2

AW: BubbleSort Problem

  Alt 7. Dez 2013, 22:45
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?
Garbage Collector ... Delphianer erzeugen keinen Müll, also brauchen sie auch keinen Müllsucher.
my Delphi wish list : BugReports/FeatureRequests

Geändert von himitsu ( 7. Dez 2013 um 22:47 Uhr)
  Mit Zitat antworten Zitat
Chiqupon

Registriert seit: 7. Okt 2013
6 Beiträge
 
#3

AW: BubbleSort Problem

  Alt 8. Dez 2013, 00:59
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
  Mit Zitat antworten Zitat
Volker Z.

Registriert seit: 3. Dez 2012
Ort: Augsburg, Bayern, Süddeutschland
419 Beiträge
 
Delphi XE4 Ultimate
 
#4

AW: BubbleSort Problem

  Alt 8. Dez 2013, 01:31
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ß
Volker Zeller
  Mit Zitat antworten Zitat
Chiqupon

Registriert seit: 7. Okt 2013
6 Beiträge
 
#5

AW: BubbleSort Problem

  Alt 8. Dez 2013, 02:20
Danke Es funktioniert soweit nur egal was ich versuche ich kriege es nur hin das Listbox2 unsortiert ist und Listbox 1 sortiert :/
  Mit Zitat antworten Zitat
Volker Z.

Registriert seit: 3. Dez 2012
Ort: Augsburg, Bayern, Süddeutschland
419 Beiträge
 
Delphi XE4 Ultimate
 
#6

AW: BubbleSort Problem

  Alt 8. Dez 2013, 02:35
Hallo,

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

Gruß
Volker Zeller
  Mit Zitat antworten Zitat
Chiqupon

Registriert seit: 7. Okt 2013
6 Beiträge
 
#7

AW: BubbleSort Problem

  Alt 8. Dez 2013, 02:55
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.
  Mit Zitat antworten Zitat
Volker Z.

Registriert seit: 3. Dez 2012
Ort: Augsburg, Bayern, Süddeutschland
419 Beiträge
 
Delphi XE4 Ultimate
 
#8

AW: BubbleSort Problem

  Alt 8. Dez 2013, 03:22
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ß
Volker Zeller
  Mit Zitat antworten Zitat
Benutzerbild von himitsu
himitsu

Registriert seit: 11. Okt 2003
Ort: Elbflorenz
43.139 Beiträge
 
Delphi 12 Athens
 
#9

AW: BubbleSort Problem

  Alt 8. Dez 2013, 09:44
Wie gesagt, du hast zwar eine Methode BubbleSort implementiert, sie aber nicht verwendet, sondern stattdessen die eigene Sortierung der ListBox (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.
Garbage Collector ... Delphianer erzeugen keinen Müll, also brauchen sie auch keinen Müllsucher.
my Delphi wish list : BugReports/FeatureRequests
  Mit Zitat antworten Zitat
Benutzerbild von Sir Rufo
Sir Rufo

Registriert seit: 5. Jan 2005
Ort: Stadthagen
9.454 Beiträge
 
Delphi 10 Seattle Enterprise
 
#10

AW: BubbleSort Problem

  Alt 8. Dez 2013, 11:18
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.
Kaum macht man's richtig - schon funktioniert's
Zertifikat: Sir Rufo (Fingerprint: ‎ea 0a 4c 14 0d b6 3a a4 c1 c5 b9 dc 90 9d f0 e9 de 13 da 60)
  Mit Zitat antworten Zitat
Antwort Antwort
Seite 1 von 2  1 2      


Forumregeln

Es ist dir nicht erlaubt, neue Themen zu verfassen.
Es ist dir nicht erlaubt, auf Beiträge zu antworten.
Es ist dir nicht erlaubt, Anhänge hochzuladen.
Es ist dir nicht erlaubt, deine Beiträge zu bearbeiten.

BB-Code ist an.
Smileys sind an.
[IMG] Code ist an.
HTML-Code ist aus.
Trackbacks are an
Pingbacks are an
Refbacks are aus

Gehe zu:

Impressum · AGB · Datenschutz · Nach oben
Alle Zeitangaben in WEZ +1. Es ist jetzt 00:11 Uhr.
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