You are on page 1of 3
172 @ Introduction to Recursive Programming Listing 6.1 Function that determines whether a list is sorted in ascending order. dof is List_sorted(a) n= len(a) send return True else: return (is list_sorted(a(0:n // 21) and an // 2 - 1] <= ala // 2) and is List_sorted(atn // 2:a))) of half the size of the original's (the method would invoke itself only once). However, many authors consider that the term should only be used when the solution relies on breaking up a problem into two or more subproblems. This book adopts this last convention. Thus, the methods that invoke themselves once, dividing the size of the problem by two, have been covered in earlier chapters (mostly in Chapter 5) ‘The following, sections describe classical recursive divide and conquer algorithms. 6.1_IS A LIST SORTED IN ASCENDING ORDER? In this problem the input is a list a of n items that can be sorted through the < operator (or some function that allows us to compare elements by implementing a total order binary relation), and the output is a Boolean value that can be expressed as nd Fla) = Alas avr) (6.1) ‘Thus, the result is True when the elements of the list appear in (non- strictly) increasing or nondecreasing order ‘The size of this problem is the number of elements in the list. If the list contains one element the result is trivially True. In addition, we can consider that an empty list is also ordered. The problem can be solved by a linear-recursive method that reduces the size of the problem one unit in the decomposition stage. However, wwe will present a solution that decomposes the problem by dividing the input list in two halves, and which leads to the following partition of Multiple Recursion I: Divide and Conquer m 173 (6.1): Fla) = (a0 < a1) A A (ajnprj-2 ¥ @nj2}-1). ‘Subproblem 1 A (Ajnjaj-1 $ njaj) © (a[nj2j $ OInja}er) A A (an-2 $ On-2) Subproiemn 2 Cleatly, fa list is sorted in ascending order then both halves must also be sorted in ascending order. Thus, the method can invoke itself twice with the two corresponding sublists, and perform a logical AND operation with their result. Finally, the combination of the results of the subprob- Jems needs an additional step. In particular, the last element of the first sublist (ajq/2}-1) must be less than or equal to the first clement of the second sublist (ajqj2}). This condition also requires another AND oper- ation in the recursive case. Listing 6.1 shows a possible implementation of the method, which runs in O(n) time, since its cost is characterized by 1 ifn<1, Ton) = fers +1 ifn>1 6.2_ SORTING Sorting a general list (or array, sequence, etc.) is one of the most studied problems in computer science. It can be solved by numerous algorithms that can be used to introduce essential concepts related to computational complexity and runtime analysis, algorithm design paradigms, or data structures. The sorting algorithms covered in this chapter will assume that the input to the problem is a list a of 1 real numbers. ‘The out- put is a rearrangement (or permutation) of the elements that provides another list a’ where af < a!,,, for i= 0,....n~2 (< can be thought of as Boolean function that allows us to determine if an element precedes another, implementing a total order binary relation), The choice of real numbers for the type of the elements of the list implies that the algo- rithms need to carry out comparisons through <. It can be shown that any algorithm that solves this problem requires §¢(rlogn) comparisons (ie., at least on the order of nlogn decisions using <). Lastly, there exist algorithms for sorting lists that do not need to compare elements, which require (11) operations. However, the elements of the list must satisfy certain conditions in order to apply them. For example, the “counting 174 @ Introduction to Recursive Programming nerge_sort merge sort nerge_sort > merge « Figure 6.1 Merge sort algorithm, sort” algorithm sorts integers that belong to a sinall interval [0, F] (see Exercise 5.3) 6.2.1 The merge sort algorithm ‘The merge sort algorithm is one of the most extensively used examples to show the potential of the divide and conquer approach. While many sorting algorithms run in O(n?) time in the worst case (see Section 4.4.1 and Exercise 4.17), the merge sort algorithm runs in @(nlogn). In other words, its efficiency is optimal from a computational complexity perspec~ tive, ‘The size of the problem is the length of the list (n). The smallest instances occur when n <1, where the algorithm can simply return the input list. In the recursive case the algorithm decomposes the problem by

You might also like