Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

updating chapter 4 and rebuilding book.pdf #103

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file modified book.pdf
Binary file not shown.
34 changes: 8 additions & 26 deletions chapter04.tex
Original file line number Diff line number Diff line change
Expand Up @@ -755,40 +755,22 @@ \subsubsection{Algorithm 3}
so the total time complexity is $O(n \log n)$.

\subsubsection{Efficiency comparison}

The following table shows how efficient
the above algorithms are when $n$ varies and
the elements of the lists are random
integers between $1 \ldots 10^9$:
integers between $1 \ldots 2 \cdot 10^7$:

\begin{center}
\begin{tabular}{rrrr}
$n$ & Algorithm 1 & Algorithm 2 & Algorithm 3 \\
\hline
$10^6$ & $1.5$ s & $0.3$ s & $0.2$ s \\
$2 \cdot 10^6$ & $3.7$ s & $0.8$ s & $0.3$ s \\
$3 \cdot 10^6$ & $5.7$ s & $1.3$ s & $0.5$ s \\
$4 \cdot 10^6$ & $7.7$ s & $1.7$ s & $0.7$ s \\
$5 \cdot 10^6$ & $10.0$ s & $2.3$ s & $0.9$ s \\
$10^6$ & $2.8$ s & $2.2$ s & $2.2$ s \\
$2 \cdot 10^6$ & $5.7$ s & $4.6$ s & $5$ s \\
$3 \cdot 10^6$ & $8.9$ s & $7.4$ s & $8.6$ s \\
$4 \cdot 10^6$ & $12.3$ s & $10$ s & $12.1$ s \\
$5 \cdot 10^6$ & $15.7$ s & $12.1$ s & $15$ s \\
$10^7$ & $31.7$ s & $24.5$ s & $32.5$ s \\
\end{tabular}
\end{center}

Algorithms 1 and 2 are equal except that
they use different set structures.
In this problem, this choice has an important effect on
the running time, because Algorithm 2
is 4–5 times faster than Algorithm 1.

However, the most efficient algorithm is Algorithm 3
which uses sorting.
It only uses half the time compared to Algorithm 2.
Interestingly, the time complexity of both
Algorithm 1 and Algorithm 3 is $O(n \log n)$,
but despite this, Algorithm 3 is ten times faster.
This can be explained by the fact that
sorting is a simple procedure and it is done
only once at the beginning of Algorithm 3,
and the rest of the algorithm works in linear time.
On the other hand,
Algorithm 1 maintains a complex balanced binary tree
during the whole algorithm.
Algorithms 1 and 3 are equal and Algorithm 2 is the fastest. which is intuitive as the time complexity of Algorithm 1 and Algorithm 3 are O(nlogn) which is bigger than O(n) when n tends to be very large(like $10^7$).