# How to draw array (data structure) and show partition?

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).

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.

-
Could you show us what you've tried so far? –  Seamus Feb 14 '13 at 20:32
–  Peter Grill Feb 14 '13 at 20:35
@Seamus: Before I asked the question, I found the following question which shows me how to draw an array. Since I'm not familiar with using tikz, I'm unable to modify the code to include black bars, letter above a cell, or customized color (I can only modify the spacing). Note that typesetting this array is not necessary for the assignment, since I can draw it by hand. It just would be nice to know how to. –  Kiet Tran Feb 14 '13 at 20:53
You can change the output. Take a look here : tex.stackexchange.com/questions/83727/… . –  projetmbc Feb 14 '13 at 23:21

Something like this perhaps

\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}

-
Yes, thank you. Your code is straightforward and easy to understand, too. :) –  Kiet Tran Feb 15 '13 at 3:49
@DavidCarlisle: But, but... that's not TikZ! No fair! –  Jake Feb 15 '13 at 7:46