想要一个函数:将十个整数排序(100分)

  • 主题发起人 主题发起人 yhwyb
  • 开始时间 开始时间
Y

yhwyb

Unregistered / Unconfirmed
GUEST, unregistred user!
如题,十个整数,每一个在1000以内,请高手帮一下,谢谢
 
最简单的你就用个冒泡法不就ok了吗?
 
看一下DELPHI自带的例子,关于线程的有一个例子,是关于排序的。
 
把他们全部放到StringLIst里面。调用他的排序方法就可以了。
 
creation-zy老版主也来了,有好看的了
 
StringLIst好像不能这样排序的吧?
 
快速排序,冒泡排序,插入排序,选择排序.....
 
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs;
type
TForm1 = class(TForm)
procedure FormCreate(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;
implementation
{$R *.dfm}
function Compare_i(List: TStringList;
Index1, Index2: Integer): Integer;
var
v1, v2: integer;
begin
v1 := integer(List.Objects[Index1]);
v2 := integer(List.Objects[Index2]);
Result := v1 - v2;
if Result = 0 then
Result := Index1 - Index2;
end;

procedure sort_int(var list: array of integer);
var
sl: TstringList;
i, idx: Integer;
begin
sl := TstringList.Create;
for i := low(list) to high(list) do
sl.AddObject('', TObject(list));
sl.CustomSort(Compare_i);
idx := 0;
for i := low(list) to high(list) do
begin
list := integer(sl.Objects[idx]);
inc(idx);
end;
FreeAndNil(sl);
end;

procedure TForm1.FormCreate(Sender: TObject);
var
list: array of integer;
i: integer;
begin
Randomize;
setlength(list, 10000);
for i := low(list) to high(list) do
list := Random(1000);
sort_int(list);
setlength(list, 0);
end;

end.
 
继续顶一下!!!!!~~~~~~~~~
 
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
TForm1 = class(TForm)
Memo1: TMemo;
Memo2: TMemo;
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;
implementation
{$R *.dfm}
procedure ComPear(var list: array of integer);
var i, j: Integer;
begin
for j := low(list) to high(list) do
for i := j to high(list)-1 do
if list[j] > list[i+1] then
begin
list[j] := list[j] + list[i+1];
list[i+1] := list[j] - list[i+1];
list[j] := list[j] - list[i+1];
end;
end;

procedure TForm1.Button1Click(Sender: TObject);
var
list: array of integer;
i: integer;
begin
Randomize;
setlength(list, 10);
for i := low(list) to high(list) do
list := Random(1000);
Memo1.Clear;
for i := low(list) to high(list) do
Memo1.Lines.Add(inttostr(list));
ComPear(list);
Memo2.Clear;
for i := low(list) to high(list) do
Memo2.Lines.Add(inttostr(list));
end;

end.
 
后退
顶部