\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/box/.style={ 
        shape=rectangle,%
        minimum size=1em,%
        top color=white,
        bottom color=red!50!black!50,
        inner sep=0pt,
        draw=black
      },
      /tikz/bbox/.style={
        shape=rectangle,
        minimum size=1em,
        fill=yellow,
        draw=black
      },
      /tikz/bbbox/.style={
        shape=rectangle,
        minimum size=2em,
        fill=blue!50,
        draw=black,
        thick
      },
      /tikz/garbage/.style={%
        starburst,%
        fill=yellow,%
        draw=red
      }
    }



    
\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 19}
\date{September 11, 2009}
\begin{document}
\begin{frame}
  \maketitle
\end{frame}

\begin{frame}
  \frametitle{Sorting}

  Given a list of numbers arrange them in ascending order.
  
  We have already seen a sorting algorithm.


  Let us first write a function that swaps the $i$-th and $j$-th
  element of an array.

  \begin{tabbing}\tt
~~~void~swap\char40{}int~\char42{}a\char44{}~int~i\char44{}~int~j\char41{}\\
\tt ~~~\char123{}\\
\tt ~~~~~int~temp\char59{}\\
\tt ~~~~~temp~\char61{}~a\char91{}i\char93{}\char59{}\\
\tt ~~~~~a\char91{}i\char93{}~\char61{}~a\char91{}j\char93{}\char59{}\\
\tt ~~~~~a\char91{}j\char93{}~\char61{}~temp\char59{}\\
\tt ~~~\char125{}
\end{tabbing}

        
\end{frame}

\begin{frame}
 
  
  \begin{tabbing}\tt
~~~~~for\char40{}int~i~\char61{}~0\char59{}~i~\char60{}~n~\char45{}~1\char59{}~i~\char43{}\char43{}\char41{}\\
\tt ~~~~~\char123{}\\
\tt ~~~~~~~j~\char61{}~i~\char43{}~1\char59{}\\
\tt ~~~~~~~while\char40{}~j~\char62{}~0~~\char38{}\char38{}~a\char91{}j\char93{}~\char60{}~a\char91{}j\char45{}1\char93{}~\char41{}\\
\tt ~~~~~~~\char123{}\\
\tt ~~~~~~~~~swap\char40{}a\char44{}j\char44{}j\char45{}1\char41{}\char59{}\\
\tt ~~~~~~~~~j\char45{}\char45{}\char59{}\\
\tt ~~~~~~~\char125{}\\
\tt ~~~~~\char125{}
\end{tabbing}
  How many comparisons does it take ?
  \begin{itemize}
  \item In the best case.
  \item In the worst case.
  \end{itemize}

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

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

\end{frame}


\begin{frame}
  \frametitle{How many operations?}
  

\end{frame}


\end{document}

