将一维数组的20个元素按出现频率的说少排序,然后按大到小排序。(100)

  • 主题发起人 主题发起人 leizi1128
  • 开始时间 开始时间
L

leizi1128

Unregistered / Unconfirmed
GUEST, unregistred user!
将一维数组的20个元素按出现频率的说少排序,然后按大到小排序。
 
用个排序算法排就是了,才20个元素,用冒泡都很快排完了。
 
20 个元素 按多少排序,20个元素有相同的么,先找出现频率多少的元素,最后在把这些元素排序,和直接排序有什么区别啊。
 
form中拽3个memounit Unit1;interfaceuses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls;const MAXA1=20;type TSortArray=array[1..MAXA1] of integer; TSort=class private a1value: TSortArray; a1rate: TSortArray; function GetItemValue(index: Integer): integer; function GetItemRate(index: Integer): integer; public constructor Create(a1: TSortArray); procedure Sort(); property ItemsValue[index: Integer]: integer read GetItemValue ; property ItemsRate[index: Integer]: integer read GetItemRate ; end;type TForm1 = class(TForm) Memo1: TMemo; Button1: TButton; Memo2: TMemo; Memo3: TMemo; procedure Button1Click(Sender: TObject); private { Private declarations } public { Public declarations } end;var Form1: TForm1; a1: TSortArray; implementation{$R *.dfm}procedure random_a1();var i:integer;begin Randomize; for i:=1 to MAXA1 do begin a1:=random(10); end;end;procedure sort_a1();var i:integer;beginend;constructor TSort.Create(a1: TSortArray);var i,j:integer;begin for i:=1 to MAXA1 do a1value:=-1; for i:=1 to MAXA1 do a1rate:=0; for i:=1 to MAXA1 do begin for j:=1 to MAXA1 do begin if(a1value[j]<0) or (a1value[j]=a1) then begin break; end; end; a1value[j]:=a1; a1rate[j]:=a1rate[j]+1; end;end;procedure TSort.Sort();var i,j:integer; tmp:integer;begin for i:=1 to MAXA1 do begin for j:=i+1 to MAXA1 do begin if(a1rate<a1rate[j]) then begin tmp:=a1rate; a1rate:=a1rate[j]; a1rate[j]:=tmp; tmp:=a1value; a1value:=a1value[j]; a1value[j]:=tmp; end; end; end;end;function TSort.GetItemValue(index: Integer): integer;begin if((index<=0) or (index>MAXA1)) then result:=-1 else result:=a1value[index];end;function TSort.GetItemRate(index: Integer): integer;begin if((index<=0) or (index>MAXA1)) then result:=-1 else result:=a1rate[index];end;procedure TForm1.Button1Click(Sender: TObject);var i:integer; sort1:TSort;begin random_a1(); for i:=1 to MAXA1 do memo1.Lines.Add(inttostr(a1)); sort1:=TSort.Create(a1); for i:=1 to MAXA1 do if(sort1.GetItemValue(i)>=0) then memo2.Lines.Add(inttostr(sort1.GetItemValue(i))+'--'+inttostr(sort1.GetItemRate(i))); sort1.Sort(); for i:=1 to MAXA1 do if(sort1.GetItemValue(i)>=0) then memo3.Lines.Add(inttostr(sort1.GetItemValue(i))+'--'+inttostr(sort1.GetItemRate(i)));end;end.
 
后退
顶部