Sams Teach Yourself Data Structures and Algorithms in 24 Hours by Robert Lafore
Author:Robert Lafore [Lafore, Robert]
Language: eng
Format: epub, pdf
Tags: Programming Languages, Computers, General, C, Databases
ISBN: 9780672316333
Google: E2s_AQAAIAAJ
Amazon: 0672316331
Publisher: Sams
Published: 1999-05-15T00:00:00+00:00
16 72316331 Ch12 10/31/02 7:15 AM Page 246
246
Hour 12
First the 1-element ranges 0–0 and 1–1 are merged into the 2-element range 0–1. Then range 0–1 is merged with the 1-element range 2–2. This creates the 3-element range 0–2.
It’s merged with the 3-element range 3–5. The process continues until the array is sorted.
Notice that in mergesort we don’t merge two separate arrays into a third one, as we demonstrated in the merge.cpp program. Instead, we merge parts of a single array into itself.
You might wonder where all these subarrays are located in memory. In the algorithm, a workspace array of the same size as the original array is created. The subarrays are stored in sections of the workspace array. This means that subarrays in the original array are copied to appropriate places in the workspace array. After each merge, the workspace array is copied back into the original array.
The mergeSort Workshop Applet
All this is easier to appreciate when you see it happening before your very eyes. Start up the mergeSort Workshop applet. Repeatedly pressing the Step button will execute mergeSort step by step. Figure 12.8 shows what it looks like after the first three presses.
FIGURE 12.8
The mergeSort
Workshop applet.
The Lower and Upper arrows show the range currently being considered by the algorithm, and the Mid arrow shows the middle part of the range. The range starts as the entire array and then is halved each time the mergeSort() member function calls itself.
When the range is one element, mergeSort() returns immediately; that’s the base case.
Otherwise, the two subarrays are merged. The applet provides messages, such as Entering mergeSort: 0-5, to tell you what it’s doing and the range it’s operating on.
TeamLRN
16 72316331 Ch12 10/31/02 7:15 AM Page 247
Applied Recursion
247
Many steps involve the mergeSort() member function calling itself or returning.
Comparisons and copies are performed only during the merge process, when you’ll see messages like Merged 0-0 and 1-1 into workspace. You can’t see the merge happening because the workspace isn’t shown. However, you can see the result when the appropriate section of the workspace is copied back into the original (visible) array: The bars in the specified range will appear in sorted order.
First, the first two bars will be sorted, then the first three bars, then the two bars in the range 3–4, then the three bars in the range 3–5, then the six bars in the range 0–5, and so on, corresponding to the sequence shown in Figure 12.7. Eventually all the bars will be sorted.
You can cause the algorithm to run continuously by pressing the Run button. You can stop this process at any time by pressing Step, single-step as many times as you want, and resume running by pressing Run again.
As in the other sorting Workshop applets, pressing New resets the array with a new group of unsorted bars, and toggles between random and inverse arrangements. The Size button toggles between 12 bars and 100 bars.
It’s especially instructive to watch the algorithm run with 100 inversely sorted bars. The resulting patterns
Download
Sams Teach Yourself Data Structures and Algorithms in 24 Hours by Robert Lafore.pdf
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.
Coding Theory | Localization |
Logic | Object-Oriented Design |
Performance Optimization | Quality Control |
Reengineering | Robohelp |
Software Development | Software Reuse |
Structured Design | Testing |
Tools | UML |
Deep Learning with Python by François Chollet(12563)
Hello! Python by Anthony Briggs(9911)
OCA Java SE 8 Programmer I Certification Guide by Mala Gupta(9794)
The Mikado Method by Ola Ellnestam Daniel Brolund(9775)
Dependency Injection in .NET by Mark Seemann(9335)
Algorithms of the Intelligent Web by Haralambos Marmanis;Dmitry Babenko(8292)
Test-Driven iOS Development with Swift 4 by Dominik Hauser(7758)
Grails in Action by Glen Smith Peter Ledbrook(7693)
The Well-Grounded Java Developer by Benjamin J. Evans Martijn Verburg(7557)
Becoming a Dynamics 365 Finance and Supply Chain Solution Architect by Brent Dawson(7016)
Microservices with Go by Alexander Shuiskov(6783)
Practical Design Patterns for Java Developers by Miroslav Wengner(6696)
Test Automation Engineering Handbook by Manikandan Sambamurthy(6636)
Secrets of the JavaScript Ninja by John Resig Bear Bibeault(6409)
Angular Projects - Third Edition by Aristeidis Bampakos(6045)
The Art of Crafting User Stories by The Art of Crafting User Stories(5575)
NetSuite for Consultants - Second Edition by Peter Ries(5508)
Demystifying Cryptography with OpenSSL 3.0 by Alexei Khlebnikov(5309)
Kotlin in Action by Dmitry Jemerov(5061)
