# lec8 - Click to edit Master subtitle style Sundar B CSIS...

This preview shows pages 1–4. 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 is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: Click to edit Master subtitle style 2/7/11 Sundar B. CSIS, Recursion and Iteration Divide-and-Conquer- Recursive vs. Iterative Implementations Conversion of Recursive procedures to Iterative procedures 11 2/7/11 Sundar B. 2/7/11 Sundar B. recursive vs. iterative Algorithms – Example 2 Problem: Sort, in-place, a list of N elements. Assume list is stored as an array A[0], A[1], … A[n-1] Design : Divide-And-Conquer Sub-problem: Sort a list of size N-1 ( A[0], A[1],…A[n-2] ) Combination: Insert A[n-1] in order (i.e. in the right position) Termination : Stop when size is <= 1. 2/7/11 22 Sundar B. { 6 A 6 B C 2 B -5 A-4 2 F 7 -B 6 9 2 -3 8 1 C A E C 0 7 8 2 8 } Sort(A, n) { 8 2 3 E 4 6 3 -5 D 1 5 -4 5 2 3 -8 3 E F -2 3 E F 6 0 D 2 F 2 5 2 } Sort(A,n-1) { E 0 2 7 D A B -9 5 1 E -4 1 D -B 2 3 7 -9 C 9 8 F 4 0 F B 6 C } Sort(A,n-2) { 0 E 6 3 C 5 0 7 -A 9 1 0 -4 7 D A -8 8 6 -1 8 3 F 2 1 0 8 4 E D 0 } Sort (A,1) … insertInOrd(A[n- 1],A,n-1) insertInOrd(A[n- 2],A,n-2) insertInOrd(A[1] ,A,1) 2/7/11 Sundar B. recursive vs. iterative Algorithms – Example 2 Recursive Algorithm // Precondition: A is an array of size n // Postcondition: A is ordered in place insertSort(A, n) { if (n>1) { insertSort(A,n-1);...
View Full Document

## This note was uploaded on 02/07/2011 for the course CS 123 taught by Professor Murali during the Spring '11 term at Birla Institute of Technology & Science, Pilani - Hyderabad.

### Page1 / 11

lec8 - Click to edit Master subtitle style Sundar B CSIS...

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

View Full Document
Ask a homework question - tutors are online