2015-05-13 20:12:10 +08:00
\documentclass [twoside] { article}
\setlength { \oddsidemargin } { 0.25 in}
\setlength { \evensidemargin } { -0.25 in}
\setlength { \topmargin } { -0.6 in}
\setlength { \textwidth } { 6.5 in}
\setlength { \textheight } { 8.5 in}
\setlength { \headsep } { 0.75 in}
\setlength { \parindent } { 0 in}
\setlength { \parskip } { 0.1 in}
2015-05-13 00:55:34 +08:00
\usepackage { clrscode3e}
2015-05-13 20:12:10 +08:00
\usepackage { multicol}
\usepackage { amsmath}
2015-05-13 00:55:34 +08:00
\begin { document}
\title { 2.2-2}
\author { pezy}
\maketitle
2015-05-13 20:12:10 +08:00
\begin { multicols} { 3}
\begin { codebox}
\Procname { $ \proc { Selection - Sort } ( A ) $ }
\li \For $ j \gets 1 $ \To $ \attrib { A } { length } - 1 $
\li \Do $ min \gets j $
\li \For $ i \gets j + 1 $ \To $ \attrib { A } { length } $
\li \Do \If $ A [ i ] < A [ min ] $
\li \Do $ min \gets i $
\End
\End
\li \func { swap} ($ A [ min ] , A [ j ] $ )
\End
\end { codebox}
\columnbreak
\begin { center} % column 2 holds cost of each statement
\textbf { \large Cost}
\\ * $ c _ { 1 } $
\\ * $ c _ { 2 } $
\\ * $ c _ { 3 } $
\\ * $ c _ { 4 } $
\\ * $ c _ { 5 } $
\\ * $ c _ { 6 } $
\end { center}
\columnbreak
\begin { flushleft} % displays run time of each statement
\textbf { \large Time}
\\ * $ n $
\\ * $ n - 1 $
\\ * $ \sum _ { j = 1 } ^ { n - 1 } t _ j $
\\ * $ \sum _ { j = 1 } ^ { n - 1 } ( t _ j - 1 ) $
\\ * $ 0 \To \sum _ { j = 1 } ^ { n - 1 } ( t _ j - 1 ) $
\\ * $ n - 1 $
\end { flushleft}
\end { multicols}
We will now calculate the running time, $ T ( n ) $ , of \proc { Selection-Sort} :
\begin { align}
\notag
T(n) & = c_ 1 n + c_ 2 (n-1) + c_ 3 \sum _ { j=1} ^ { n-1} t_ j + c_ 4 \sum _ { j=1} ^ { n-1} ( t_ j -1 ) + k c_ 5 \sum _ { j=1} ^ { n-1} (t_ j -1) + c_ 6 (n-1),\\
\notag
& = c_ 1 n+ (c_ 2 + c_ 6)(n-1) + c_ 3 \sum _ { j=1} ^ { n-1} t_ j + (c_ 4 + k c_ 5) \sum _ { j=1} ^ { n-1} (t_ j -1),\\
\notag
& = c_ 1 n+ (c_ 2 + c_ 6)(n-1) + (c_ 3 + c_ 4 + k c_ 5 ) \sum _ { j=1} ^ { n-1} t_ j - (c_ 4 + k c_ 5) (n-1),\\
\label { eq:Tn}
& = c_ 1 n +(c_ 2 + c_ 6 - c_ 4 - k c_ 5) (n-1) + (c_ 3 + c_ 4 + k c_ 5 ) \sum _ { j=1} ^ { n-1} t_ j.
\end { align}
\section { Best case running time}
In the { \bf BEST CASE} running time, the list of input will already be sorted. Thus, the body of \If is never step in, and $ k = 0 $ . we obtain that $ t _ j = j + 1 $ , for every choice of j. Thus,
\[
\sum _ { j=1} ^ { n-1} t_ j = \frac { 1} { 2} n(n+1) - 1 = (\frac { n} { 2} +1)(n-1)
\]
Substituting this into the last term of Eqn.~(\ref { eq:Tn} ) yields,
\begin { align}
T(n) & = c_ 1 n +(c_ 2 + c_ 6 - c_ 4)(n-1) + (c_ 3 + c_ 4)(\frac { n} { 2} +1)(n-1) \\
& = \frac { c_ 3+c_ 4} { 2} n^ 2 + (c_ 1+c_ 2+\frac { c_ 3} { 2} -\frac { c_ 4} { 2} +c_ 6)n - (c_ 2+c_ 3+c_ 6)
\end { align}
which can be simplified to the linear equation $ T ( n ) = An ^ 2 + Bn + C $ where
\begin { align*}
A & = \frac { c_ 3+c_ 4} { 2} > 0,\\
B & = c_ 1+c_ 2+\frac { c_ 3} { 2} -\frac { c_ 4} { 2} +c_ 6, \quad \text { and,} \\
C & = -c_ 2-c_ 3-c_ 6 < 0.
\end { align*}
Therefore, the { \bf BEST CASE} running time of the \proc { Selection-Sort} Algorithm equals:
\begin { center}
$ \boldsymbol { T ( n ) = An ^ 2 + Bn + C } $
\end { center}
%
%Begin section for Worst Case Running Time
%
%
\section { Worst case running time}
We will now look at the { \bf WORST CASE} for \proc { Selection-Sort} :
\begin { itemize}
\item In the worst case, the \If statement is invoked on every occasion.
\item This means $ k = 1 $
\end { itemize}
Substituting $ t _ { j } $ with $ j $ into the last summation in Eqn.~(\ref { eq:Tn} ) yields,
\[
\sum _ { j=1} ^ { n-1} t_ j = \frac { 1} { 2} n(n+1) - 1 = (\frac { n} { 2} +1)(n-1)
\]
Thus, Eqn.~(\ref { eq:Tn} ) becomes,
\begin { align*}
T(n) & = c_ 1 n +(c_ 2+c_ 6-c_ 4-c_ 5)(n-1) + (c_ 3+c_ 4+c_ 5)(\frac { n} { 2} +1)(n-1),\\
& = \frac { c_ 3+c_ 4+c_ 5} { 2} n^ 2 + (c_ 1+c_ 2+\frac { c_ 3} { 2} -\frac { c_ 4} { 2} -\frac { c_ 5} { 2} +c_ 6) n - (c_ 2+c_ 3+c_ 6)
\end { align*}
a \emph { quadratic function} of $ n $ , the input sequence length, where,
\begin { align*}
A' & = \frac { c_ 3+c_ 4+c_ 5} { 2} > 0,\\
B' & = c_ 1+c_ 2+\frac { c_ 3} { 2} -\frac { c_ 4} { 2} -\frac { c_ 5} { 2} +c_ 6, \quad \text { and,} \\
C' & = -c_ { 2} -c_ { 3} -c_ { 6} < 0.
\end { align*}
Therefore, the { \bf WORST CASE} running time of the \proc { Selection-Sort} Algorithm also equals:
\begin { center}
$ \boldsymbol { T ( n ) = An ^ 2 + Bn + C } $
\end { center}
2015-05-13 00:55:34 +08:00
\end { document}