368 Arrays Chapter 7 RECURSION EXERCISES 7.29 (Selection (Post office web site)
Saturday, April 21st, 2007368 Arrays Chapter 7 RECURSION EXERCISES 7.29 (Selection Sort) A selection sort searches an array looking for the smallest element in the array, then swaps that element with the first element of the array. The process is repeated for the sub- array beginning with the second element. Each pass of the array places one element in its proper location. For 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 method selectionSortto perform this algorithm. 7.30 (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 method testPalindrome that returns boolean value true if the string stored in the array is a palindrome and false otherwise. The method should ignore spaces and punctuation in the string. [Hint: Use String method toCharArray, which takes no arguments, to get a char array containing the characters in the String. Then, pass the array to method testPalindrome.] 7.31 (Linear Search) Modify Figure 7.12 to use recursive method linearSearch to perform a linear search of the array. The method should receive an integer array, the array size and the search key as arguments. If the search key is found, return the array subscript; otherwise, return 1. 7.32 (Binary Search) Modify the program of Figure 7.13 to use a recursive method binary- Search to perform the binary search of the array. The method should receive an integer array and the starting subscript and ending subscript as arguments. If the search key is found, return the array subscript; otherwise, return 1. 7.33 (Eight Queens) Modify the Eight Queens program you created in Exercise 7.24 to solve the problem recursively. 7.34 (Print an array) Write a recursive method printArray that takes an array of int values and the size of the array as arguments and returns nothing. The method should stop processing and return when it receives an array of size 0. 7.35 (Print a string backward) Write a recursive method stringReversethat takes a character array containing a string as an argument, prints the string backward and returns nothing. 7.36 (Find the minimum value in an array) Write a recursive method recursiveMinimumthat takes an integer array and the array size as arguments and returns the smallest element of the array. The method should stop processing and return when it receives an array of one element. 7.37 (Quicksort) In the examples and exercises of this chapter, we discussed the sorting techniques bubble sort, bucket sort and selection sort. We now present the recursive sorting technique called Quicksort. The basic algorithm for a single-subscripted array of values is as follows: a) Partitioning Step: Take the first element of the unsorted array and determine its final location in the sorted array (i.e., all values to the left of the element in the array are less than the element, and all values to the right of the element in the array are greater than the element). We now have one element in its proper location and two unsorted subarrays. b) Recursive Step: Perform step 1 on each unsorted subarray. Each time step 1 is performed on a subarray, another element is placed in its final location of the sorted array and two unsorted subarrays are created. When a subarray consists of one element, it must be sorted therefore, that element is in its final location. The basic algorithm seems simple enough, but how do we determine the final position of the first element of each subarray? As an example, consider the following set of values (the element in bold is the partitioning element it will be placed in its final location in the sorted array): 37 2 6 4 89 8 10 12 68 45 Copyright 1992 2002 by Deitel & Associates, Inc. All Rights Reserved. 7/3/01
Note: In case you are looking for affordable and reliable webhost to host and run your business application check Vision php5 hosting services