\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/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{spec} void swap(int *a, int i, int j) { int temp; temp = a[i]; a[i] = a[j]; a[j] = temp; } \end{spec} \end{frame} \begin{frame} \begin{spec} for(int i = 0; i < n - 1; i ++) { j = i + 1; while( j > 0 && a[j] < a[j-1] ) { swap(a,j,j-1); j--; } } \end{spec} 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{code} void qsort(int *a , int start, int end) { if( start >= end) return pivot = start; /* partition */ l = start + 1; m = end do { while( l < m && a[l] < a[pivot] ) l ++; while( m > l && a[m] > a[pivot] ) m --; if( m > l) swap(a, l, m); }while(l < m); if( a[l] < a[pivot]) { swap(a, l, pivot); qsort(a,start, l); qsort(a,m+1,end); } else { swap(a, pivot, l - 1); qsort(a, start,l-2); qsort(a,m,end); } } \end{code} \end{frame} \begin{frame} \frametitle{How many operations?} \end{frame} \end{document}