1) A slightly different MWE
AFAIU, it demonstrates the same sort of behavior you're talking about:
\documentclass[a5paper,12pt]{memoir}
\usepackage{graphicx, wrapfig, lipsum}
\begin{document}
\section{First section}
% \lipsum[1] % uncommenting this make image jump to after section 2
\begin{wrapfigure}{R}{0.4\linewidth}
\centering
\includegraphics[width=1\linewidth]{example-image-a}%
\caption{ This is example image A }
\end{wrapfigure}
\lipsum[1]
\section{Second section}
\lipsum[2]
\end{document}
Same in overleaf.
The result looks fine if those line is commented out:

But the image jumps to the very end if that line is uncommented:
Sometimes, this "jump" is triggered by adding just one character. And, personally, jumping that far is not the behavior I expect.
2) \WFclear
Donald Arseneau (wrapfig dev) suggested to use built-in undocumented \WFclear command for that.
\documentclass[a5paper,12pt]{memoir}
\usepackage{graphicx, wrapfig, lipsum}
\begin{document}
\section{First section}
\lipsum[1]
\begin{wrapfigure}{R}{0.4\linewidth}
\centering
\includegraphics[width=1\linewidth]{example-image-a}%
\caption{ This is example image A }
\end{wrapfigure}
\lipsum[2]
% \lipsum[3] % if uncommented, the image would be next-to-this paragraph
\WFclear
\section{Second section}
\lipsum[2]
\end{document}
Same in overleaf.
And at the first glance, this is all you need. It just puts the image separately from the text, if there's no suitable paragraph:

And uses the first suitable, if any:

However, there's still a caveat, associated with too short paragraphs:
\documentclass[a5paper,12pt]{memoir}
\usepackage{graphicx, wrapfig, lipsum}
\begin{document}
\section{First section}
\lipsum[1]
\begin{wrapfigure}{R}{0.4\linewidth}
\centering
\includegraphics[width=1\linewidth]{example-image-a}%
\caption{ This is example image A }
\end{wrapfigure}
\lipsum[2]
% \lipsum[3] % if uncommented, the image would be next-to-this paragraph
This is a very short paragraph.
\WFclear
\section{Second section}
\lipsum[2]
\end{document}
Same in overleaf.

3) \WFfill
The same Donald's post proposes a fix for this:
\documentclass[a5paper,12pt]{memoir}
\usepackage{graphicx, wrapfig, lipsum}
%%% Adds empty text lines next to the wrapfigure,
% if the paragraph is shorter than needed.
\makeatletter
\def\WFfill{\par
\ifx\parshape\WF@fudgeparshape
\nobreak
\ifnum\c@WF@wrappedlines>\@ne
\advance\c@WF@wrappedlines\m@ne
\vskip\c@WF@wrappedlines\baselineskip
\global\c@WF@wrappedlines\z@
\fi
\allowbreak
\WF@finale
\fi
}
\makeatother
\begin{document}
\section{First section}
\lipsum[1]
\begin{wrapfigure}{R}{0.4\linewidth}
\centering
\includegraphics[width=1\linewidth]{example-image-a}%
\caption{ This is example image A }
\end{wrapfigure}
\lipsum[2]
% \lipsum[3] % if uncommented, the image would be next-to-this paragraph (works OK)
This is a very short paragraph.
% \lipsum[3] % if uncommented, this paragraph would wrap the figure as well (works OK)
\WFfill
\WFclear
\section{Second section}
\lipsum[4]
\end{document}
Same in overleaf.
Now short paragraph case is not looking that bad:

And if the first-suitable, but too-short paragraph is followed by another one, it also perfectly works:

4) Put it inside \section
Just like FloatBarrier, these commands could be embedded in \section:
\documentclass[a5paper,12pt]{memoir}
\usepackage{graphicx, wrapfig, lipsum}
%% Adds empty text lines next to the wrapfigure, if the paragraph is shorter than needed.
\makeatletter
\def\WFfill{\par
\ifx\parshape\WF@fudgeparshape
\nobreak
\ifnum\c@WF@wrappedlines>\@ne
\advance\c@WF@wrappedlines\m@ne
\vskip\c@WF@wrappedlines\baselineskip
\global\c@WF@wrappedlines\z@
\fi
\allowbreak
\WF@finale
\fi
}
\makeatother
%% Modify `\section` definition to include all the barriers inside.
\usepackage{placeins}
\let\Oldsection\section
\renewcommand{\section}{\WFfill\WFclear\FloatBarrier\Oldsection}
\begin{document}
\section{First section}
\lipsum[1]
\begin{wrapfigure}{R}{0.4\linewidth}
\centering
\includegraphics[width=1\linewidth]{example-image-a}%
\caption{ This is example image A }
\end{wrapfigure}
\lipsum[2]
% \lipsum[3] % if uncommented, the image would be next-to-this paragraph (works OK)
This is a very short paragraph.
% \lipsum[3] % if uncommented, this paragraph would wrap the figure as well (works OK)
\section{Second section}
\lipsum[4]
\end{document}
Same in overleaf.
wraptablein connection with list environments (such ascenter). – egreg Jun 30 '16 at 23:32\FloatBarrieris irrelevant. I'm not sure about @egreg 's point. You can't use them when the surrounding stuff is a list. I don't know if that prohibits using a list within thewrapfigorwraptable. However,\centeringis probably a better option anyway. – cfr Jun 30 '16 at 23:58tablehere, – David Carlisle Sep 23 '17 at 20:52