Skip to content

Instantly share code, notes, and snippets.

@DasLampe
Created May 5, 2009 08:29
Show Gist options
  • Save DasLampe/106893 to your computer and use it in GitHub Desktop.
Save DasLampe/106893 to your computer and use it in GitHub Desktop.
Bubblesort, Delphi, Example (from teacher)
unit bubblesort;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
TForm1 = class(TForm)
lb_demo: TListBox;
bt_add: TButton;
bt_sort: TButton;
ed_add: TEdit;
procedure bt_addClick(Sender: TObject);
procedure bt_sortClick(Sender: TObject);
private
{ Private-Deklarationen }
public
{ Public-Deklarationen }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.bt_addClick(Sender: TObject);
var i, anz: integer;
begin
randomize;
i := 0;
anz := StrToInt(ed_add.Text);
lb_demo.Clear;
repeat
lb_demo.Items.Add(IntToStr(random(1000)));
i := i +1;
until i = anz;
end;
procedure TForm1.bt_sortClick(Sender: TObject);
var i,j: integer;
str: string;
getauscht: boolean;
begin
j := lb_demo.Items.count -1; //Bubblesort
repeat
i := 0;
getauscht := false;
repeat
if lb_demo.Items[i] > lb_demo.Items[i+1] then
begin
getauscht := true;
str := lb_demo.Items[i];
lb_demo.Items[i] := lb_demo.Items[i +1];
lb_demo.Items[i+1] := str;
end;
i := i+1;
until i >= j;
j := j -1;
until (j = 0) or not getauscht;
end;
end.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment