\documentclass{beamer} %include lhs2TeX.fmt \author{Piyush P Kurur\\ Office no: 224\\ Dept. of Comp. Sci. and Engg.\\ IIT Kanpur} \newcommand{\Token}[2][]{\node(#2#1)[token]{#2};} \newcommand{\Symbol}[2][]{\node(#2#1)[symbol]{#2};} \newcommand{\Point}[1]{\node(#1)[point]{#1};} \usepackage{tikz} \usetikzlibrary{positioning,shapes,chains,fit} \usetikzlibrary{shapes.symbols} \usetikzlibrary{matrix} \usetikzlibrary{backgrounds} \usetikzlibrary{shapes.geometric} \usepackage{multicol} \usepackage{algorithm2e} \usepackage{pgfkeys} \pgfkeys {% /tikz/redbox/.style={% shape=rectangle,% minimum size=1.5em,% top color = white, % bottom color=red!50!black!50, inner sep=0pt, draw=black }, /tikz/greybox/.style={ shape=rectangle,% minimum width=7em,% minimum height=1.5em, fill=black!20, inner sep=0pt, draw=black }, /tikz/whitebox/.style={ shape=rectangle, minimum width=5em, minimum height=1.5em, draw=black }, /tikz/whitetape/.style={ shape=rectangle, minimum width=4em, minimum height=1.5em, draw=black }, /tikz/bluearr/.style={ ->, line width=0.25em, draw=blue!40 } } \newcommand{\arraythree}[4]{ \begin{tikzpicture}[node distance=0] \pgfsetstrokeopacity{#4}; \node[#1](a0#3){#2}; \node[#1, right=of a0#3](a1#3){#2}; \node[#1, right=of a1#3](a1#3){#2}; \end{tikzpicture} } \newcommand{\arraytwo}[4]{ \begin{tikzpicture}[node distance=0] \pgfsetstrokeopacity{#4}; \node[#1](a0#3){#2}; \node[#1,right=of a0#3]{#2}; \end{tikzpicture} } \newcommand{\Array}[0]{ \arraythree{box}{}{a}{1} } \newcommand{\AArray}[0]{% \arraythree{bbox}{\Array}{b}{0.5}} \newcommand{\AAArray}[0]{% \arraytwo{bbbox}{\AArray}{c}{0.25}} \title{Fundamentals of Computing: Lecture 20} \date{September 14, 2009} \begin{document} \begin{frame} \maketitle \end{frame} \begin{frame} \frametitle{Quick sort} \begin{itemize} \item Choose a pivot element $x$ \item Divide the rest of elements into two groups $A$ and $B$ such that \begin{itemize} \item A consists of all elements less than $x$. \item B consists of all elements greater than or equal to $x$. \end{itemize} \item Sort $A$ and $B$ recursively and then the arange them in the order $A$, $x$, $B$. \end{itemize} \end{frame} \begin{frame} \frametitle{The function |partition|} \begin{code} int partition(int a[], int start, int end, int pivotIndex); \end{code} \begin{itemize} \item Takes as input an array slice $\{ \texttt{a[start]},\ldots, \texttt{a[end - 1]} \}$, \item Chooses |pivot = a[pivotIndex]| as the pivot element and, \item Rearranges the elements and returns |seperator| such that \begin{itemize} \item The slice $\{ \texttt{a[start]}, \texttt{\ldots a[sperator-1]} \}$ contains elements less than or equal to |pivot| and, \item $\{ \texttt{a[seperator]}, \ldots, \texttt{a[end - 1]} \}$ contains elements greater than equal to |pivot|. \end{itemize} \end{itemize} \begin{tikzpicture} \begin{scope} [node distance=0] \node[whitetape](start){$\ldots$}; \node[greybox, right=of start,minimum width=10.25em] (lessthan){}; \node[greybox, right=of lessthan, minimum size=1.5em ](seperator){}; \node[greybox, right=of seperator, minimum width=10.25em]% (greaterthan){}; \node[whitetape, right =of greaterthan](end){$\ldots$}; \end{scope} \node [above =of seperator](sep){separator}; \node [above =of {start.north east}](st){$\texttt{start}$}; \node [above =of {end.north west}](ed){$\texttt{end}$}; \draw[->, thick] (st) -- (start.north east)% node [midway] (ltstart){}; \draw (seperator.north west) -- (seperator.north west ||- sep.south) node [midway](ltend){}; \draw (ed) -- (end.north west)% node[midway] (gtend){}; \draw[->, thick] (sep) -- (seperator); \draw[->, thick] (ed) -- (end.north west); %%%%%%%%%%%%%%%%% decorations \draw[<->] (ltstart) -- (ltend) node [midway, fill=white] {$\leq \texttt{pivot}$}; \draw[<->] (ltend) -- (gtend) node [midway, fill=white] {$\geq \texttt{pivot}$}; \end{tikzpicture} \end{frame} \begin{frame} \frametitle{The Quick sort algorithm} \begin{spec} void qsort(int a[], int start, int end); \end{spec} \begin{itemize} \item Takes as input the array slice $\{ \texttt{a[start]},\ldots, \texttt{a[end -1]}\}$. \item Rearranges the input in sorted order. \end{itemize} \begin{code} void qsort(int *a , int start, int end) { int seperator; if( start >= end - 1) return; seperator = partition(a, start, end, start); qsort(a, start, seperator); qsort(a, seperator , end); } \end{code} \end{frame} \begin{frame} \frametitle{Designing the function |partition|} \begin{tikzpicture} \begin{scope} [node distance=0] \node[whitetape](start){$\ldots$}; \node[greybox, right=of start, % label=above:$\leq \texttt{pivot}$,minimum width=10.25em] (lessthan){}; \node[greybox, right=of lessthan, minimum size=1.5em ](seperator){}; \node[greybox, right=of seperator, minimum width=10.25em, label=above:$\geq \texttt{pivot}$ ] (greaterthan){}; \node[whitetape, right =of greaterthan](end){$\ldots$}; \end{scope} \node [above =of seperator](sep){separator}; \node [above =of {start.north east}](st){$\texttt{start}$}; \node [above =of {end.north west}](ed){$\texttt{end}$}; \draw[->, thick] (sep) -- (seperator); \draw[->, thick] (st) -- (start.north east); \draw[->, thick] (ed) -- (end.north west); \end{tikzpicture} \pause \begin{block}{The invariant} \begin{tikzpicture} \begin{scope} [node distance=0] \node[whitetape](start){$\ldots$}; \node[greybox, right=of start,% label=above:$\leq \texttt{pivot}$ ](lessthan){}; \node[redbox, right=of lessthan](l){}; \node[whitebox, right=of l](ldots){$\ldots \ldots$}; \node[redbox, right=of ldots](m){}; \node[greybox, right=of m, % label=above:$\geq \texttt{pivot}$ ](greaterthan){}; \node[whitetape, right =of greaterthan](end){$\ldots$}; \end{scope} \node[above =of l] (lp) {$\texttt{l}$}; \node[above =of m](mp) {$\texttt{m}$}; \draw[->, thick] (lp) -- (l.north); \draw[->, thick] (mp) -- (m.north); \end{tikzpicture} \end{block} \end{frame} \begin{frame} \begin{block}{The invariant} \begin{itemize} \item Elements |a[start]| to |a[l-1]| are less than or equal to |pivot| and \item Elements |a[m+1]| to |a[end - 1]| are greater than or equal to |pivot|. \end{itemize} \end{block} \begin{tikzpicture} \begin{scope} [node distance=0] \node[whitetape](start){$\ldots$}; \node[greybox, right=of start, label=above:$\leq \texttt{pivot}$ ](lessthan){}; \node[redbox, right=of lessthan](l){}; \node[whitebox, right=of l](ldots){$\ldots \ldots$}; \node[redbox, right=of ldots](m){}; \node[greybox, right=of m, label=above:$\geq \texttt{pivot}$ ](greaterthan){}; \node[whitetape, right =of greaterthan](end){$\ldots$}; \end{scope} \node[above =of l] (lp) {$\texttt{l}$}; \node[above =of m](mp) {$\texttt{m}$}; \draw[->, thick] (lp) -- (l.north); \draw[->, thick] (mp) -- (m.north); \end{tikzpicture} \pause \begin{block}{To begin with} \begin{tikzpicture} \begin{scope} [node distance=0] \node[whitetape](start){$\ldots$}; \node[redbox, right=of start](l){}; \node[whitebox, right=of l, minimum width=19em](ldots){$\ldots \ldots$}; \node[redbox, right=of ldots](m){}; \node[whitetape, right =of m](end){$\ldots$}; \end{scope} \node[above =of l] (lp) {$\texttt{l}$}; \node[above =of m](mp) {$\texttt{m}$}; \draw[->, thick] (lp) -- (l.north); \draw[->, thick] (mp) -- (m.north); \end{tikzpicture} \end{block} \end{frame} \begin{frame} \begin{block}{If |a[l] <= pivot|} \begin{tikzpicture} \begin{scope} [node distance=0] \node[whitetape](start){$\ldots$}; \node[greybox, right=of start, % label=above:$\leq \texttt{pivot}$ ](lessthan){}; \node[redbox, right=of lessthan](l){}; \node[whitebox, right=of l](ldots){$\ldots \ldots$}; \node[redbox, right=of ldots](m){}; \node[greybox, right=of m,% label=above:$\geq \texttt{pivot}$ ](greaterthan){}; \node[whitetape, right =of greaterthan](end){$\ldots$}; \end{scope} \node[above =of l] (lp) {$\texttt{l}$}; \node[above =of m](mp) {$\texttt{m}$}; \draw[->, thick] (lp) -- (l.north); \draw[->, thick] (mp) -- (m.north); \uncover<2->{ \draw[bluearr, draw=green!50!black!50](lp.east) -- +(1.5em,0); } \end{tikzpicture} \end{block} \begin{block}{\uncover<3->{Similarly if |a[m] >= pivot|}} \begin{tikzpicture} \uncover<3->{ \begin{scope} [node distance=0] \node[whitetape](start){$\ldots$}; \node[greybox, right=of start,% label=above:$\leq \texttt{pivot}$ ](lessthan){}; \node[redbox, right=of lessthan](l){}; \node[whitebox, right=of l](ldots){$\ldots \ldots$}; \node[redbox, right=of ldots](m){}; \node[greybox, right=of m, % label=above:$\geq \texttt{pivot}$ ](greaterthan){}; \node[whitetape, right =of greaterthan](end){$\ldots$}; \end{scope} \node[above =of l] (lp) {$\texttt{l}$}; \node[above =of m](mp) {$\texttt{m}$}; \draw[->, thick] (lp) -- (l.north); \draw[->, thick] (mp) -- (m.north); } \uncover<4->{ \draw[bluearr, draw=green!50!black!50] (mp.west) -- +(-1.5em,0); } \end{tikzpicture} \end{block} \end{frame} \begin{frame} \frametitle{What when |a[l] > pivot| and |a[m] < pivot|} \begin{tikzpicture} \begin{scope} [node distance=0] \node[whitetape](start){$\ldots$}; \node[greybox, right=of start, label=above:$\leq \texttt{pivot}$ ](lessthan){}; \node[redbox, right=of lessthan](l){}; \node[whitebox, right=of l](ldots){$\ldots \ldots$}; \node[redbox, right=of ldots](m){}; \node[greybox, right=of m, label=above:$\geq \texttt{pivot}$ ](greaterthan){}; \node[whitetape, right =of greaterthan](end){$\ldots$}; \end{scope} \node[above =of l] (lp) {$\texttt{l}$}; \node[above =of m](mp) {$\texttt{m}$}; \draw[->, thick] (lp) -- (l.north); \draw[->, thick] (mp) -- (m.north); \uncover<2->{ \draw[bluearr] (l.north east) to [out=45, in=135] (m.north west); \draw[bluearr] (m.south west) to [out=225, in=-45] (l.south east); } \end{tikzpicture} \begin{tikzpicture} \uncover<3->{ \begin{scope} [node distance=0] \node[whitetape](start){$\ldots$}; \node[greybox, right=of start,% label=above:$\leq \texttt{pivot}$ ](lessthan){}; \node[redbox, right=of lessthan](l){}; \node[whitebox, right=of l](ldots){$\ldots \ldots$}; \node[redbox, right=of ldots](m){}; \node[greybox, right=of m, % label=above:$\geq \texttt{pivot}$ ](greaterthan){}; \node[whitetape, right =of greaterthan](end){$\ldots$}; \end{scope} \node[above =of l] (lp) {$\texttt{l}$}; \node[above =of m](mp) {$\texttt{m}$}; \draw[->, thick] (lp) -- (l.north); \draw[->, thick] (mp) -- (m.north); } \uncover<4->{ \draw[bluearr, draw=green!50!black!50] (mp.west) -- +(-1.5em,0); \draw[bluearr, draw=green!50!black!50](lp.east) -- +(1.5em,0); } \end{tikzpicture} \end{frame} \begin{frame} \frametitle{The function |partition| contd} \begin{code} int partition(int *a, int start, int end, int pivotIndex) { int l = start; int m = end - 1; int pivot = a[pivotIndex]; while(l < m ) { if( a[l] <= pivot ) {l++; continue;} if( a[m] >= pivot ) {m--; continue;} /* Here a[l] > pivot && a[m] < pivot */ swap(a, l, m); } return l; } \end{code} \end{frame} \end{document}