Recursion Exercises

7.31

(Selection Sort) A selection sort searches an array looking for the smallest element. Then, the smallest element is swapped with the first element of the array. The process is repeated for the subarray beginning with the second element of the array. Each pass of the array results in one element being placed in its proper location. This sort performs comparably to the insertion sortfor an array of n elements, n 1 passes must be made, and for each subarray, n 1 comparisons must be made to find the smallest value. When the subarray being processed contains one element, the array is sorted. Write recursive function selectionSort to perform this algorithm.

7.32

(Palindromes) A palindrome is a string that is spelled the same way forward and backward. Some examples of palindromes are "radar," "able was i ere i saw elba" and (if blanks are ignored) "a man a plan a canal panama." Write a recursive function testPalindrome that returns true if the string stored in the array is a palindrome, and false otherwise. The function should ignore spaces and punctuation in the string.

7.33

(Linear Search) Modify the program in Fig. 7.19 to use recursive function linearSearch to perform a linear search of the array. The function should receive an integer array and the size of the array as arguments. If the search key is found, return the array subscript; otherwise, return 1.

7.34

(Eight Queens) Modify the Eight Queens program you created in Exercise 7.26 to solve the problem recursively.

7.35

(Print an array) Write a recursive function printArray that takes an array, a starting subscript and an ending subscript as arguments and returns nothing. The function should stop processing and return when the starting subscript equals the ending subscript.

7.36

(Print a string backward) Write a recursive function stringReverse that takes a character array containing a string and a starting subscript as arguments, prints the string backward and returns nothing. The function should stop processing and return when the terminating null character is encountered.

7.37

(Find the minimum value in an array) Write a recursive function recursiveMinimum that takes an integer array, a starting subscript and an ending subscript as arguments, and returns the smallest element of the array. The function should stop processing and return when the starting subscript equals the ending subscript.

Категории