С помощью алгоритма сортировки с убывающим шагом отсортируйте четные элементы массива 4 8 31 5 6 47 12.
Я сделал, но прога сортирует все значения. Как сделать так, чтобы сортировало только чётные?
Var tmp,m:Array[1..10]of LongInt;
t,i:LongInt;
Procedure Sort(st,t:LongINt);
Var u,Key,i:LongInt;
Begin
While st<=7 Do
Begin
u:=st;
Key:=m[u];
While (u-t>0)And(m[u-t]>Key) Do
Begin
m[u]:=m[u-t];
Dec(u,t);
End;
m[u]:=Key;
st:=st+t;
End;
End;
Begin
For i:=1 to 7 Do
begin
write('m[',i,']=');
readln(m[i]);
end;
tmp:=m;
t:=1 Shl 10;
While t>0 Do
Begin
If t<=7 Then
For i:=1 To t Do Sort(i,t);
t:=t Shr 1;
End;
For i:=1 To 7 Do Writeln(tmp[i],' ',m[i]);
WriteLn;
readln;
End.
Вот самый прстой вариант :
uses crt;
const
n = 10;
var
x : array [1..n] of integer = (1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
a, b, T : integer;
begin
for a := 1 to n - 1 do
for b := a + 1 to n do
if not(odd(x[a])) and not(odd(x[b])) and not(x[a] >= x[b]) then begin
T := x[a];
x[a] := x[b];
x[b] := T;
end;
for a := 1 to n do write(x[a], ' ');
end.
klem4, внимательнее читаем задание:
Метод Шелла (убывающий шаг)
uses crt;
const
n = 10;
var
x : array [1..n] of integer = (1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
i, j, d, T : integer;
begin
clrscr;
d := n div 2;
repeat
i := d;
repeat
j := i - d + 1;
repeat
if not(odd(x[j])) and not(odd(x[j + d])) then
if not(x[j] >= x[j + d]) then begin
T := x[j];
x[j] := x[j + d];
x[j + d] := T;
end;
dec(j);
until (j < 1);
inc(i);
until (i > n);
d := d div 2;
until (d <= 0);
for i := 1 to n do write(x[i], ' ');
readln;
end.
Спасибо, конечно, но я уже сдал. Немного схитрил, просто вывел только чётные элементы и всё.