Algo3

# Algo3 - More recursive algorithms 1 Computing factorial...

This preview shows pages 1–8. Sign up to view the full content.

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: More recursive algorithms 1 Computing factorial Input : n // factorial function argument Output : // n! Factorial (n) if n == 1 return 1 endif return n* Factorial (n-1) endMinimum 2 Finding minimum in array Input : A // array of integers n // number of elements in array (array size) Output : Min // value of element with smallest value Minimum (A, n) // non-recursive version Min = A[0] // initialize min. as first element for i = 1 to n-1 // look at remaining elements if A[i] < Min then Min = A[i] endif endfor return Min endMinimum Input : A // array of integers n // number of elements in array (array size) i // consider elements from i onwards Output : Min // value of element with smallest value MinR (A, n, i ) // recursive version if i0 == n-1 // last element return A[i0] endif tmp = MinR (A, n, i0+1) // min(A[i +1], …, A[n-1]) // min(A[i ], …, A[n-1]) is min(A[i ], tmp) if A[i0] < tmp return A[i ] else return tmp endMinR 3 Recursive sorting: merge sort Input : A // array of integers l // the index of the first element of A to be considered r // the index of the last element of A to be considered Output : B // array with elements of A in ascending order MergeSortR (A, l, r) // initial call MergeSortR (A, 0, n-1) if l == r // termination condition (subarray with 1 el.) B[0] = A[l] return B endif m = (l+r)/2 // midpoint Bl = MergeSortR (A, l, m) // sort left subarray Br = MergeSortR (A, m+1, r) // sort right subarray B = MergeSorted (Bl, Br) // merge subarrays return B // return merged subarrays endMergeSortR 4 Merge sort trace Input : A // array of integers l // the index of the first element of A to be considered r // the index of the last element of A to be considered Output : B // array with elements of A in ascending order MergeSortR (A, l, r) // initial call MergeSortR (A, 0, n-1) if l == r // termination condition (subarray with 1 el.) B[0] = A[l] return B endif m = (l+r)/2 // midpoint Bl = MergeSortR (A, l, m) // sort left subarray Br = MergeSortR (A, m+1, r) // sort right subarray B = MergeSorted (Bl, Br) // merge subarrays return B // return merged subarrays endMergeSortR 5 Call Return MergeSortR (A, 0, 5) MergeSortR (A, 0, 2) MergeSortR (A, 0, 1) MergeSortR (A, 0, 0) {9} 0 1 2 3 4 5 9 1 0 8 2 4 A: Merge sort trace Input : A // array of integers l // the index of the first element of A to be considered r // the index of the last element of A to be considered Output : B // array with elements of A in ascending order MergeSortR (A, l, r) // initial call MergeSortR (A, 0, n-1) if l == r // termination condition (subarray with 1 el.) B[0] = A[l] return B endif m = (l+r)/2 // midpoint Bl = MergeSortR (A, l, m) // sort left subarray Br = MergeSortR (A, m+1, r) // sort right subarray B = MergeSorted (Bl, Br) // merge subarrays return B // return merged subarrays endMergeSortR 6 Call Return MergeSortR (A, 0, 5) MergeSortR (A, 0, 2) MergeSortR (A, 0, 1) MergeSortR (A, 0, 0) {9} MergeSortR (A, 1, 1) {1} 0 1 2 3 4 5 9 1 0 8 2 4 A: Merge sort trace...
View Full Document

## This note was uploaded on 02/27/2012 for the course CS 177 taught by Professor Staff during the Spring '08 term at Purdue.

### Page1 / 28

Algo3 - More recursive algorithms 1 Computing factorial...

This preview shows document pages 1 - 8. Sign up to view the full document.

View Full Document
Ask a homework question - tutors are online