[Tex/LaTex] How to draw array (data structure) and show partition

diagramstikz-pgf

I'm doing a homework for algorithm class where I have to show what a particular array looks like at each step of the quick sort algorithm, in a manner similar to this image: (page from CLRS book).

array image

I would like to know how to draw an array that looks like the one in the book. In particular, I want to know how to (in order from most to least important)

  • Draw an array.

  • Put letter(s) above a node or a blank space.

  • Draw the black bars that partition the array.

  • Customize a cell with gray or light gray color.

Best Answer

Something like this perhaps

enter image description here

\documentclass{article}

\usepackage{colortbl}
\makeatletter
\newcolumntype{W}{!{\smash{\vrule
\@width 4\arrayrulewidth
\@height\dimexpr\ht\@arstrutbox+2pt\relax
\@depth\dimexpr\dp\@arstrutbox+2pt\relax}}}
\makeatother
\definecolor{gray}{rgb}{.7,.7,.7}
\begin{document}

\begin{tabular}{|c|c|cWc|cWc|cWc|}
\multicolumn{1}{c}{$p$}&
\multicolumn{1}{c}{}&
\multicolumn{1}{c}{$i$}&
\multicolumn{1}{c}{}&
\multicolumn{1}{c}{}&
\multicolumn{1}{c}{$j$}&
\multicolumn{1}{c}{}&
\multicolumn{1}{c}{$r$}\\
\cellcolor{gray}2&
\cellcolor{gray}1&
\cellcolor{gray}3&
\cellcolor{black}&
\cellcolor{black}&
5&
6&
4
\end{tabular}

\end{document}
Related Question