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
 
Benutzerbild von Sir Rufo
Sir Rufo

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

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
 


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 21:55 Uhr.
Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024-2025 by Thomas Breitkreuz