The document outlines a tutorial for a Data Structure & Algorithms course focusing on recursion. It includes exercises on writing recursive functions for finding maximum values in arrays, counting and summing digits of integers, calculating binomial coefficients, and implementing the Quick Sort algorithm. Each exercise provides specific tasks and examples to enhance understanding of recursive programming concepts.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0 ratings0% found this document useful (0 votes)
14 views
Tutorial Week13
The document outlines a tutorial for a Data Structure & Algorithms course focusing on recursion. It includes exercises on writing recursive functions for finding maximum values in arrays, counting and summing digits of integers, calculating binomial coefficients, and implementing the Quick Sort algorithm. Each exercise provides specific tasks and examples to enhance understanding of recursive programming concepts.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2
Data Structure & Algorithms 1 (DSA1)
Tutorial (week 13) - Rucursion
2024 - 2025
05 Januray 2025
OBJECTIVES Learning recursive functions.
Exercise 1: Maximum in an Array
Write a recursive function, MaxValue, to find the maximum value in an array of integers.
Exercise 2: Counting, Summing, and Displaying Digits
1. Write a recursive function to count the digits of a positive integer. 2. Write a value-returning recursive function called DigitSum that computes the sum of the digits in a given positive integer argument. For example, if the argument is 12345, then the function returns 1 + 2 + 3 + 4 + 5 = 15. 3. Write a value-returning recursive function that utilizes the DigitSum function from question (2) to compute the single digit to which the integer argument’s digits ultimately sum. Trace the execution of your function with an argument of your choice. For instance, given the argument 999, the DigitSum result would be 9 + 9 + 9 = 27, but the single-digit sum would then be 2 + 7 = 9. 4. Construct a recursive function to display the digits of any positive integer in reverse order.
Exercise 3: Binomial Coefficients
In probability and statistics applications, it is often necessary to determine the total possible number of certain outcome combinations. For example, you may need to know the number of possible committees of 3 people that can be formed from a 12-person department, etc. The binomial coefficient, often referred to as ”n choose k,” provides the number of combinations of k things that can be formed from a set of n things. The binomial coefficient is mathematically written as: nk , referred to as ”n choose k.” Binomial coefficients can be defined recursively:
n n−1 n−1 = + k k−1 k
n n = =1 n 0 Write a recursive function that will compute and return the value of the binomial coefficient.
1 Figure 1: Quick sort algorithm
Exercise 4: Quick Sort
In general, Quicksort can efficiently sort a list of data elements using a ”divide and conquer” strategy. If given a large stack of final exams to sort by name, you might follow this approach: Pick a splitting value—let’s say, L—and divide the stack of tests into two piles, A–L and M–Z. Note that the two piles do not necessarily contain the same number of tests. Take the first pile and subdivide it into two piles, A–F and G–L. The A–F pile can be further broken down into A–C and D–F. This division process continues until the piles are small enough to be easily sorted by hand. The same process is applied to the M–Z pile. Eventually, all of the small sorted piles can be stacked on top of each other to produce a sorted set of tests. The key process in QuickSort is a partition(). The goal of partitioning is to place the pivot (any element can be chosen as a pivot) at its correct position in the sorted array and put all smaller elements to the left of the pivot and all greater elements to the right of the pivot. Partitioning is done recursively on each side of the pivot after the pivot is placed in its correct position, and this finally sorts the array. There are various choices for picking pivots (first, last, middle, or random element). The array partitioning may be performed with a simple method: start from the leftmost element and keep track of the index of smaller (or equal) elements as i. While traversing, if we find an element smaller than the pivot, we swap the current element with array[i]. Otherwise, we ignore the current element. The partition function should return the right position of the pivot. - Develop an algorithm to sort an array using Quick sort .