Fórum Algoritimo dos 5 maiores no vetor #341240
03/05/2007
0
e preciso retornar os 5 maiores valores dele..
minha primeira ideia seria:
aux: array[0..4] of double;
for I := 0 to 4 do
begin
maior := 0;
for X := 0 to Total - 1 do
begin
if (vetor[X] >= maior) then
begin
maior := vetor[X];
end
end;
aux[I] := maior;
end;
alguem conhece alguma forma mais rapida de retornar isto??
pos preciso de velocidade..
abraço..
Nightshade
Curtir tópico
+ 0Posts
03/05/2007
Acacio
Método da bolha mais ou menos
for i := 1 to high(vetor) do
for j := i to high(vetor) do
if vetor[i] < vetor[j] then
begin
varAux := vetor[i];
vetor[i] := vetor[j];
vetor[j] := varAux
end;
mais ou menos isso;
Gostei + 0
03/05/2007
Rjun
Gostei + 0
04/05/2007
Motta
http://www.nist.gov/dads/
Gostei + 0
04/05/2007
Nightshade
var
X,Y,Z: Integer;
ListaV, ListaN: TStringList;
Maior,MAux,Atual: Double;
MCorret: String;
begin
// try
L.Items.Clear;
ListaV := TStringList.Create;
ListaN := TStringList.Create;
Maior := 0;
MAux := 1000000000000;
//Verifica os 5 maiores
for Y := 0 to 4 do
begin
//Ordena Internamente
Maior := 0;
for Z := 0 to S.Items.Count -1 do
begin
Atual := (StrToFloat(S.Items.Strings[Z]));
if ((Atual <= MAux) and (Atual >= Maior)) then
begin
Maior := StrToFloat(S.Items.Strings[Z]);
MCorret := IntToStr(Z + 1);
end;
end;
MAux := Maior;
with L.Items.Add do
begin
Caption := IntToStr(Y + 1);
SubItems.Add(MCorret);
SubItems.Add(FloatToStr(Maior));
end;
end;
// except
//
// end;
end;
to tentando com esta função..
o stringbox eh temporario, eh so pra teste q to usando ele, mais tarde sera um stringlist.. mas n importa :p
o problema que está ocorrendo é que ele retorna 5X o mesmo valor, que realmente eh o maior da lista..
Gostei + 0
04/05/2007
Marco Salles
var i:integer; num:Double; aux: array[1..5] of double ; vetor: array [1..150] of double ; begin //claro que aqui devo carregar o aux=[0,0,0,0,0] for i := 1 to high(vetor) do begin num:=vetor[i]; if vetor[i] > aux[1] then begin aux[5]:=aux[4]; aux[4]:=aux[3]; aux[3]:=aux[2]; aux[2]:=aux[1]; aux[1]:=num; end else if vetor[i] > aux[2] then begin aux[5]:=aux[4]; aux[4]:=aux[3]; aux[3]:=aux[2]; aux[2]:=num; end else if vetor[i] > aux[3] then begin aux[5]:=aux[4]; aux[4]:=aux[3]; aux[3]:=num; end else if vetor[i] > aux[4] then begin aux[5]:=aux[4]; aux[4] :=num end else if vetor[i] > aux [5] then aux[5]:=num; end; end;
Sinceramente não vejo muito o que se ganha em ordenar 150 posiçoes , enquanto que por outro lado so se precisa é de reogarnizar as vezes cinco , quatro , tres , duas , uma ou nenhuma posição do vetor Aux
Gostei + 0
05/05/2007
Marco Salles
Uma forma mais elegante seria
var i,j,p:integer; num:Double; aux: array[1..5] of double ; begin for i := 1 to high(vetor) do begin num:=vetor[i]; for j := 1 to 5 do begin if vetor[i] > aux [j] then begin for p:=5 downto j do aux[p]:=aux[p-1]; aux[j]:=num; Break; end; end; end;
Gostei + 0
08/05/2007
Nightshade
Gostei + 0
10/05/2007
Marco Salles
[b:fb92ebda95]citação de nightshade[/b:fb92ebda95]
Gostei + 0
Clique aqui para fazer login e interagir na Comunidade :)