5
Java Algorithms Scott Robert Ladd McGraw-Hill New York San Francisco Washington, D.С Auckland Bogota Caracas Lisbon London Madrid Mexico City Milan Montreal New Delhi San Juan Singapore Sydney Tokyo Toronto

Java Algorithms - GBV

  • Upload
    others

  • View
    16

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Java Algorithms - GBV

Java Algorithms

Scott Robert Ladd

McGraw-Hill New York San Francisco Washington, D.С

Auckland Bogota Caracas Lisbon London Madrid Mexico City Milan Montreal New Delhi

San Juan Singapore Sydney Tokyo Toronto

Page 2: Java Algorithms - GBV

Table of Contents V

TABLE OF CONTENTS

I n t r o d u c t i o n P u r p o s e a n d Objective 1

C h a p t e r 1 Sor t ing Th ings O u t 3 A Framework for Sorting 3 Elementary Sorts 8

Selection Sort 8 Insertion Sort 10

Shell Sort 12 Quicksort 14

The Basic Algorithm 14 Avoiding SlowSort 20

Removing Recursion 25 The Code 27 Other Considerations 33

Array Conversions 33 Onward 36

C h a p t e r 2 M a t h e m a t i c a l Componen t s 37 Java Does Math 37

Conversions 38 Floating Point Bestiary 39 The Math Class 40

Polynomials 41 A Java Implementation 42

Complex Numbers 48 Complex Polynomials 53 The Fast Fourier Transform 58

Onward 68

C h a p t e r 3 J a v a S t r ings a n d Text 69 Brute Force 69 Boyer-Moore String Searching 72 Classes for Searching 74

A Generic Base 75 The BoyerMoore Class 78 Tools for Searching Text Areas 82

Onward 88

C h a p t e r 4 Layou t M a n a g e r s 89 Types of Layout Managers 89 Building a Vertical Flow Layout 92 Building a Clock Layout 103 Onward 112

Page 3: Java Algorithms - GBV

VI Table of Contents

C h a p t e r 5 High-Per fo rmance Con ta ine r s 113 Garbage Collection Issues 114 Exceptions 115 Stacks 115 Queues 119 Deques 122 Heaps & Priority Queues 127 An Example 134 Onward 140

C h a p t e r 6 Tree S t r u c t u r e s 141 Binary Trees 141

Searching 142 Insertion 144 Deletion 145 Exceptions 147

Implementing a Binary Tree 148 Iterators 150 Constructors and Properties 154 Utility Methods 155 Adding and Removing Objects 158 Limitations 161

Red Black Trees 163 Red Black Tree Class 163

Iterators 164 Sorting Tools 165 Constructors and Destructors 166 Utility Functions 167 Insertion 170 Deletion 172

Onward 174

C h a p t e r 7 F in i t e S t a t e Mach ines a n d Evolving Software 175 FSM Design 175 Exceptions 177 Transitions 177 The FSM Class 178 Finite State Machines in Action 183 Evolving Software 186 Evolving Machines 189 Robot Ants 196 Stochastic Algorithms 208

Random Numbers 208 Algorithms 209

Onward 218

C h a p t e r 8 Mat r i ces a n d L inea r Algebra 219 Java Arrays & Matrices 219 Exceptions 220

Page 4: Java Algorithms - GBV

Table of Contents VII

Basic Operations 221 Matrix Characteristics 223 Basic Operations 230 Matrix Multiplication 235

Minors 240 Linear Equations and Gaussian Elimination 243 LUP Decomposition 247 Inversion 252 Onward 252

C h a p t e r 9 Ser ia l iza t ion 253 Concepts 254 Object Identity 255 Object Streams 257

ObjectOutput 257 ObjectOutputStream 258 Objectlnput 260 ObjectlnputStreams 261

Serialization Interfaces 263 Serializable 263 Transient Fields 264 Externalizable 264

ObjectOutput 265 Objectlnput 267

Versioning 268 Onward 270

C h a p t e r 10 D a t a Compress ion 271 Object-Oriented Compression 274 Encoded Data 275

Headers 275 Compressed Data 276

The Huffman Class 277 Compression 277 Exceptions 289

Elaborations 289 Onward 293

C h a p t e r 11 Ser ia l iz ing Objects in R a n d o m Access Fi les 295 Random Access Objects 295

Random Access File Concepts 297 An Object Database Class 299 An Example 314 Onward 321

C h a p t e r 12 BTree Indexed D a t a b a s e s 323 Properties of BTrees 323

Pages and Keys 324 Searching 325 Insertion 328

Page 5: Java Algorithms - GBV

VIII Table of Contents

C h a p t e r 12 {cont'd) Deletion 331 Implementation Issues 332

Exceptions 333 BTree Page Classes 335

Page Class 335 Page Files 340 BTree Database Class 347

Constructors 348 Writing Records 350 Reading Records 361 Deleting Records 362 InOrder Traversal '. 374

An Example 376 Onward 381

C h a p t e r 13 W h e r e a n d W h e n 383 Utility Classes 384 Latitude & Longitude 392

The Flattened Ear th 395 Implementation 397 Time 401 The Gregorian Calendar 403 The Julian Calendar 404 Sidereal Time 408 Implementation 410

Onward 416

C h a p t e r 14 S te l l a r C a r t o g r a p h y 417 Movement in the Heavens 418 Geometry in the Round 419 Adjustments 425

Epochs 425 Precession 426 Proper Motion 428 Aberration 429 Refraction 430

Implementation 431 Examples 445 Onward 448

C h a p t e r 15 A S t a r M a p P lo t t e r 449 Design 449 Implementation 454 Operation 463 Final Thoughts 465

I n d e x 467