`to publish.
`I
`recipes to A
`Now we can sa_V,
`if you follow
`
`TEX is a trademark of the American Mathematical Society
`
`METHFONT is a trademark of Addison—Wesley
`
`Library of Congress Cataloging-in-Publication Data
`Knuth, Donald Ervin, 1938-
`The art of computer programming : fundamental algorithms / Donald
`Ervin Knuth. -- 3rd ed.
`xx,650 p.
`24 cm.
`Includes bibliographical references and index.
`ISBN 0-201-89683-4
`1. Electronic digital computers-—Programming.
`algorithms.
`I. Title.
`QA76.6.K64
`1997
`005 . 1--dc21
`
`2. Computer
`
`97-2147
`CIP
`
`
`
`\
`I,
`'2
`4’
`
`0
`‘ 3
`.3.
`‘ ‘
`-"
`A
`
`THE PROCESS of prep
`tive, not only because
`also because it can b<
`music. This book is th
`
`designed to train the 1
`The following cha1
`programming; the rear
`prerequisites are actua
`in order to understanc
`possess:
`a) Some idea of how
`the electronics, ra‘
`machine's memory
`
`b) An ability to put
`computer can “um
`they do exactly a.
`hardest concept to
`C) some knowledge °j
`mg (performing a 5
`the use of indexed
`
`d) A little knowledge
`“bits,” “floating p(
`the text are given '-
`
`These four prerequisite:
`that the reader should
`grams for at least one c
`I have tried to writ
`
`needs.
`
`In the first plac
`
`Internet page http://www-cs-faculty.stamford.edu/"knuth/taocp.htm1 contains
`current information about this book and related books.
`
`Copyright © 1997 by Addison Wesley Longman
`
`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 consent of the publisher.
`Printed in the United States of America. Published simultaneously in Canada.
`
`ISBN 0-201-89683-4
`
`Text printed on acid-free paper
`1 2 3.4 5 6 78 9MA 00999897
`
`First printing, May 1997
`
`4‘iF'.i"—'.'-<=lH-_un';'.-..-
`
`.n.1--- -.m.u...rd\'-w am-~---\-u
`
` "
`
`
`
`Cr(
`[9
`
`l!a
`
`aE on o da 2 U E
`
`d
`
`zaF
`lqoF
`l 5l
`!a
`
`Fl
`
`Hz
`
`lF
`N'
`
`0e
`
`a
`
`
`n
`
`3*
`
`(e
`
`*
`I
`
`
`
`oa
`
`j z+='i9 PE'P=-
`rhajf.u- + U. o io 5E Eo
`=
`=':j.Z -E e < .-
`a a
`-6+E65R-5=3n
`g=3d=.:=3i"iE
`;=-+fop96-+p,
`5!5+dx'=5o2^=
`E s lq iFE-e EE rE.
`E U)O At>At--
`H+.-
`
`AU'PAH'-
`+'J:Hr
`
`vvvvv
`\J)H.u.
`
`x
`
`i'
`o .D v o
`
`€
`
`?
`i
`P
`+
`4a
`(D +
`tsA' +
`3
`(D
`d?
`m i:..*
`
`<btDx 9-7=
`C-^=x 1X-E-P"
`.
`5 0c:*
`* >6' g.re
`
`=' a-
`l0ro== oo=
`:P.o
`tscf(+
`=!its
`PP+J7UP
`6O
`
`Pao
`
`-
`
`PA
`
`lJ
`ia-
`5
`+
`Oo!ts1
`?-.
`.5
`aArrE,+5+
`5
`-o
`*
`i3'E-
`B5=+9
`i-.d'2^6:+X
`-+=oX
`d
`X
`A'o
`r
`^-ry!o+-+Q-;l
`
`rD
`
`lla
`==
`
`o t
`s
`
`le
`
`5,
`
`II-
`
`o +Poao lDaah
`
`-,
`
`
`
`!o
`
`-oa
`
`oco+
`
`
`0a + a l
`g l
`
`D o a P o aoEc+o
`
`l0
`
`
`
`ooa o+o tsac
`
`f
`
`X + a
`
`a
`
`le
`
`p-
`
`D
`a aa .
`
`o (
`D a o
`
`ri
`
`(D
`oe
`
`p
`
`+ o
`
`,.ioa
`
`lD
`
`D a o oa O
`Da (
`+ (
`
`r
`
`(D
`
`a l
`
`:
`
`a + ao
`
`ts
`
`le
`
`t= i
`
`== =
`
`=== =
`
`NNi" l"
`
`H'
`
`tranz Srn OD rFo.Oa EJ2ui
`
`,o
`
`Eoo
`
`!
`
`'=cc
`oIA
`
`oop o
`
`o Eo
`
`a o l
`e o a
`
`oo El
`D a
`
`aa
`
`a Ho oao + (
`D
`
`le
`
`s + a o p
`ot
`
`-
`
`oo |
`: p
`
`0q
`
`t
`
`H+
`
`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
`(o:l-
`q6 6X
`+i:
`9=o5
`+L++ "* 5H
`-Fi,6.(! !.+^i
`!e€(P9-
`(0 Fr b-i
`p-o" 5 =
`!D -;
`s
`* H
`* A'id
`o*lra
`, i5 96'
`o +
`vx
`u)4ah-
`adah Q=A.D
`i6so XF
`oa
`-L
`^.vL^- x
`l!C?:'
`\viP
`u/i->
`L^ E 4
`!-/^'
`-e N*a'
`Fg^+
`=t'atD E-J a3
`<.d 5 6
`r*(u- aZ E'=
`f'-: 6 6'
`iD S =S
`c. lQ
`!o Q
`E"E
`
`l!;<
`X--d^
`
`U)
`
`.D
`
`6(
`
`!
`
`(D
`}.,
`
`a
`
`(D
`
`o
`4l
`
`D
`O oE(
`oEC
`
`=i =
`i i"i=i i +;iEE ==,-===s=_= ..
`=;:;i,Ei irc
`+$iiffu$! flf}ifgi+l*
`'qgIri3 n i
`re *+ss*+ire g$$iEif fa*[$f,gu$g$gi
`ii[+[if$i E ts iq$ir* * ilEEi3i3lsr;ai:
`+[f ]5+;Ef E+$sE+[*gE; ?*t$5Ese r+
`.E€
`f$ I gr[$ r I r}Eg rr$i i f ts + a[r F +f ;r+ r; r
`i"3$F$rIFs,
`p*u ti [&1; E irB; Ir5g€ *IFF$
`i g E [ ];; F; + E a s E ] 3 i x r a
`e.i $ $ rs q * 3€ 3sE€
`FgE [E ii$E i
`irrFI E flE +! $fg5E $F; g5$r$s
`F x$H s;Er; EE 3iIEs[fl3[i*iEFIiIgEFfl}[
`E$giifEg: i r ef fi$ I $1 $ t +FE [F [F +F *fl--
`
`9
`ax xt
`
`x! oa a-a D
`
`.
`
`(!
`t0
`
`+ s
`
`Ga b
`
`a
`
`(Dn
`l:o
`
`n o+ aE
`
`-+
`
`soo a
`a t
`
`oal
`o
`
`oco
`
`le
`-a
`
`
`
`o os?E
`
`- (D
`
`
`
`d+ 8oo
`
`o ao
`
`+ a E I
`
`'p-
`oE. 5(r
`h<
`o5
`=a
`o6
`19P
`oQ-
`ilx
`=qoS_
`s.x
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