Mastering Algorithms with Perl.pdf

(16461 KB) Pobierz
Mastering Algorithms with Perl
Jarkko Hietaniemi, John Macdonald, and Jon Orwant
Mastering Algorithms with Perl
by Jarkko Hietaniemi, John Macdonald, and Jon Orwant
Copyright © 2010 O'Reilly Media, Inc.. All rights reserved.
Printed in the United States of America.
Published by O’Reilly Media, Inc., 1005 Gravenstein Highway North, Sebastopol, CA 95472.
O’Reilly books may be purchased for educational, business, or sales promotional use. Online editions are
also available for most titles (http://my.safaribooksonline.com). For more information, contact our cor­
porate/institutional sales department: 800-998-9938 or
corporate@oreilly.com.
Editor:
Andy Oram
Cover Designer:
Karen Montgomery
Interior Designer:
David Futato
Illustrator:
Robert Romano
First Edition
August 1999:
Revision History for the First Edition:
See
http://oreilly.com/catalog/errata.csp?isbn=9781565923980
for release details.
Nutshell Handbook, the Nutshell Handbook logo, and the O’Reilly logo are registered trademarks of
O’Reilly Media, Inc.
Mastering Algorithms with Perl
and related trade dress are trademarks of O’Reilly
Media, Inc.
Many of the designations used by manufacturers and sellers to distinguish their products are claimed as
trademarks. Where those designations appear in this book, and O’Reilly Media, Inc., was aware of a
trademark claim, the designations have been printed in caps or initial caps.
While every precaution has been taken in the preparation of this book, the publisher and authors assume
no responsibility for errors or omissions, or for damages resulting from the use of the information con­
tained herein.
ISBN: 978-1-565-92398-0
[LSI]
Table of Contents
Preface. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xiii
1. Introduction. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
What Is an Algorithm?
A Sample Algorithm: Binary Search
Adapting Algorithms
Generality
Efficiency
Space Versus Time
Benchmarking
Floating-Point Numbers
Temporary Variables
Caching
Evaluating Algorithms: O(N) Notation
Recurrent Themes in Algorithms
Recursion
Divide and Conquer
Dynamic Programming
Choosing the Right Representation
1
2
5
6
9
9
11
14
15
16
17
21
21
22
22
23
2. Basic Data Structures. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25
Perl’s Built-in Data Structures
Build Your Own Data Structure
A Simple Example
Lols and Lohs and Hols and Hohs
Objects
Using a Constructed Datatype
Shortcuts
Perl Arrays: Many Data Structures in One
Queues
26
27
28
31
32
35
36
38
39
iii
Zgłoś jeśli naruszono regulamin