Place algorithm environment directly under sections/subsections

algorithm2ealgorithmicalgorithmicxalgorithms

Algorithm environment is placed wrong, not in the place I want it to be (directly under subsection statement). I am aware of popular answers about the problem like using float package and [H] parameter just like in case of graphics, but I tried and it fails yet again, Algorithm 2 goes before subsection 2.2 but in code it should go after

PS: I have float package included right now

Preambula:

\documentclass[12pt,letterpaper,oneside,reqno]{amsart}
\usepackage{amsfonts}
\usepackage{amsmath}
\usepackage{amssymb}
\usepackage{amsthm}
\usepackage{float}
\usepackage{mathrsfs}
\usepackage{colonequals}
\usepackage[font=small,labelfont=bf]{caption}
\usepackage[left=1in,right=1in,bottom=1in,top=1in]{geometry}
\usepackage[pdfpagelabels,hyperindex,colorlinks=true,linkcolor=blue,urlcolor=magenta,citecolor=green]{hyperref}
\usepackage{setspace}
\usepackage{csquotes}
\usepackage{graphicx}
\usepackage{algorithm}
\usepackage{algpseudocode}
\onehalfspacing
\emergencystretch=1em

\newtheorem{thm}{Theorem}[section]
\newtheorem{cor}[thm]{Corollary}
\newtheorem{lem}[thm]{Lemma}
\newtheorem{examp}[thm]{Example}
\newtheorem{definition}[thm]{Definition}

\numberwithin{equation}{section}

Latex code:

\subsection{Building solution space tree}
Generation of abstract solution space tree is quit simple using Breadth-first approach as following pseudocode shows
\input{pseudocodes/build_solution_tree_pseudocode.tex}
\subsection{Finding terminal nodes of solution space tree}
\input{pseudocodes/create_set_of_terminal_nodes_pseudocode.tex}
\subsection{Finding solution candidates}
\subsection{Finding globally optimal solution}
\begin{algorithm}[H]
    \caption{Build solution space tree.}
    \begin{algorithmic}[1]
        \State \textbf{Inputs:} Set of variables $X$, initial constraint value $c\in\mathbb{Z}^n$.
        \State \textbf{Outputs:} Set of nodes $L$.
        \State Create queue $Q$
        \State Create list $L$
        \State $InitialVariableSet \leftarrow \{x_i \; | \; x_i \in X \; \wedge \; x_i \leq c\}$
        \For {each variable $x$ in $InitialVariableSet$}
            \State $constraint \leftarrow  c - x$
            \State $node  \leftarrow  Node(x, constraint, NULL)$
            \State $Q \leftarrow node$
            \State $L \leftarrow node$
        \EndFor
        \While{queue $Q$ in not empty}
            \State $currentNode \leftarrow \mathrm{dequeue \; from \; Q}$
            \State $currentConstraint \leftarrow \mathrm{constraint \; value \; of \; the \; currentNode}$
            \State $set \leftarrow \{x_i \; | \; x_i \in X \; \wedge \; x_i \leq currentConstraint\}$
            \For {each variable $s$ in $set$}
                \State $constraint \leftarrow  currentConstraint - x$
                \State $node  \leftarrow  Node(x, constraint, currentNode)$
                \State $currentNode.ChildNodes \leftarrow node$
                \State $Q \leftarrow node$
            \EndFor
        \EndWhile
    \end{algorithmic}
\end{algorithm}
\begin{algorithm}[H]
    \caption{Generate set of Terminal nodes.}
    \begin{algorithmic}[1]
        \State \textbf{Inputs:} Set of nodes $L$.
        \State \textbf{Outputs:} Set of terminal nodes $T$.
        \State Create queue $Q$
        \State Create list $T$
        \For {each node $x$ in $L$}
            \State $Q \leftarrow x$
            \While{queue $Q$ in not empty}
                \State $currentNode \leftarrow \mathrm{dequeue \; from \; Q}$
                \If {$currentNode \; \mathrm{is \; terminal \; node}$}
                    \State $T \leftarrow currentNode$
                    \State $continue$
                \EndIf
                \For {each node $s$ in $currentNode$ child nodes}
                    \State $Q \leftarrow s$
                \EndFor
            \EndWhile
        \EndFor
    \end{algorithmic}
\end{algorithm}

Output image:

enter image description here

Best Answer

Add \usepackage{placeins} to your preamble and change your second algorithm as follows:

\begin{algorithm}[ht!] % do not use H <<<<
    \caption{Generate set of Terminal nodes.}
    \begin{algorithmic}[1]
        \State \textbf{Inputs:} Set of nodes $L$.
        \State \textbf{Outputs:} Set of terminal nodes $T$.
        \State Create queue $Q$
        \State Create list $T$
        \For {each node $x$ in $L$}
        \State $Q \leftarrow x$
        \While{queue $Q$ in not empty}
        \State $currentNode \leftarrow \mathrm{dequeue \; from \; Q}$
        \If {$currentNode \; \mathrm{is \; terminal \; node}$}
        \State $T \leftarrow currentNode$
        \State $continue$
        \EndIf
        \For {each node $s$ in $currentNode$ child nodes}
        \State $Q \leftarrow s$
        \EndFor
        \EndWhile
        \EndFor
    \end{algorithmic}
\end{algorithm}
 \FloatBarrier% add to the end<<<<<<<<<<<

a

The problem lies in the way that amsart defines subsection. See material swapped with \subsection in AMS document classes

As you can see in the first subsection now \subsection creates a run-in header.

a

Option 2 Redefine subsection. Adding to your preamble

  \makeatletter
  \renewcommand\subsection{\@startsection{subsection}{2}{\z@}%
        {.5\linespacing\@plus.7\linespacing}
        {0.5ex \@plus .2ex}%
    {\normalfont\bfseries}}
  \makeatother

you will get

c

and in the next page

d