Algorithms in Java: Parts 1-4, Third Edition By Robert Sedgewick Publisher: Addison Wesley Pub Date: July 23, 2002 ISBN: 0-201-36120-5, 768 pages Sedgewick has a real gift for explaining concepts in a way that makes them easy to understand. The use of real programs in page-size (or less) chunks that can be easily understood is a real plus. The figures, programs, and tables are a significant contribution to the learning experience of the reader; they make this book distinctive William A. Ward, University of South Alabama This edition of Robert Sedgewick's popular work provides current and comprehensive coverage of important algorithms for Java programmers. Michael Schidlowsky and Sedgewick have developed new Java implementations that both express the methods in a concise and direct manner and provide programmers with the practical means to test them on real applications. Many new algorithms are presented, and the explanations of each algorithm are much more detailed than in previous editions. A new text design and detailed, innovative figures, with accompanying commentary, greatly enhance the presentation. The third edition retains the successful blend of theory and practice that has made Sedgewick's work an invaluable resource for more than 400,000 programmers! This particular book, Parts 1-4, represents the essential first half of Sedgewick's complete work. It provides extensive coverage of fundamental data structures and algorithms for sorting, searching, and related applications. Although the substance of the book applies to programming in any language, the implementations by Schidlowsky and Sedgewick also exploit the natural match between Java classes and abstract data type (ADT) implementations. Highlights Java class implementations of more than 100 important practical algorithms Emphasis on ADTs, modular programming, and object-oriented programming Extensive coverage of arrays, linked lists, trees, and other fundamental data structures Thorough treatment of algorithms for sorting, selection, priority queue ADT implementations, and symbol table ADT implementations (search algorithms) C omplete implementations for binomial queues, multiway radix sorting, randomized BSTs, splay trees, skip lists, multiway tries, B trees, extendible hashing, and many other advanced methods Quantitative information about the algorithms that gives you a basis for comparing them More than 1,000 exercises and more than 250 detailed figures to help you learn properties of the algorithms Whether you are learning the algorithms for the first time or wish to have up-to-date reference material that incorporates new programming styles with classic and new algorithms, you will find a wealth of useful information in this book. 1 / 414 Algorithms in Java: Parts 1-4, Third Edition C opyright Preface Scope Use in the C urriculum Algorithms of Practical Use Programming Language Acknowledgments Java C onsultant's Preface Notes on Exercises Part I: Fundamentals Chapter 1. Introduction Section 1.1. Algorithms Section 1.2. A Sample Problem: C onnectivity Section 1.3. Union–Find Algorithms Section 1.4. Perspective Section 1.5. Summary of Topics Chapter 2. Principles of Algorithm Analy sis Section 2.1. Implementation and Empirical Analysis Section 2.2. Analysis of Algorithms Section 2.3. Growth of Functions Section 2.4. Big-Oh Notation Section 2.5. Basic Recurrences Section 2.6. Examples of Algorithm Analysis Section 2.7. Guarantees, Predictions, and Limitations References for Part One Part II: Data Structures Chapter 3. Ele mentary Data Structures Section 3.1. Building Blocks Section 3.2. Arrays Section 3.3. Linked Lists Section 3.4. Elementary List Processing Section 3.5. Memory Allocation for Lists Section 3.6. Strings Section 3.7. C ompound Data Structures Chapter 4. Abstract Data Ty pes Exercises Section 4.1. C ollections of Items Section 4.2. Pushdown Stack ADT Section 4.3. Examples of Stack ADT C lients Section 4.4. Stack ADT Implementations Section 4.5. Generic Implementations Section 4.6. C reation of a New ADT Section 4.7. FIFO Queues and Generalized Queues Section 4.8. Duplicate and Index Items Section 4.9. First-Class ADTs Section 4.10. Application-Based ADT Example Section 4.11. Perspective Chapter 5. Recursion and Trees Section 5.1. Recursive Algorithms Section 5.2. Divide and C onquer Section 5.3. Dynamic Programming Section 5.4. Trees Section 5.5. Mathematical Properties of Binary Trees Section 5.6. Tree Traversal Section 5.7. Recursive Binary-Tree Algorithms Section 5.8. Graph Traversal Section 5.9. Perspective References for Part Two Part III: Sorting Chapter 6. Ele mentary Sorting Methods Section 6.1. Rules of the Game Section 6.2. Generic Sort Implementations Section 6.3. Selection Sort Section 6.4. Insertion Sort Section 6.5. Bubble Sort Section 6.6. Performance C haracteristics of Elementary Sorts Section 6.7. Algorithm Visualization Section 6.8. Shellsort Section 6.9. Sorting of Linked Lists Section 6.10. Key-Indexed C ounting Chapter 7. Quicksort Section 7.1. The Basic Algorithm Section 7.2. Performance C haracteristics of Quicksort Section 7.3. Stack Size Section 7.4. Small Subfiles Section 7.5. Median-of-Three Partitioning 2 / 414 Section 7.6. Duplicate Keys Section 7.7. Strings and Vectors Section 7.8. Selection Chapter 8. Merging and Mergesort Section 8.1. Two-Way Merging Section 8.2. Abstract In-Place Merge Section 8.3. Top-Down Mergesort Section 8.4. Improvements to the Basic Algorithm Section 8.5. Bottom-Up Mergesort Section 8.6. Performance C haracteristics of Mergesort Section 8.7. Linked-List Implementations of Mergesort Section 8.8. Recursion Revisited Chapter 9. Priority Que ues and Heapsort Exercises Section 9.1. Elementary Implementations Section 9.2. Heap Data Structure Section 9.3. Algorithms on Heaps Section 9.4. Heapsort Section 9.5. Priority-Queue ADT Section 9.6. Priority Queues for C lient Arrays Section 9.7. Binomial Queues Chapter 10. Radix Sorting Section 10.1. Bits, Bytes, and Words Section 10.2. Binary Quicksort Section 10.3. MSD Radix Sort Section 10.4. Three-Way Radix Quicksort Section 10.5. LSD Radix Sort Section 10.6. Performance Characteristics of Radix Sorts Section 10.7. Sublinear-Time Sorts Chapter 11. Special-Purpose Sorting Methods Section 11.1. Batcher's Odd–Even Mergesort Section 11.2. Sorting Networks Section 11.3. Sorting In Place Section 11.4. External Sorting Section 11.5. Sort–Merge Implementations Section 11.6. Parallel Sort–Merge References for Part Three Part IV: Searching Chapter 12. Symbol Tables and Binary Search Trees Section 12.1. Symbol-Table Abstract Data Type Section 12.2. Key-Indexed Search Section 12.3. Sequential Search Section 12.4. Binary Search Section 12.5. Index Implementations with Symbol Tables Section 12.6. Binary Search Trees Section 12.7. Performance Characteristics of BSTs Section 12.8. Insertion at the Root in BSTs Section 12.9. BST Implementations of Other ADT Operations Chapter 13. Balance d Trees Exercises Section 13.1. Randomized BSTs Section 13.2. Splay BSTs Section 13.3. Top-Down 2-3-4 Trees Section 13.4. Red–Black Trees Section 13.5. Skip Lists Section 13.6. Performance Characteristics Chapter 14. Hashing Section 14.1. Hash Functions Section 14.2. Separate C haining Section 14.3. Linear Probing Section 14.4. Double Hashing Section 14.5. Dynamic Hash Tables Section 14.6. Perspective Chapter 15. Radix Search Section 15.1. Digital Search Trees Section 15.2. Tries Section 15.3. Patricia Tries Section 15.4. Multiway Tries and TSTs Section 15.5. Text-String–Index Algorithms Chapter 16. External Searching Section 16.1. Rules of the Game Section 16.2. Indexed Sequential Access Section 16.3. B Trees Section 16.4. Extendible Hashing Section 16.5. Perspective References for Part Four Appendix Exercises Top 3 / 414 4 / 414 5 / 414 Copyright 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 Addison-Wesley was aware of a trademark claim, the designations have been printed in initial capital letters or all capitals. The author and publisher have taken care in the preparation of this book, but make no expressed or implied warranty of any kind and assume no responsibility for errors or omissions. No liability is assumed for incidental or consequential damages in connection with or arising out of the use of the information or programs contained herein. The publisher offers discounts on this book when ordered in quantity for special sales. For more information, please contact: U.S. C orporate and Government Sales (800) 382-3410 corpsales@pearsontechgroup.com. For sales outside of the United States, please contact: International Sales (317) 581-3793 international@pearsontechgroup.com Visit Addison-Wesley on the Web: www.awprofessional.com Library of Congress Cataloging-in-Publication Data Sedgewick, Robert, 1946 – Algorithms in Java / Robert Sedgewick. — 3d ed. p. cm. Includes bibliographical references and index. C ontents: v. 1, pts. 1–4. Fundamentals, data structures, sorting, searching. 1. Java (C omputer program language) 2. C omputer algorithms. I. Title. QA76.73.C 15S 2003 005.13'3—dc20 92-901 C IP C opyright © 2003 by Pearson Education, Inc. All rights reserved. No part of this publication may be reproduced, stored in a retrieval system, or transmitted, in any form or by any means, electronic, mechanical, photocopying, recording, or otherwise, without the prior written permission of the publisher. Printed in the United States of America. Published simultaneously in C anada. For information on obtaining permission for use of material from this work, please submit a written request to: Pearson Education, Inc. 75 Arlington Street, Suite 300 Boston, MA 02116 Fax: (617) 848-7047 corpsales@pearsontechgroup.com Text printed on recycled paper 1 2 3 4 5 6 7 8 9 10 – C RS – 0605040302 First printing, July 2002 Dedication To Adam, Andrew, Brett, Robbie, and especially Linda Top 6 / 414 Preface This book is the first of three volumes that are intended to survey the most important computer algorithms in use today. This first volume (Parts I–IV) covers fundamental concepts (Part I), data structures (Part II), sorting algorithms (Part III), and searching algorithms (Part IV); the second volume (Part 5) covers graphs and graph algorithms; and the (yet to be published) third volume (Parts 6–8) covers strings (Part 6), computational geometry (Part 7), and advanced algorithms and applications (Part 8). The books are useful as texts early in the computer science curriculum, after students have acquired basic programming skills and familiarity with computer systems, but before they have taken specialized courses in advanced areas of computer science or computer applications. The books also are useful for self-study or as a reference for people engaged in the development of computer systems or applications programs because they contain implementations of useful algorithms and detailed information on these algorithms' performance characteristics. The broad perspective taken makes the series an appropriate introduction to the field. Together the three volumes comprise the Third Edition of a book that has been widely used by students and programmers around the world for many years. I have completely rewritten the text for this edition, and I have added thousands of new exercises, hundreds of new figures, dozens of new programs, and detailed commentary on all the figures and programs. This new material provides both coverage of new topics and fuller explanations of many of the classic algorithms. A new emphasis on abstract data types throughout the books makes the programs more broadly useful and relevant in modern object-oriented programming environments. People who have read previous editions will find a wealth of new information throughout; all readers will find a wealth of pedagogical material that provides effective access to essential concepts. These books are not just for programmers and computer science students. Everyone who uses a computer wants it to run faster or to solve larger problems. The algorithms that we consider represent a body of knowledge developed during the last 50 years that is the basis for the efficient use of the computer for a broad variety of applications. From N-body simulation problems in physics to genetic-sequencing problems in molecular biology, the basic methods described here have become essential in scientific research; and from database systems to Internet search engines, they have become essential parts of modern software systems. As the scope of computer applications becomes more widespread, so grows the impact of basic algorithms. The goal of this book is to serve as a resource so that students and professionals can know and make intelligent use of these fundamental algorithms as the need arises in whatever computer application they might undertake. Top 7 / 414 Scope This book, Algorithms in Java, Third Edition, Parts 1-4, contains 16 chapters grouped into four major parts: fundamentals, data structures, sorting, and searching. The descriptions here are intended to give readers an understanding of the basic properties of as broad a range of fundamental algorithms as possible. The algorithms described here have found widespread use for years, and represent an essential body of knowledge for both the practicing programmer and the computer-science student. The second volume is devoted to graph algorithms, and the third consists of four additional parts that cover strings, geometry, and advanced topics. My primary goal in developing these books has been to bring together fundamental methods from these areas, to provide access to the best methods known for solving problems by computer. You will most appreciate the material here if you have had one or two previous courses in computer science or have had equivalent programming experience: one course in programming in a high-level language such as Java, C , or C ++, and perhaps another course that teaches fundamental concepts of programming systems. This book is thus intended for anyone conversant with a modern programming language and with the basic features of modern computer systems. References that might help to fill in gaps in your background are suggested in the text. Most of the mathematical material supporting the analytic results is self-contained (or is labeled as beyond the scope of this book), so little specific preparation in mathematics is required for the bulk of the book, although mathematical maturity is definitely helpful. Top 8 / 414 Use in the Curriculum There is a great deal of flexibility in how the material here can be taught, depending on the taste of the instructor and the preparation of the students. There is sufficient coverage of basic material for the book to be used to teach data structures to beginners, and there is sufficient detail and coverage of advanced material for the book to be used to teach the design and analysis of algorithms to upper-level students. Some instructors may wish to emphasize implementations and practical concerns; others may wish to emphasize analysis and theoretical concepts. An elementary course on data structures and algorithms might emphasize the basic data structures in Part II and their use in the implementations in Parts III and IV. A course on design and analysis of algorithms might emphasize the fundamental material in Part I and C hapter 5, then study the ways in which the algorithms in Parts III and IV achieve good asymptotic performance. A course on software engineering might omit the mathematical and advanced algorithmic material, and emphasize how to integrate the implementations given here into large programs or systems. A course on algorithms might take a survey approach and introduce concepts from all these areas. Earlier editions of this book that are based on other programming languages have been used at scores of colleges and universities as a text for the second or third course in computer science and as supplemental reading for other courses. At Princeton, our experience has been that the breadth of coverage of material in this book provides our majors with an introduction to computer science that can be expanded on in later courses on analysis of algorithms, systems programming, and theoretical computer science, while providing the growing group of students from other disciplines with a large set of techniques that these people can put to good use immediately. The exercises—nearly all of which are new to this third edition—fall into several types. Some are intended to test understanding of material in the text, and simply ask readers to work through an example or to apply concepts described in the text. Others involve implementing and putting together the algorithms, or running empirical studies to compare variants of the algorithms and to learn their properties. Still others are a repository for important information at a level of detail that is not appropriate for the text. Reading and thinking about the exercises will pay dividends for every reader. Top 9 / 414 Algorithms of Practical Use Anyone wanting to use a computer more effectively can use this book for reference or for self-study. People with programming experience can find information on specific topics throughout the book. To a large extent, you can read the individual chapters in the book independently of the others, although, in some cases, algorithms in one chapter make use of methods from a previous chapter. The orientation of the book is to study algorithms likely to be of practical use. The book provides information about the tools of the trade to the point that readers can confidently implement, debug, and put algorithms to work to solve a problem or to provide functionality in an application. Full implementations of the methods discussed are included, as are descriptions of the operations of these programs on a consistent set of examples. Because we work with real code, rather than write pseudo-code, you can put the programs to practical use quickly. Program listings are available from the book's home page. You can use these working programs in many ways to help you study algorithms. Read them to check your understanding of the details of an algorithm, or to see one way to handle initializations, boundary conditions, and other awkward situations that often pose programming challenges. Run them to see the algorithms in action, to study performance empirically and check your results against the tables in the book, or to try your own modifications. C haracteristics of the algorithms and of the situations in which they might be useful are discussed in detail. C onnections to the analysis of algorithms and theoretical computer science are developed in con-text. When appropriate, empirical and analytic results are presented to illustrate why certain algorithms are preferred. When interesting, the relationship of the practical algorithms being discussed to purely theoretical results is described. Specific information on performance characteristics of algorithms and implementations is synthesized, encapsulated, and discussed throughout the book. Top 10 / 414 [...]... and the standard Java mechanism for taking parameter values from the command line is described in Section 3.7 public class QuickF { public static void main(String[] args) { int N = Integer.parseInt(args[0]); int id[] = new int[N]; for (int i = 0; i < N ; i++) id[i] = i; for( In. init(); !In. empty(); ) { int p = In. getInt(), q = In. getInt(); int t = id[p]; if (t == id[q]) continue; for (int i = 0;i . main(String[] args) { int N = Integer.parseInt(args[0]); int id[] = new int[N]; for (int i = 0; i < N ; i++) id[i] = i; for( In. init(); !In. empty(); ) { int p = In. getInt(), q = In. getInt();. photocopying, recording, or otherwise, without the prior written permission of the publisher. Printed in the United States of America. Published simultaneously in C anada. For information on obtaining. Edition, Parts 1-4, contains 16 chapters grouped into four major parts: fundamentals, data structures, sorting, and searching. The descriptions here are intended to give readers an understanding of