From 1ee57c911b197c52c8e816af536d24767abb5f0f Mon Sep 17 00:00:00 2001 From: Antti H S Laaksonen Date: Sun, 28 May 2017 10:40:54 +0300 Subject: [PATCH] Fix indices --- chapter02.tex | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/chapter02.tex b/chapter02.tex index 2cfcf52..e1e81af 100644 --- a/chapter02.tex +++ b/chapter02.tex @@ -411,10 +411,10 @@ The following code implements this algorithm: \begin{lstlisting} int best = 0; -for (int first = 0; first < n; first++) { - for (int last = first; last < n; last++) { +for (int a = 0; a < n; a++) { + for (int b = a; b < n; b++) { int sum = 0; - for (int k = first; k <= last; k++) { + for (int k = a; k <= b; k++) { sum += array[k]; } best = max(best,sum); @@ -423,9 +423,9 @@ for (int first = 0; first < n; first++) { cout << best << "\n"; \end{lstlisting} -The variables \texttt{first} and \texttt{last} determine the range -of the subarray, -and the sum of the numbers is calculated to the variable \texttt{sum}. +The variables \texttt{a} and \texttt{b} fix the first and +last index of the subarray, +and the sum of values is calculated to the variable \texttt{sum}. The variable \texttt{best} contains the maximum sum found during the search. The time complexity of the algorithm is $O(n^3)$, @@ -442,10 +442,10 @@ The result is the following code: \begin{lstlisting} int best = 0; -for (int first = 0; first < n; first++) { +for (int a = 0; a < n; a++) { int sum = 0; - for (int last = first; last < n; last++) { - sum += array[last]; + for (int b = a; b < n; b++) { + sum += array[b]; best = max(best,sum); } }