Nhập N và dãy số nguyên A= a1… an Hãy sắp xếp dãy số nguyên A thành dãy không tăng
0 bình luận về “Nhập N và dãy số nguyên A= a1… an Hãy sắp xếp dãy số nguyên A thành dãy không tăng”
Theo bình luận của bạn thì mình thấy bạn cần cách sắp xếp có độ phức tạp O(n), mình sẽ viết cho bận một chương trình quicksort (nhanh, có thể sử dụng có mảng lớn)
type tlist = array[1..max] of longint; procedure qsortAB(var a,b : tlist); procedure sort(l,r: longint); var i,j,x,y: longint; begin i:=l; j:=r; x:=a[(l+r) div 2]; while(i<=j) do begin while a[i]<x do inc(i); while x<a[j] do dec(j); if not(i>j) then begin y:=a[i]; a[i]:=a[j]; a[j]:=y; y:=b[i]; b[i]:=b[j]; b[j]:=y; inc(i); j:=j-1; end; end; if l<j then sort(l,j); if i<r then sort(i,r); end;
program Min; uses crt; var A:array[1..1000] of integer; N,M,i,j,tam:integer; begin clrscr; writeln(‘Nhap N: ‘); readln(N); for i:=1 to N do begin writeln(‘A[‘,i,’]: ‘); readln(A[i]); end; for j:=N downto 2 do for i:=1 to j-1 do if A[i]<A[i+1] then begin tam:=A[i]; A[i]:=A[i+1]; A[i+1]:=tam; end; write(‘Day da duoc sap xep la: ‘); for i:=1 to n do write(A[i]:4); readln end.
Theo bình luận của bạn thì mình thấy bạn cần cách sắp xếp có độ phức tạp O(n), mình sẽ viết cho bận một chương trình quicksort (nhanh, có thể sử dụng có mảng lớn)
type
tlist = array[1..max] of longint;
procedure qsortAB(var a,b : tlist);
procedure sort(l,r: longint);
var
i,j,x,y: longint;
begin
i:=l;
j:=r;
x:=a[(l+r) div 2];
while(i<=j) do
begin
while a[i]<x do
inc(i);
while x<a[j] do
dec(j);
if not(i>j) then
begin
y:=a[i]; a[i]:=a[j]; a[j]:=y;
y:=b[i]; b[i]:=b[j]; b[j]:=y;
inc(i);
j:=j-1;
end;
end;
if l<j then
sort(l,j);
if i<r then
sort(i,r);
end;
begin
sort(1,max);
end;
program Min;
uses crt;
var A:array[1..1000] of integer;
N,M,i,j,tam:integer;
begin
clrscr;
writeln(‘Nhap N: ‘); readln(N);
for i:=1 to N do
begin
writeln(‘A[‘,i,’]: ‘);
readln(A[i]);
end;
for j:=N downto 2 do
for i:=1 to j-1 do if A[i]<A[i+1] then
begin
tam:=A[i];
A[i]:=A[i+1];
A[i+1]:=tam;
end;
write(‘Day da duoc sap xep la: ‘);
for i:=1 to n do write(A[i]:4);
readln
end.
Em tham khảo nha.