
28.12.2015, 22:56
|
Прохожий
|
|
Регистрация: 28.12.2015
Сообщения: 1
Версия Delphi: Delphi 7
Репутация: 10
|
|
Количество сдвигов
Сортировка последовательности двухпутевыми вставками. Не подсчитывает количество сдвигов, оно всегда = 0. Что не так?
Код:
program Project2;
{$APPTYPE CONSOLE}
uses
SysUtils;
const
maxn=100;
type
posl=array [1..maxn] of integer;
stroka=string[30];
var
a,b:posl;
n,k:integer;
x: array[1..2*maxn] of integer;
procedure vvodposl(var a:posl;const namefile:stroka);
var
fin: textfile;
i:integer;
begin
assignfile(fin,namefile);
reset(fin);
readln(fin,n);
for i:=1 to n do
read(fin,a[i]);
close(fin);
end;
procedure sortirovka(var a,b:posl);
var
t, i, j, left, right, k: integer;
x: array[1..2*maxn] of integer;
begin
left := n;
right := n;
x[n] := a[1];
k:=0;
for i := 2 to n do
begin
t := a[i];
if t >= a[1] then
begin
Inc(right);
j := right;
while t < x[j - 1] do
begin
Inc(k);
x[j] := x[j - 1];
Dec(j);
end;
x[j] := t;
end
else
begin
Dec(left);
j := left;
while t > x[j + 1] do
begin
Inc(k);
x[j] := x[j + 1];
Inc(j);
end;
x[j] := t;
end;
end;
for j := 1 to n do
b[j] := x[j + left - 1];
end;
procedure vivodposl(namefile:stroka;const a:posl; flag:boolean);
var
i:integer;
fout:text;
begin
assign(fout,namefile);
if flag then
rewrite(fout)
else
append(fout);
write(fout,'posl a ');
for i:=1 to n do
write(fout,a[i]:5);
writeln(fout);
write(fout,'posl b ');
for i:=1 to n do
write(fout,b[i]:5);
writeln(fout);
write(fout,'k ',k);
close(fout);
end;
begin
vvodposl(a,'f1.txt');
sortirovka(a,b);
vivodposl('f2.txt',a,true);
end.
|