\documentclass{beamer}
%
%
\makeatletter
\@ifundefined{lhs2tex.lhs2tex.sty.read}%
  {\@namedef{lhs2tex.lhs2tex.sty.read}{}%
   \newcommand\SkipToFmtEnd{}%
   \newcommand\EndFmtInput{}%
   \long\def\SkipToFmtEnd#1\EndFmtInput{}%
  }\SkipToFmtEnd

\newcommand\ReadOnlyOnce[1]{\@ifundefined{#1}{\@namedef{#1}{}}\SkipToFmtEnd}
\usepackage{amstext}
\usepackage{amssymb}
\usepackage{stmaryrd}
\DeclareFontFamily{OT1}{cmtex}{}
\DeclareFontShape{OT1}{cmtex}{m}{n}
  {<5><6><7><8>cmtex8
   <9>cmtex9
   <10><10.95><12><14.4><17.28><20.74><24.88>cmtex10}{}
\DeclareFontShape{OT1}{cmtex}{m}{it}
  {<-> ssub * cmtt/m/it}{}
\newcommand{\texfamily}{\fontfamily{cmtex}\selectfont}
\DeclareFontShape{OT1}{cmtt}{bx}{n}
  {<5><6><7><8>cmtt8
   <9>cmbtt9
   <10><10.95><12><14.4><17.28><20.74><24.88>cmbtt10}{}
\DeclareFontShape{OT1}{cmtex}{bx}{n}
  {<-> ssub * cmtt/bx/n}{}
\newcommand{\tex}[1]{\text{\texfamily#1}}	% NEU

\newcommand{\Sp}{\hskip.33334em\relax}
\newcommand{\NB}{\textbf{NB}}
\newcommand{\Todo}[1]{$\langle$\textbf{To do:}~#1$\rangle$}

\EndFmtInput
\makeatother
%
\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 \text{\tt partition}}
  \begin{tabbing}\tt
~int~partition\char40{}int~a\char91{}\char93{}\char44{}~int~start\char44{}~int~end\char44{}~int~pivotIndex\char41{}\char59{}
\end{tabbing}

  \begin{itemize}
  \item Takes as input an array slice $\{ \texttt{a[start]},\ldots,
    \texttt{a[end - 1]} \}$,
  \item Chooses \text{\tt pivot~\char61{}~a\char91{}pivotIndex\char93{}} as the pivot element and,
  \item Rearranges the elements and returns \text{\tt seperator} such that
    \begin{itemize}
    \item The slice $\{ \texttt{a[start]}, \texttt{\ldots a[sperator-1]}
      \}$ contains elements less than or equal to \text{\tt pivot} and,
    \item $\{ \texttt{a[seperator]}, \ldots, \texttt{a[end - 1]} \}$
      contains elements greater than equal to \text{\tt 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{tabbing}\tt
~void~qsort\char40{}int~a\char91{}\char93{}\char44{}~int~start\char44{}~int~end\char41{}\char59{}
\end{tabbing}
  \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{tabbing}\tt
~void~qsort\char40{}int~\char42{}a~\char44{}~int~start\char44{}~int~end\char41{}\\
\tt ~\char123{}\\
\tt ~~~int~seperator\char59{}\\
\tt ~~~if\char40{}~start~\char62{}\char61{}~end~\char45{}~1\char41{}~return\char59{}\\
\tt ~~~\\
\tt ~~~seperator~\char61{}~partition\char40{}a\char44{}~start\char44{}~end\char44{}~start\char41{}\char59{}\\
\tt ~~~\\
\tt ~~~qsort\char40{}a\char44{}~start\char44{}~seperator\char41{}\char59{}\\
\tt ~~~qsort\char40{}a\char44{}~seperator~\char44{}~end\char41{}\char59{}\\
\tt ~\char125{}
\end{tabbing}
\end{frame}




\begin{frame}
  \frametitle{Designing the function \text{\tt 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 \text{\tt a\char91{}start\char93{}} to \text{\tt a\char91{}l\char45{}1\char93{}} are less than or equal to \text{\tt pivot} and
    \item Elements \text{\tt a\char91{}m\char43{}1\char93{}} to \text{\tt a\char91{}end~\char45{}~1\char93{}} are greater than or equal to \text{\tt 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 \text{\tt a\char91{}l\char93{}~\char60{}\char61{}~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 \text{\tt a\char91{}m\char93{}~\char62{}\char61{}~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 \text{\tt a\char91{}l\char93{}~\char62{}~pivot} and \text{\tt a\char91{}m\char93{}~\char60{}~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 \text{\tt partition} contd}

\begin{tabbing}\tt
~int~partition\char40{}int~\char42{}a\char44{}~int~start\char44{}~int~end\char44{}~int~pivotIndex\char41{}\\
\tt ~\char123{}\\
\tt ~~~int~l~\char61{}~start\char59{}\\
\tt ~~~int~m~\char61{}~end~\char45{}~1\char59{}\\
\tt ~~~int~pivot~\char61{}~a\char91{}pivotIndex\char93{}\char59{}\\
\tt ~~~\\
\tt ~~~while\char40{}l~\char60{}~m~\char41{}\\
\tt ~~~\char123{}\\
\tt ~~~~~if\char40{}~a\char91{}l\char93{}~\char60{}\char61{}~pivot~\char41{}~\char123{}l\char43{}\char43{}\char59{}~continue\char59{}\char125{}\\
\tt ~~~~~if\char40{}~a\char91{}m\char93{}~\char62{}\char61{}~pivot~\char41{}~\char123{}m\char45{}\char45{}\char59{}~continue\char59{}\char125{}\\
\tt ~~~~~\\
\tt ~~~~~\char47{}\char42{}~Here~a\char91{}l\char93{}~\char62{}~pivot~\char38{}\char38{}~a\char91{}m\char93{}~\char60{}~pivot~\char42{}\char47{}\\
\tt ~~~~~\\
\tt ~~~~~swap\char40{}a\char44{}~l\char44{}~m\char41{}\char59{}\\
\tt ~~~\char125{}\\
\tt ~~~return~l\char59{}\\
\tt ~\char125{}
\end{tabbing}
\end{frame}


\end{document}

