Sorting part 4
Thông tin tài liệu:
Nội dung trích xuất từ tài liệu:
Sorting part 4336 Chapter 8. Sorting } }} You could, in principle, rearrange any number of additional arrays along withbrr, but this becomes wasteful as the number of such arrays becomes large. Thepreferred technique is to make use of an index table, as described in §8.4. visit website http://www.nr.com or call 1-800-872-7423 (North America only),or send email to trade@cup.cam.ac.uk (outside North America). readable files (including this one) to any servercomputer, is strictly prohibited. To order Numerical Recipes books,diskettes, or CDROMs Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machine- Copyright (C) 1988-1992 by Cambridge University Press.Programs Copyright (C) 1988-1992 by Numerical Recipes Software. Sample page from NUMERICAL RECIPES IN C: THE ART OF SCIENTIFIC COMPUTING (ISBN 0-521-43108-5)CITED REFERENCES AND FURTHER READING:Sedgewick, R. 1978, Communications of the ACM, vol. 21, pp. 847–857. [1]8.3 Heapsort While usually not quite as fast as Quicksort, Heapsort is one of our favoritesorting routines. It is a true “in-place” sort, requiring no auxiliary storage. It is anN log2 N process, not only on average, but also for the worst-case order of input data.In fact, its worst case is only 20 percent or so worse than its average running time. It is beyond our scope to give a complete exposition on the theory of Heapsort.We will mention the general principles, then let you refer to the references [1,2] , oranalyze the program yourself, if you want to understand the details. A set of N numbers ai , i = 1, . . . , N , is said to form a “heap” if it satisfiesthe relation aj/2 ≥ aj for 1 ≤ j/2 < j ≤ N (8.3.1)Here the division in j/2 means “integer divide,” i.e., is an exact integer or elseis rounded down to the closest integer. Definition (8.3.1) will make sense if youthink of the numbers ai as being arranged in a binary tree, with the top, “boss,”node being a1 , the two “underling” nodes being a2 and a3 , their four underlingnodes being a4 through a7 , etc. (See Figure 8.3.1.) In this form, a heap hasevery “supervisor” greater than or equal to its two “supervisees,” down throughthe levels of the hierarchy. If you have managed to rearrange your array into an order that forms a heap,then sorting it is very easy: You pull off the “top of the heap,” which will be thelargest element yet unsorted. Then you “promote” to the top of the heap its largestunderling. Then you promote its largest underling, and so on. The process is likewhat happens (or is supposed to happen) in a large corporation when the chairmanof the board retires. You then repeat the whole process by retiring the new chairmanof the board. Evidently the whole thing is an N log2 N process, since each retiringchairman leads to log2 N promotions of underlings. Well, how do you arrange the array into a heap in the first place? The answeris again a “sift-up” process like corporate promotion. Imagine that the corporationstarts out with N/2 employees on the production line, but with no supervisors. Nowa supervisor is hired to supervise two workers. If he is less capable than one ofhis workers, that one is promoted in his place, and he joins the production line. 8.3 Heapsort 337 a1 a2 a3 visit website http://www.nr.com or call 1-800-872-7423 (North America only),or send email to trade@cup.cam.ac.uk (outside North America). readable files (including this one) to any servercomputer, is strictly prohibited. To order Numerical Recipes books,diskettes, or CDROMs Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machine- Copyright (C) 1988-1992 by Cambridge University Press.Programs Copyright (C) 1988-1992 by Numerical Recipes Software. Sample page from NUMERICAL RECIPES IN C: THE ART OF SCIENTIFIC COMPUTING (ISBN 0-521-43108-5) a4 a5 a6 a7 a8 a9 a10 a11 a12Figure 8.3.1. Ordering implied by a “heap,” here of 12 elements. Elements connected by an upwardpath are sorted with respect to one another, but there is not necessarily any ordering among elementsrelated only “laterally.”After supervisors are hired, then supervisors of supervisors are hired, and so on upthe corporate ladder. Each employee is brought in at the top of the tree, but thenimmediately sifted down, with more capable workers promoted until their propercorporate level has been reached. In the ...
Tìm kiếm theo từ khóa liên quan:
tư liệu tài liệu lập trình thủ thuật lập trình ngôn ngữ C# ngôn ngữ lập trìnhGợi ý tài liệu liên quan:
-
Giáo trình Lập trình hướng đối tượng: Phần 2
154 trang 276 0 0 -
Bài thuyết trình Ngôn ngữ lập trình: Hệ điều hành Window Mobile
30 trang 267 0 0 -
Kỹ thuật lập trình trên Visual Basic 2005
148 trang 266 0 0 -
Giáo trình Lập trình cơ bản với C++: Phần 1
77 trang 232 0 0 -
Bài giảng Một số hướng nghiên cứu và ứng dụng - Lê Thanh Hương
13 trang 226 0 0 -
Giáo án Tin học lớp 11 (Trọn bộ cả năm)
125 trang 218 1 0 -
Thủ thuật giúp giải phóng dung lượng ổ cứng
4 trang 217 0 0 -
NGÂN HÀNG CÂU HỎI TRẮC NGHIỆM THIẾT KẾ WEB
8 trang 208 0 0 -
Bài tập lập trình Windows dùng C# - Bài thực hành
13 trang 186 0 0 -
Giáo trình Lập trình C căn bản: Phần 1
64 trang 170 0 0 -
Bài giảng Nhập môn về lập trình - Chương 1: Giới thiệu về máy tính và lập trình
30 trang 168 0 0 -
Thiết kế mạch logic bằng Verilog - HDL
45 trang 164 0 0 -
Hướng dẫn lập trình với Android part 4
5 trang 156 0 0 -
Báo cáo thực tập: Quản lý nhân sự & tiền lương
52 trang 154 0 0 -
Giáo trình nhập môn lập trình - Phần 22
48 trang 139 0 0 -
Giáo trình Lập trình C căn bản - HanoiAptech Computer Education Center
136 trang 134 0 0 -
LUẬN VĂN: ỨNG DỤNG NGÔN NGỮ LẬP TRÌNH RÀNG BUỘC COMET VÀO BÀI TOÁN LẬP THỜI KHÓA BIỂU
43 trang 132 0 0 -
142 trang 130 0 0
-
Bài giảng lập trình c căn bản - Trường Apptech - Chương 4
27 trang 118 0 0 -
Bài giảng Phương pháp lập trình: Chương 9 - GV. Từ Thị Xuân Hiền
36 trang 112 0 0