

\documentclass[11pt]{article}

\setlength{\topmargin}{-.8in}  
\setlength{\oddsidemargin}{0in}
\setlength{\evensidemargin}{0in}
\setlength{\textwidth}{6.5in}
\setlength{\textheight}{9.5in}


\begin{document}

\title{Class  work: Quicksort}

\date{}
\maketitle

\begin{enumerate}


\item Below is the pseudocode for Quicksort that we talked about in
  class. As usual with recursive functions on arrays, we see the array
  indices $p$ and $r$ as arguments. Quicksort($a,p,r$) sorts the part
  of the array between $p$ and $r$ inclusively.  The initial call
  (that is, to sort the entire array) is Quicksort($A, 0, n-1$).

\fbox{
\parbox{6cm}{
{\sc Quicksort}($A,p,r$) \\
IF $p<r$ THEN
\begin{itemize}
\item[] q={\sc Partition}($A,p,r$)
\item[] {\sc Quicksort}($A,p,q-1$)
\item[] {\sc Quicksort}($A,q+1,r$)
\end{itemize}
FI
}}\\



\fbox{
\parbox{6cm}{
{\sc Partition}($A,p,r$) \\
$x=A[r]$ \\
$i=p-1$ \\
FOR $j=p$ TO $r-1$ DO 
\begin{itemize}
\item[] IF $A[j]\leq x$ THEN
	\begin{itemize}
	\item[] $i=i+1$
	\item[] Exchange $A[i]$ and $A[j]$
	\end{itemize}
\item[] FI	
\end{itemize}
OD\\
Exchange $A[i+1]$ and $A[r]$\\
RETURN $i+1$
}}
  

Let $A=\{3,6,1,5,8,2,4,1,3 \}$, and assume we call Quicksort($A, 0,
8$).  Show what happens during the first invocation of Partition. What
is the value of $q$ returned, and what are the two recursive calls made?


\item What is the running time of \textsc{Quicksort} when
all elements of arrary $A$ have the same value?

\item Briefly sketch why  the runnning time of \textsc{Quicksort} is
  $\Theta(n^2)$ when the arrary $A$ contains distinct elements and is
  sorted in decreasing order.


% \item Argue that for any constant $0 < \alpha \le 1/2$, the
%   probability is approximately $1 -2\alpha$ that on a random input
%   array, PARTITION produces a split more balanced that
%   $(1-\alpha)$-to-$\alpha$.

\end{enumerate}



\end{document}
%%% Local Variables: 
%%% mode: latex
%%% TeX-master: t
%%% End: 

