Clean code etc.

This commit is contained in:
Antti H S Laaksonen 2017-05-25 23:34:59 +03:00
parent a98da53ce4
commit bb35501b3d
1 changed files with 15 additions and 18 deletions

View File

@ -172,7 +172,7 @@ calls, except for $n=1$.
Let us see what happens when $g$ is called Let us see what happens when $g$ is called
with parameter $n$. with parameter $n$.
The following table shows the function calls The following table shows the function calls
this single call yields: produced by this single call:
\begin{center} \begin{center}
\begin{tabular}{rr} \begin{tabular}{rr}
function call & number of calls \\ function call & number of calls \\
@ -362,11 +362,11 @@ Given an array of $n$ numbers,
our task is to calculate the our task is to calculate the
\key{maximum subarray sum}, i.e., \key{maximum subarray sum}, i.e.,
the largest possible sum of the largest possible sum of
a sequence of consecutive numbers a sequence of consecutive values
in the array\footnote{J. Bentley's in the array\footnote{J. Bentley's
book \emph{Programming Pearls} \cite{ben86} made the problem popular.}. book \emph{Programming Pearls} \cite{ben86} made the problem popular.}.
The problem is interesting when there may be The problem is interesting when there may be
negative numbers in the array. negative values in the array.
For example, in the array For example, in the array
\begin{center} \begin{center}
\begin{tikzpicture}[scale=0.7] \begin{tikzpicture}[scale=0.7]
@ -403,22 +403,19 @@ the following subarray produces the maximum sum $10$:
\subsubsection{Algorithm 1} \subsubsection{Algorithm 1}
Assume that the numbers are stored in
an array \texttt{t}.
A straightforward way to solve the problem A straightforward way to solve the problem
is to go through all possible ways of is to go through all possible subarrays,
selecting a subarray, calculate the sum of calculate the sum of values in each subarray and maintain
the numbers in each subarray and maintain
the maximum sum. the maximum sum.
The following code implements this algorithm: The following code implements this algorithm:
\begin{lstlisting} \begin{lstlisting}
int best = 0; int best = 0;
for (int a = 0; a < n; a++) { for (int first = 0; first < n; first++) {
for (int b = a; b < n; b++) { for (int last = first; last < n; last++) {
int sum = 0; int sum = 0;
for (int k = a; k <= b; k++) { for (int k = first; k <= last; k++) {
sum += t[k]; sum += array[k];
} }
best = max(best,sum); best = max(best,sum);
} }
@ -426,8 +423,8 @@ for (int a = 0; a < n; a++) {
cout << best << "\n"; cout << best << "\n";
\end{lstlisting} \end{lstlisting}
The variables \texttt{a} and \texttt{b} determine the first and last The variables \texttt{first} and \texttt{last} determine the range
number in the subarray, of the subarray,
and the sum of the numbers is calculated to the variable \texttt{sum}. and the sum of the numbers is calculated to the variable \texttt{sum}.
The variable \texttt{best} contains the maximum sum found during the search. The variable \texttt{best} contains the maximum sum found during the search.
@ -445,10 +442,10 @@ The result is the following code:
\begin{lstlisting} \begin{lstlisting}
int best = 0; int best = 0;
for (int a = 0; a < n; a++) { for (int first = 0; first < n; first++) {
int sum = 0; int sum = 0;
for (int b = a; b < n; b++) { for (int last = first; last < n; last++) {
sum += t[b]; sum += array[last];
best = max(best,sum); best = max(best,sum);
} }
} }
@ -489,7 +486,7 @@ The following code implements the algorithm:
\begin{lstlisting} \begin{lstlisting}
int best = 0, sum = 0; int best = 0, sum = 0;
for (int k = 0; k < n; k++) { for (int k = 0; k < n; k++) {
sum = max(t[k],sum+t[k]); sum = max(array[k],sum+array[k]);
best = max(best,sum); best = max(best,sum);
} }
cout << best << "\n"; cout << best << "\n";