It is required to implement a recursive division of a segment in half. My version of the algorithm (pseudocode, for simplicity, comments in {}
):
proc divide(B, E) {B - начало отрезка, E - конец отрезка} M = (B + E) div 2 {M - очередная искомая середина} ata(M) {процедура добавления в массив} if M = 1 then exit divide(B, M) {рекурсивный поиск следующей середины отрезка, начало-середина} divide(M, E) {рекурсивный поиск следующей середины отрезка, середина-конец}
The procedure is called in the program body as follows:
divide(X1,X2)
The ata(M)
procedure simply adds the M
value to the array.
The procedure should divide the segment with the initial coordinates on the X
axis equal to X1
and X2
in half, the halves still in half and so on, until the length is 1
. The problem must be solved precisely by recursion. Help to deal with the problem, because only the extreme halves are taken as it seemed to me. Is recursion implemented correctly?