-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNowy dokument tekstowy (5).txt
56 lines (44 loc) · 1.05 KB
/
Nowy dokument tekstowy (5).txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
// Wyszukiwanie interpolacyjne
// Data: 8.05.2008
// (C)2012 mgr Jerzy Wałaszek
//---------------------------
program prg;
const N = 100;
var
Z : array[0..N - 1] of integer;
i,ip,ik,isr,k,L,p : integer;
begin
randomize;
// wypełniamy tablicę Z[]
Z[0] := random(5);
for i := 1 to N - 1 do Z[i] := Z[i - 1] + random(5);
// generujemy klucz k
k := Z[0] + random(Z[N - 1] - Z[0] + 1);
// poszukujemy interpolacyjnie elementu k
L := 0; p := -1; ip := 0; ik := N - 1;
while (Z[ip] <= k) and (k <= Z[ik]) do
begin
inc(L);
isr := ip + (k-Z[ip])*(ik-ip) div (Z[ik]-Z[ip]);
if Z[isr] = k then
begin
p := isr; break;
end
else if k < Z[isr] then
ik := isr - 1
else
ip := isr + 1;
end;
// wyświetlamy wyniki
write(k,' : ');
if p >= 0 then write(p) else write('BRAK');
writeln(' : obiegi = ',L);
writeln;
for i := 0 to N - 1 do
begin
write(Z[i]:3);
if p = i then write('<') else write(' ');
end;
writeln;
writeln;
end.