There is a well-known result that for any $x \in(0,1)$, given by $$x = \sum_{j = 1}^{\infty} a_j 3^{-j}$$ and let $N=\min \left\{j: a_{j}=1\right\}$, i.e., the index for the first time we have $a_{j}=1$. Now, let $b_{j}=a_{j} / 2$ for $j<N$ (note that $a_{j}$ must be 0 or 2 for $j<N$, which means $b_{j}$ must be 0 or 1 ), and $b_{N}=1$, and then we have for the cantor function $g(x)$, $$ g(x)=\sum_{j=1}^{N} b_{j} 2^{-j}. $$ For example :
if $\frac{1}{4}$ is $0.02020202\dots$ in base $3$. There are no 1s so the next stage is still $0.02020202\dots$ This is rewritten as $0.01010101\dots$ When read in base $2$, this corresponds to $\frac{1}{3}$, so $g(\frac{1}{4}) = \frac{1}{3}$.On the other hand, $\frac{1}{5}$ is$\space $$ 0.01210121\dots$ in base $3$. The digits after the first $1$ are replaced by 0s to produce $0.01000000\dots$ This is not rewritten since there are no 2s. When read in base $2$, this corresponds to $\frac{1}{4}$, so $g(\frac{1}{5}) = \frac{1}{4}$.
Now, we want an algorithm for any given $x \in (0, 1)$ to put out the result $g(x)$. The crux of this question is to find $(a_1, \ldots, a_N)$ for a given $x \in (0, 1)$. In this case, if we know that $N = \infty$, we need an approximation, or we need to find $N$ exact. Could anyone can give me a hint? Thanks so much!
Your use of "algorithm" terminology is not really appropriate, because defining the base 3 expansion of $x$ and defining $N$ are both infinite processes. There is no finite process, no "algorithm", for determining that information. Real numbers do not lend themselves to algorithmic computation.
But although there is no algorithm, there are mathematical formulas. There's nothing very enlightening about these formulas, because they are simple consequences of the definition of base 3 expansion. But here they are anyway.
First, there is an inductive formula for the base 3 expansion of any real number $x \in (0,1)$: \begin{align*} a_1 &= \bigl\lfloor 3x \bigl\rfloor\\ a_2 &= \bigl\lfloor 3(3x-a_1) \bigr\rfloor = \bigl\lfloor 3^2 x - 3 a_1\bigr\rfloor \\ a_3 &= \bigl\lfloor 3^3 x - 3^2 a_1 - 3 a_2 \bigr\rfloor \\ ... \\ a_j &= \bigl\lfloor 3^j x - \sum_{i=1}^{j-1} (3^{j-i} a_i) \bigr\rfloor \\ ... \end{align*} Notice: these formulas require exact arithmetic, so even the very first formula $a_1 = \lfloor 3x \rfloor$ is not an "algorithm".
As for $N$ itself, there is nothing better than the formula you have already given:
This is an exact formula, although again it is not an "algorithm".
As for approximations, there is one interesting thing to be said, namely a kind of "uniform approximability" of the base 3 expansion: for every $\delta > 0$ there exists $N_0$ such that for every $N \ge N_0$, and for every $x \in (0,1)$, if you compute $a_1,...,a_N$ to be the first $N$ digits of the base 3 expansion then $$\bigl| x - \sum_{j=1}^N a_j 3^{-j} \bigr| < \delta $$ A standard theorem of analysis then says that the Cantor function is uniformly continuous (because it is defined and continuous on the closed interval $[0,1]$), and you can then combine this with uniform approximability of the base 3 expansion if you like.