请问 :10;20;30;98;100;200 怎么写进行排序算法啊 !(100分)

  • 主题发起人 主题发起人 007
  • 开始时间 开始时间
0

007

Unregistered / Unconfirmed
GUEST, unregistred user!
10;20;30;98;100;200 我想对这几个数字进行 升序 和 降序的 排序怎么写啊
请高手写个 例子给兄弟!!谢谢::
 
冒泡法-最简单
其它的二叉树法等等,看看数据结构吧
 
http://www.delphibbs.com/delphibbs/dispq.asp?lid=2524302
 
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
TForm1 = class(TForm)
Memo1: TMemo;
Edit1: TEdit;
Button1: TButton;
Button2: TButton;
procedure FormCreate(Sender: TObject);
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;
data: array[1..20] of integer;
implementation
uses Math;
{$R *.dfm}
procedure TForm1.FormCreate(Sender: TObject);
var
i: integer;
begin

Edit1.Text := '';
for i := low(data) to high(data)do
begin
data := Random(100);
if Edit1.Text <> '' then
Edit1.Text := Edit1.Text + ';';
Edit1.Text := Edit1.Text + inttostr(data);
end;
end;

procedure TForm1.Button1Click(Sender: TObject);
var
sl: TstringList;
i: integer;
begin
sl := TstringList.Create;
for i := low(data) to high(data)do
begin
sl.Add(format('%20s%20s', [inttostr(data), inttostr(i)]));
end;
sl.Sort;
memo1.Text := '';
for i := 0 to sl.count - 1do
memo1.Lines.Add(trim(copy(sl, 1, 20)));
freeandnil(sl);
end;

procedure TForm1.Button2Click(Sender: TObject);
var
sl: TstringList;
i: integer;
begin
sl := TstringList.Create;
for i := low(data) to high(data)do
begin
sl.Add(format('%20s%20s', [inttostr(data), inttostr(i)]));
end;
sl.Sort;
memo1.Text := '';
for i := sl.count - 1do
wnto 0do
memo1.Lines.Add(trim(copy(sl, 1, 20)));
freeandnil(sl);
end;

initialization
Randomize;
end.

object Form1: TForm1
Left = 192
Top = 107
Width = 696
Height = 480
Caption = 'Form1'
Color = clBtnFace
Font.Charset = DEFAULT_CHARSET
Font.Color = clWindowText
Font.Height = -11
Font.Name = 'MS Sans Serif'
Font.Style = []
OldCreateOrder = False
OnCreate = FormCreate
PixelsPerInch = 96
TextHeight = 13
object Memo1: TMemo
Left = 503
Top = 0
Width = 185
Height = 453
Align = alRight
Lines.Strings = (
'Memo1')
ScrollBars = ssBoth
TabOrder = 0
end
object Edit1: TEdit
Left = 0
Top = 32
Width = 441
Height = 33
TabOrder = 1
Text = 'Edit1'
end
object Button1: TButton
Left = 128
Top = 168
Width = 75
Height = 25
Caption = 'Button1'
TabOrder = 2
OnClick = Button1Click
end
object Button2: TButton
Left = 128
Top = 200
Width = 75
Height = 25
Caption = 'Button2'
TabOrder = 3
OnClick = Button2Click
end
end
 
楼上说的对啊!用冒泡法排序最简单!
 
多人接受答案了。
 
后退
顶部