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 Table of Contents V

TABLE OF CONTENTS

Introduction Purpose and Objective 1

Chapter 1 Sorting Things Out 3 A Framework for Sorting 3 Elementary Sorts 8 8 10 Shell Sort 12 14 The Basic Algorithm 14 Avoiding SlowSort 20 Removing Recursion 25 The Code 27 Other Considerations 33 Array Conversions 33 Onward 36

Chapter 2 Mathematical Components 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

Chapter 3 Java Strings and 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

Chapter 4 Layout Managers 89 Types of Layout Managers 89 Building a Vertical Flow Layout 92 Building a Clock Layout 103 Onward 112 VI Table of Contents

Chapter 5 High-Performance Containers 113 Garbage Collection Issues 114 Exceptions 115 Stacks 115 Queues 119 Deques 122 Heaps & Priority Queues 127 An Example 134 Onward 140 Chapter 6 Tree Structures 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 Chapter 7 Finite State Machines and 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 Chapter 8 Matrices and Linear Algebra 219 Java Arrays & Matrices 219 Exceptions 220 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 Chapter 9 Serialization 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 Chapter 10 Data Compression 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 Chapter 11 Serializing Objects in Random Access Files 295 Random Access Objects 295 Random Access File Concepts 297 An Object Database Class 299 An Example 314 Onward 321 Chapter 12 BTree Indexed Databases 323 Properties of BTrees 323 Pages and Keys 324 Searching 325 Insertion 328 VIII Table of Contents

Chapter 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

Chapter 13 Where and When 383 Utility Classes 384 Latitude & Longitude 392 The Flattened Earth 395 Implementation 397 Time 401 The Gregorian Calendar 403 The Julian Calendar 404 Sidereal Time 408 Implementation 410 Onward 416

Chapter 14 Stellar Cartography 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

Chapter 15 A Star Map Plotter 449 Design 449 Implementation 454 Operation 463 Final Thoughts 465

Index 467