var
x : t;
m, i : integer;
{---------------------------------------------------------------------------------------}
Procedure create(n : integer; var x : t);
var
i : integer;
begin
randomize;
writeln('Berilgan butun sonli massiv’);
for i := 1 to n do
begin
x[i] := random(201)-100;
write(x[i], ' ')
end;
writeln
end;
{----------------------------------------------------------------------------------------}
Procedure exchange(l, r : integer);
var
p : integer;
begin
p := x[l]; x[l] := x[r]; x[r] := p
end;
{----------------------------------------------------------------------------------------}
Procedure middle(k : integer; var x : t; var m : integer);
var
l, r : integer;
begin
l := 1; r := k;
repeat
while (x[l] <= x[r]) and (l < r) do r := r - 1;
exchange(l, r); {Almashtirish prosedurasi}
while (x[l] <= x[r]) and (l < r) do l := l + 1;
exchange(l, r)
until l = r;
m := l
end;
{---------------------------------------------------------------------------------------}
begin
create(n, x);
middle(n, x, m);
write('O’rta elementli o’zgartirilgan massiv', x[m]);
writeln(m, 'chi o’rinda');
for i := 1 to n do write(x[i], ' ');
writeln
end.
Bu dasturni bir necha marta bajarib ko’ring va “o’rta” elementning massivdagi joylashishga e’tibor bering. Siz shunga e’tibor berishingiz kerakki, har doim ham (aniqrog’i, kamdan-kam hollarda) “o’rta” element massivdagi haqiqiy o’rta joyni egallayvermaydi, lekin bu uning tezkorligiga hech qanday ta’sir qilmaydi.
Do'stlaringiz bilan baham: |