How can i write the pseudo code Algorithm in Latex?

I would like to write pseudo code algorithm in Latex as in the attached picture.I have written code to make just like above algorithm but its not working for me. Is this code correct... \IncMargin{1em}
\begin{algorithm}
\SetKwInOut{Part 1}{input}
\SetKwInOut{Part 2}{output}
\Part 1{}
\BlankLine
\emph{abcdefgh $D$ ijklmnop}\;
\emph{ abcdefgh $A$}\;
\For{$k\leftarrow 1$ \KwTo $M$}{
\For {each $m$ $∈$ {1, 2, …, J}{
\emph{ abcdefgh $D$}\;
\emph{abcdef }\;
}
\Part 2{}
\emph{abcdefgh $A$ ijklmnop}\;
\emph{ abcdefgh $A$}\;
\For {each $m$ $∈$ {1, 2, …, J}{
\emph{ abcdefgh $D$}\;
\emph{abcdef }\;
\emph{abcdef }\;
}
}
\For{$k\leftarrow 1$ \KwTo $M$}{
\emph{ abcdefghklmop}\;
\emph{ abcdefghklmop}\;
\emph{ abcdefghklmop}\;
}
\caption { Algorithm Sample}
\end{algorithm}
\DecMargin{1em}

The following is more in line with the regular syntax of algorithm2e: \documentclass{article}

\usepackage{amsmath}
\usepackage[ruled,vlined]{algorithm2e}
\DontPrintSemicolon
\newcommand{\To}{\mbox{\upshape\bfseries to}}

\begin{document}

\begin{algorithm}
\caption{Algorithm sample}
\textbf{Part 1}\;
\nl abcdefgh $\{ D_i \}_{i = 1}^M$ ijklmnop\;
\nl abcdefgh $A_m$\;
\nl \For{$K = 1$ \To{} $M$}{%
\ForEach{$m \in \{1, 2, \dots, J\}$}{
abcdefgh $\{ D \}_m^t$\;
abcdefgh\;
}
\textbf{Part 2}\;
abcdefgh $\{ G_i \}_{i = 1}^K$\;
abcdefgh $D_m$\;
\ForEach{$m \in \{1, 2, \dots, J\}$}{
abcdefgh $\{ G \}_m^y$\;
abcdefgh\;
abcdefgh\;
}
}
\nl \For{$K = 1$ \To{} $M$}{
abcdefghijklmno\;
abcdefghijklmno\;
abcdefghijklmno\;
}
\end{algorithm}

\end{document}