Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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.

share|improve this question
3  
Could you show us what you've tried so far? –  Seamus Feb 14 '13 at 20:32
1  
Welcome to TeX.SE. –  Peter Grill Feb 14 '13 at 20:35
1  
@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
add comment

1 Answer

up vote 10 down vote accepted

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}
share|improve this answer
    
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
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.