While revisiting some old notes on algorithms, I came across a code snippet illustrating the sorting of a vector using quicksort.
I’m dissatisfied with how the nodes are defined relative to each other, requiring some nodes to be repeated to maintain references. Additionally, I had to draw them in reverse order to ensure the edges overlapped correctly.
One potential optimization I considered is ensuring the "examining" styled node always displays \(i\) above it.
How can I optimize the code to avoid repetition?
% arara: pdflatex % arara: latexmk: { clean: partial } \documentclass[tikz, border=.2cm]{standalone} \usetikzlibrary{positioning} \tikzset{ vector/.style = { draw = black, rectangle, align = center, anchor = west, fill = yellow!40, font = \ttfamily\bfseries, minimum height = 0.7cm, minimum size = 0.7cm }, label/.style = { node distance = 0pt, font = \scriptsize, }, % cells styles undiscovered/.style = { fill = teal!20, draw = teal, dashed, }, examining/.style = { very thick, fill=teal!50, }, pivot/.style = { fill = purple!60, }, smallerPivot/.style = { fill = red!20, }, greaterPivot/.style = { fill = gray!20, draw = gray!80, dashed, }, } \begin{document} \begin{tikzpicture} \node[vector, undiscovered] (11) {13}; \node[vector, left=-1pt of 11, undiscovered] (10) {25}; \node[vector, left=-1pt of 10, undiscovered] (9) {21}; \node[vector, left=-1pt of 9, undiscovered] (8) {30}; \node[vector, left=-1pt of 8, undiscovered] (7) {12}; \node[vector, left=-1pt of 7, undiscovered] (6) {27}; \node[vector, left=-1pt of 6, undiscovered] (5) {15}; \node[vector, left=-1pt of 5, undiscovered] (4) {29}; \node[vector, left=-1pt of 4, undiscovered] (3) {28}; \node[vector, left=-1pt of 3, examining] (2) {14}; \node[vector, left=-1pt of 2, pivot] (1) {20}; % NOTE I have to repeat it otherwise I don't have the reference to node 2 for node 1 \node[vector, right=-1pt of 1, examining] (2) {14}; \node[label, above=of 2] (index-i) {\(i\)}; \node[label, below=of 1] (index-j) {\(j\)}; \end{tikzpicture} \begin{tikzpicture} \node[vector, undiscovered] (11) {13}; \node[vector, left=-1pt of 11, undiscovered] (10) {25}; \node[vector, left=-1pt of 10, undiscovered] (9) {21}; \node[vector, left=-1pt of 9, undiscovered] (8) {30}; \node[vector, left=-1pt of 8, undiscovered] (7) {12}; \node[vector, left=-1pt of 7, undiscovered] (6) {27}; \node[vector, left=-1pt of 6, undiscovered] (5) {15}; \node[vector, left=-1pt of 5, undiscovered] (4) {29}; \node[vector, left=-1pt of 3, smallerPivot] (2) {14}; \node[vector, right=-1pt of 2, examining] (3) {28}; \node[vector, left=-1pt of 2, pivot] (1) {20}; \node[label, above=of 3] (index-i) {\(i\)}; \node[label, below=of 2] (index-j) {\(j\)}; \end{tikzpicture} \begin{tikzpicture} \node[vector, undiscovered] (11) {13}; \node[vector, left=-1pt of 11, undiscovered] (10) {25}; \node[vector, left=-1pt of 10, undiscovered] (9) {21}; \node[vector, left=-1pt of 9, undiscovered] (8) {30}; \node[vector, left=-1pt of 8, undiscovered] (7) {12}; \node[vector, left=-1pt of 7, undiscovered] (6) {27}; \node[vector, left=-1pt of 6, undiscovered] (5) {15}; \node[vector, left=-1pt of 4, greaterPivot] (3) {28}; \node[vector, right=-1pt of 3, examining] (4) {29}; \node[vector, left=-1pt of 3, smallerPivot] (2) {14}; \node[vector, left=-1pt of 2, pivot] (1) {20}; \node[label, above=of 4] (index-i) {\(i\)}; \node[label, below=of 2] (index-j) {\(j\)}; \end{tikzpicture} \begin{tikzpicture} \node[vector, undiscovered] (11) {13}; \node[vector, left=-1pt of 11, undiscovered] (10) {25}; \node[vector, left=-1pt of 10, undiscovered] (9) {21}; \node[vector, left=-1pt of 9, undiscovered] (8) {30}; \node[vector, left=-1pt of 8, undiscovered] (7) {12}; \node[vector, left=-1pt of 7, undiscovered] (6) {27}; \node[vector, left=-1pt of 5, greaterPivot] (4) {29}; \node[vector, right=-1pt of 4, examining] (5) {15}; \node[vector, left=-1pt of 4, greaterPivot] (3) {28}; \node[vector, left=-1pt of 3, smallerPivot] (2) {14}; \node[vector, left=-1pt of 2, pivot] (1) {20}; \node[label, above=of 5] (index-i) {\(i\)}; \node[label, below=of 2] (index-j) {\(j\)}; \end{tikzpicture} \begin{tikzpicture} \node[vector, undiscovered] (11) {13}; \node[vector, left=-1pt of 11, undiscovered] (10) {25}; \node[vector, left=-1pt of 10, undiscovered] (9) {21}; \node[vector, left=-1pt of 9, undiscovered] (8) {30}; \node[vector, left=-1pt of 8, undiscovered] (7) {12}; \node[vector, left=-1pt of 6, greaterPivot] (5) {28}; \node[vector, right=-1pt of 5, examining] (6) {27}; \node[vector, left=-1pt of 5, greaterPivot] (4) {29}; \node[vector, left=-1pt of 4, smallerPivot] (3) {15}; \node[vector, left=-1pt of 3, smallerPivot] (2) {14}; \node[vector, left=-1pt of 2, pivot] (1) {20}; \node[label, above=of 6] (index-i) {\(i\)}; \node[label, below=of 3] (index-j) {\(j\)}; \end{tikzpicture} \begin{tikzpicture} \node[vector, undiscovered] (11) {13}; \node[vector, left=-1pt of 11, undiscovered] (10) {25}; \node[vector, left=-1pt of 10, undiscovered] (9) {21}; \node[vector, left=-1pt of 9, undiscovered] (8) {30}; \node[vector, left=-1pt of 7, greaterPivot] (6) {27}; \node[vector, right=-1pt of 6, examining] (7) {12}; \node[vector, left=-1pt of 6, greaterPivot] (5) {28}; \node[vector, left=-1pt of 5, greaterPivot] (4) {29}; \node[vector, left=-1pt of 4, smallerPivot] (3) {15}; \node[vector, left=-1pt of 3, smallerPivot] (2) {14}; \node[vector, left=-1pt of 2, pivot] (1) {20}; \node[label, above=of 7] (index-i) {\(i\)}; \node[label, below=of 3] (index-j) {\(j\)}; \end{tikzpicture} \begin{tikzpicture} \node[vector, undiscovered] (11) {13}; \node[vector, left=-1pt of 11, undiscovered] (10) {25}; \node[vector, left=-1pt of 10, undiscovered] (9) {21}; \node[vector, left=-1pt of 8, greaterPivot] (7) {29}; \node[vector, right=-1pt of 7, examining] (8) {30}; \node[vector, left=-1pt of 7, greaterPivot] (6) {27}; \node[vector, left=-1pt of 6, greaterPivot] (5) {28}; \node[vector, left=-1pt of 5, smallerPivot] (4) {12}; \node[vector, left=-1pt of 4, smallerPivot] (3) {15}; \node[vector, left=-1pt of 3, smallerPivot] (2) {14}; \node[vector, left=-1pt of 2, pivot] (1) {20}; \node[label, above=of 8] (index-i) {\(i\)}; \node[label, below=of 4] (index-j) {\(j\)}; \end{tikzpicture} \begin{tikzpicture} \node[vector, undiscovered] (11) {13}; \node[vector, left=-1pt of 11, undiscovered] (10) {25}; \node[vector, left=-1pt of 9, greaterPivot] (8) {30}; \node[vector, right=-1pt of 8, examining] (9) {21}; \node[vector, left=-1pt of 8, greaterPivot] (7) {29}; \node[vector, left=-1pt of 7, greaterPivot] (6) {27}; \node[vector, left=-1pt of 6, greaterPivot] (5) {28}; \node[vector, left=-1pt of 5, smallerPivot] (4) {12}; \node[vector, left=-1pt of 4, smallerPivot] (3) {15}; \node[vector, left=-1pt of 3, smallerPivot] (2) {14}; \node[vector, left=-1pt of 2, pivot] (1) {20}; \node[label, above=of 9] (index-i) {\(i\)}; \node[label, below=of 4] (index-j) {\(j\)}; \end{tikzpicture} \begin{tikzpicture} \node[vector, undiscovered] (11) {13}; \node[vector, left=-1pt of 10, greaterPivot] (9) {21}; \node[vector, right=-1pt of 9, examining] (10) {25}; \node[vector, left=-1pt of 9, greaterPivot] (8) {30}; \node[vector, left=-1pt of 8, greaterPivot] (7) {29}; \node[vector, left=-1pt of 7, greaterPivot] (6) {27}; \node[vector, left=-1pt of 6, greaterPivot] (5) {28}; \node[vector, left=-1pt of 5, smallerPivot] (4) {12}; \node[vector, left=-1pt of 4, smallerPivot] (3) {15}; \node[vector, left=-1pt of 3, smallerPivot] (2) {14}; \node[vector, left=-1pt of 2, pivot] (1) {20}; \node[label, above=of 10] (index-i) {\(i\)}; \node[label, below=of 4] (index-j) {\(j\)}; \end{tikzpicture} \begin{tikzpicture} \node[vector, greaterPivot] (10) {25}; \node[vector, right=-1pt of 10, examining] (11) {13}; \node[vector, left=-1pt of 10, greaterPivot] (9) {21}; \node[vector, left=-1pt of 9, greaterPivot] (8) {30}; \node[vector, left=-1pt of 8, greaterPivot] (7) {29}; \node[vector, left=-1pt of 7, greaterPivot] (6) {27}; \node[vector, left=-1pt of 6, greaterPivot] (5) {28}; \node[vector, left=-1pt of 5, smallerPivot] (4) {12}; \node[vector, left=-1pt of 4, smallerPivot] (3) {15}; \node[vector, left=-1pt of 3, smallerPivot] (2) {14}; \node[vector, left=-1pt of 2, pivot] (1) {20}; \node[label, above=of 11] (index-i) {\(i\)}; \node[label, below=of 4] (index-j) {\(j\)}; \end{tikzpicture} \begin{tikzpicture} \node[vector, greaterPivot] (11) {28}; \node[vector, left=-1pt of 11, greaterPivot] (10) {25}; \node[vector, left=-1pt of 10, greaterPivot] (9) {21}; \node[vector, left=-1pt of 9, greaterPivot] (8) {30}; \node[vector, left=-1pt of 8, greaterPivot] (7) {29}; \node[vector, left=-1pt of 7, greaterPivot] (6) {27}; \node[vector, left=-1pt of 6, smallerPivot] (5) {13}; \node[vector, left=-1pt of 5, smallerPivot] (4) {12}; \node[vector, left=-1pt of 4, smallerPivot] (3) {15}; \node[vector, left=-1pt of 3, smallerPivot] (2) {14}; \node[vector, left=-1pt of 2, pivot] (1) {20}; \node[label, above=of 11] (index-i) {\phantom{\(i\)}}; \node[label, below=of 5] (index-j) {\(j\)}; \end{tikzpicture} \begin{tikzpicture} \node[vector, greaterPivot] (11) {28}; \node[vector, left=-1pt of 11, greaterPivot] (10) {25}; \node[vector, left=-1pt of 10, greaterPivot] (9) {21}; \node[vector, left=-1pt of 9, greaterPivot] (8) {30}; \node[vector, left=-1pt of 8, greaterPivot] (7) {29}; \node[vector, left=-1pt of 7, greaterPivot] (6) {27}; \node[vector, left=-1pt of 6, pivot] (5) {20}; \node[vector, left=-1pt of 5, smallerPivot] (4) {12}; \node[vector, left=-1pt of 4, smallerPivot] (3) {15}; \node[vector, left=-1pt of 3, smallerPivot] (2) {14}; \node[vector, left=-1pt of 2, smallerPivot] (1) {13}; \node[label, above=of 11] (index-i) {\phantom{\(i\)}}; \node[label, below=of 5] (index-j) {\(j\)}; \end{tikzpicture} \end{document} 

overlay-beamer-stylestikz library to highlight different nodes on different overlays. This way you would only need one copy of the code. (if you need the result in another class, you could then include it as image)