2. 8.2 Define an algorithm and relate it to problem solving. Define three construct and describe their use in algorithms. Describe UML diagrams and pseudocode and how they areused in algorithms. List basic algorithms and their applications. Describe the concept of sorting and understand themechanisms behind three primitive sorting algorithms. Describe the concept of searching and understand themechanisms behind two common searching algorithms. Define subalgorithms and their relations to algorithms. Distinguish between iterative and recursive algorithmsObjectivesObjectivesAfter studying this chapter, the student should be ableAfter studying this chapter, the student should be ableto:to:
3. 8.38-1 CONCEPT8-1 CONCEPTIn this section we informally define anIn this section we informally define an algorithmalgorithm andandelaborate on the concept using an example.elaborate on the concept using an example.
4. 8.4Informal definitionAn informal definition of an algorithm is:Algorithm: a step-by-step method for solving aproblem or doing a task.iFigure 8.1 Informal definition of an algorithm used in a computer
5. 8.5ExampleWe want to develop an algorithm for finding the largestinteger among a list of positive integers. The algorithmshould find the largest integer among a list of any values (forexample 5, 1000, 10,000, 1,000,000). The algorithm shouldbe general and not depend on the number of integers.To solve this problem, we need an intuitive approach.First use a small number of integers (for example, five), thenextend the solution to any number of integers.Figure 8.2 shows one way to solve this problem. Wecall the algorithm FindLargest. Each algorithm has a name todistinguish it from other algorithms. The algorithm receivesa list of five integers as input and gives the largest integer asoutput.
6. 8.6Figure 8.2 Finding the largest integer among five integers
7. 8.7Defining actionsFigure 8.2 does not show what should be done in each step.We can modify the figure to show more details.Figure 8.3 Defining actions in FindLargest algorithm
8. 8.8RefinementThis algorithm needs refinement to be acceptable to theprogramming community. There are two problems. First, theaction in the first step is different than those for the othersteps. Second, the wording is not the same in steps 2 to 5.We can easily redefine the algorithm to remove these twoinconveniences by changing the wording in steps 2 to 5 to“If the current integer is greater than Largest, set Largest tothe current integer.” The reason that the first step is differentthan the other steps is because Largest is not initialized. Ifwe initialize Largest to −∞ (minus infinity), then the firststep can be the same as the other steps, so we add a new step,calling it step 0 to show that it should be done beforeprocessing any integers.
9. 8.9 Figure 8.4 FindLargest refined
10. 8.10GeneralizationIs it possible to generalize the algorithm? We want to findthe largest of n positive integers, where n can be 1000,1,000,000, or more. Of course, we can follow Figure 8.4 andrepeat each step. But if we change the algorithm to aprogram, then we need to actually type the actions for nsteps!There is a better way to do this. We can tell the computer torepeat the steps n times. We now include this feature in ourpictorial algorithm (Figure 8.5).
11. 8.11Figure 8.5 Generalization of FindLargest
12. 8.128-2 THREE CONSTRUCTS8-2 THREE CONSTRUCTSComputer scientists have definedComputer scientists have defined three constructsthree constructs for afor astructured program or algorithm. The idea is that astructured program or algorithm. The idea is that aprogram must be made of a combination of only theseprogram must be made of a combination of only thesethree constructs:three constructs: sequencesequence,, decisiondecision (selection) and(selection) andrepetitionrepetition (Figure 8.6). It has been proven there is no(Figure 8.6). It has been proven there is noneed for any other constructs. Using only theseneed for any other constructs. Using only theseconstructs makes a program or an algorithm easy toconstructs makes a program or an algorithm easy tounderstand, debug or change.understand, debug or change.
13. 8.13Figure 8.6 Three constructs
14. 8.14SequenceThe first construct is called the sequence. An algorithm, andeventually a program, is a sequence of instructions, whichcan be a simple instruction or either of the other twoconstructs.DecisionSome problems cannot be solved with only a sequence ofsimple instructions. Sometimes we need to test a condition.If the result of testing is true, we follow a sequence ofinstructions: if it is false, we follow a different sequence ofinstructions. This is called the decision (selection) construct.
15. 8.15RepetitionIn some problems, the same sequence of instructions must berepeated. We handle this with the repetition or loopconstruct. Finding the largest integer among a set of integerscan use a construct of this kind.
16. 8.168-3 ALGORITHM REPRESENTATION8-3 ALGORITHM REPRESENTATIONSo far, we have used figures to convey the concept of anSo far, we have used figures to convey the concept of analgorithm. During the last few decades, tools have beenalgorithm. During the last few decades, tools have beendesigned for this purpose. Two of these tools,designed for this purpose. Two of these tools, UMLUML andandpseudocodepseudocode, are presented here., are presented here.
17. 8.17UMLUnified Modeling Language (UML) is a pictorialrepresentation of an algorithm. It hides all the details of analgorithm in an attempt to give the “big picture” and to showhow the algorithm flows from beginning to end. UML iscovered in detail in Appendix B. Here we show only how thethree constructs are represented using UML (Figure 8.7).
18. 8.18Figure 8.7 UML for three constructs
19. 8.19PseudocodePseudocode is an English-language-like representation of analgorithm. There is no standard for pseudocode—somepeople use a lot of detail, others use less. Some use a codethat is close to English, while others use a syntax like thePascal programming language.Pseudocode is covered in detail in Appendix C. Here weshow only how the three constructs can be represented bypseudocode (Figure 8.8).
20. 8.20Figure 8.8 Pseudocode for three constructs
21. 8.21Example 8.1Write an algorithm in pseudocode that finds the sum of twoWrite an algorithm in pseudocode that finds the sum of twointegers.integers.
22. 8.22Example 8.2Write an algorithm to change a numeric grade to a pass/no passWrite an algorithm to change a numeric grade to a pass/no passgrade.grade.
23. 8.23Example 8.3Write an algorithm to change a numeric grade (integer) to a letterWrite an algorithm to change a numeric grade (integer) to a lettergrade.grade.
24. 8.24Example 8.4Write an algorithm to find the largest of a set of integers. We doWrite an algorithm to find the largest of a set of integers. We donot know the number of integers.not know the number of integers.
25. 8.25Example 8.5Write an algorithm to find the largest of the first 1000 integers inWrite an algorithm to find the largest of the first 1000 integers ina set of integers.a set of integers.
26. 8.268-4 A MORE FORMAL DEFINITION8-4 A MORE FORMAL DEFINITIONNow that we have discussed the concept of an algorithmNow that we have discussed the concept of an algorithmand shown its representation, here is a more formaland shown its representation, here is a more formaldefinition.definition.Algorithm:An ordered set of unambiguous steps that produces aresult and terminates in a finite time.i
27. 8.27Ordered setAn algorithm must be a well-defined, ordered set ofinstructions.Unambiguous stepsEach step in an algorithm must be clearly andunambiguously defined. If one step is to add two integers,we must define both “integers” as well as the “add”operation: we cannot for example use the same symbol tomean addition in one place and multiplication somewhereelse.
28. 8.28Produce a resultAn algorithm must produce a result, otherwise it is useless.The result can be data returned to the calling algorithm, orsome other effect (for example, printing).Terminate in a finite timeAn algorithm must terminate (halt). If it does not (that is, ithas an infinite loop), we have not created an algorithm. InChapter 17 we will discuss solvable and unsolvableproblems, and we will see that a solvable problem has asolution in the form of an algorithm that terminates.
29. 8.298-5 BASIC ALGORITHMS8-5 BASIC ALGORITHMSSeveral algorithms are used in computer science soSeveral algorithms are used in computer science soprevalently that they are considered “basic”. We discussprevalently that they are considered “basic”. We discussthe most common here. This discussion is very general:the most common here. This discussion is very general:implementation depends on the language.implementation depends on the language.
30. 8.30SummationOne commonly used algorithm in computer science issummation. We can add two or three integers very easily,but how can we add many integers? The solution is simple:we use the add operator in a loop (Figure 8.9).A summation algorithm has three logical parts:1. Initialization of the sum at the beginning.2. The loop, which in each iteration adds a new integer to thesum.3. Return of the result after exiting from the loop.
31. 8.31Figure 8.9 Summation algorithm
32. 8.32ProductAnother common algorithm is finding the product of a list ofintegers. The solution is simple: use the multiplicationoperator in a loop (Figure 8.10).A product algorithm has three logical parts:1. Initialization of the product at the beginning.2. The loop, which in each iteration multiplies a new integerwith the product.3. Return of the result after exiting from the loop.
33. 8.33Figure 8.10 Product algorithm
34. 8.34Smallest and largestWe discussed the algorithm for finding the largest among alist of integers at the beginning of this chapter. The idea wasto write a decision construct to find the larger of twointegers. If we put this construct in a loop, we can find thelargest of a list of integers.Finding the smallest integer among a list of integers issimilar, with two minor differences. First, we use a decisionconstruct to find the smaller of two integers. Second, weinitialize with a very large integer instead of a very smallone.
35. 8.35SortingOne of the most common applications in computer science issorting, which is the process by which data is arrangedaccording to its values. People are surrounded by data. If thedata was not ordered, it would take hours and hours to find asingle piece of information. Imagine the difficulty of findingsomeone’s telephone number in a telephone book that is notordered.In this section, we introduce three sorting algorithms:selection sort, bubble sort and insertion sort. These threesorting algorithms are the foundation of faster sortingalgorithms used in computer science today.
36. 8.36Selection sortsIn a selection sort, the list to be sorted is divided into twosublists—sorted and unsorted—which are separated by animaginary wall. We find the smallest element from theunsorted sublist and swap it with the element at thebeginning of the unsorted sublist. After each selection andswap, the imaginary wall between the two sublists movesone element ahead.Figure 8.11 Selection sort
37. 8.37Figure 8.12 Example of selection sort
38. 8.38Figure 8.13 Selection sort algorithm
39. 8.39Bubble sortsIn the bubble sort method, the list to be sorted is also dividedinto two sublists—sorted and unsorted. The smallest elementis bubbled up from the unsorted sublist and moved to thesorted sublist. After the smallest element has been moved tothe sorted list, the wall moves one element ahead.Figure 8.14 Bubble sort
40. 8.40Figure 8.15 Example of bubble sort
41. 8.41Insertion sortsThe insertion sort algorithm is one of the most commonsorting techniques, and it is often used by card players. Eachcard a player picks up is inserted into the proper place intheir hand of cards to maintain a particular sequence.Figure 8.16 Insertion sort
42. 8.42Figure 8.17 Example of insertion sort
43. 8.43SearchingAnother common algorithm in computer science issearching, which is the process of finding the location of atarget among a list of objects. In the case of a list, searchingmeans that given a value, we want to find the location of thefirst element in the list that contains that value. There are twobasic searches for lists: sequential search and binarysearch. Sequential search can be used to locate an item inany list, whereas binary search requires the list first to besorted.
44. 8.44Sequential searchSequential search is used if the list to be searched is notordered. Generally, we use this technique only for small lists,or lists that are not searched often. In other cases, the bestapproach is to first sort the list and then search it using thebinary search discussed later.In a sequential search, we start searching for the target fromthe beginning of the list. We continue until we either find thetarget or reach the end of the list.
45. 8.45Figure 8.18 An example of a sequential search
46. 8.46Binary searchThe sequential search algorithm is very slow. If we have alist of a million elements, we must do a million comparisonsin the worst case. If the list is not sorted, this is the onlysolution. If the list is sorted, however, we can use a moreefficient algorithm called binary search. Generally speaking,programmers use a binary search when a list is large.A binary search starts by testing the data in theelement at the middle of the list. This determines whether thetarget is in the first half or the second half of the list. If it isin the first half, there is no need to further check the secondhalf. If it is in the second half, there is no need to furthercheck the first half. In other words, we eliminate half the listfrom further consideration.
47. 8.47Figure 8.19 Example of a binary search
48. 8.488-6 SUBALGORITHMS8-6 SUBALGORITHMSThe three programming constructs described in SectionThe three programming constructs described in Section8.2 allow us to create an algorithm for any solvable8.2 allow us to create an algorithm for any solvableproblem. The principles of structured programming,problem. The principles of structured programming,however, require that an algorithm be broken into smallhowever, require that an algorithm be broken into smallunits calledunits called subalgorithmssubalgorithms. Each subalgorithm is in. Each subalgorithm is inturn divided into smaller subalgorithms. A goodturn divided into smaller subalgorithms. A goodexample is the algorithm for the selection sort in Figureexample is the algorithm for the selection sort in Figure22.214.171.124.
49. 8.49 Figure 8.20 Concept of a subalgorithm
50. 8.50Structure chartAnother tool programmers use is the structure chart. Astructure chart is a high level design tool that shows therelationship between algorithms and subalgorithms. It is usedmainly at the design level rather than at the programminglevel. We briefly discuss the structure chart in Appendix D.
51. 8.518-7 RECURSION8-7 RECURSIONIn general, there are two approaches to writingIn general, there are two approaches to writingalgorithms for solving a problem. One usesalgorithms for solving a problem. One uses iterationiteration,,the other usesthe other uses recursionrecursion. Recursion is a process in. Recursion is a process inwhich an algorithm calls itself.which an algorithm calls itself.
52. 8.52Iterative definitionTo study a simple example, consider the calculation of afactorial. The factorial of a integer is the product of theintegral values from 1 to the integer. The definition isiterative (Figure 8.21). An algorithm is iterative wheneverthe definition does not involve the algorithm itself.Figure 8.21 Iterative definition of factorial
53. 8.53Recursive definitionAn algorithm is defined recursively whenever the algorithmappears within the definition itself. For example, the factorialfunction can be defined recursively as shown in Figure 8.22.Figure 8.22 Recursive definition of factorial
54. 8.54Figure 8.23 Tracing the recursive solution to the factorial problem
55. 8.55Iterative solutionThis solution usually involves a loop.
56. 8.56Recursive solutionThe recursive solution does not need a loop, as the recursionconcept itself involves repetition.