# CS1020 Week 13: 16 th April 2015. Contents  Sit-in Lab #4 2.

## Presentation on theme: "CS1020 Week 13: 16 th April 2015. Contents  Sit-in Lab #4 2."— Presentation transcript:

CS1020 Week 13: 16 th April 2015

Contents  Sit-in Lab #4 2

Sit-in Lab #4 Set A – Word Scramble Set B – Numeric Sequence 3

Objective  Recursion Understanding the principles of recursion Understanding the basic requirements of a problem Formulating a recursive solution Figuring out the optimized way of solving a problem 4

Problem Statement  Word Scramble: Find a word in a character array  Input 1.A character array 2.A word  Objectives 1.Finding if the characters of the word are present in the character array as substring  All the letters of the word must be in consecutive positions in the array 2.Finding if the characters of the word are present in the character array as subsequence  All the letters of the word must be in same order in the array  Things to learn: 1.Using recursion 2.Using recursion in combination with loop Set A: Word Scramble 5

Search for the starting position of the word first Recursive Logic: Substring badtal k k re Character Array talk Word to search b b a a d d t t a a l l r r e e Recursively search for the next characters till end of array/word Set A: Word Scramble 6

Recursive Logic: Substring adtalre read Search for the starting position of the word first e e a a d d i i n n g g r r e e Recursively search for the next characters till end of array/word r r Search again for the starting position of the word in the rest of the array Set A: Word Scramble Character Array Word to search 7

Recursive Logic: Subsequence badtal k k re red Search for the starting position of the word first b b a a d d t t a a l l r r e e Recursively search for the next characters till end of array/word If a character mismatches, ignore it and continue to search in the remaining array Character Array Word to search Set A: Word Scramble 8

Program Flow findWordInArray 1.Find the position of first character of the word in array  i 2.Call a recursive method to find word in the array given the 1 st position  findWordInArrayRec(ar ray,word,i,1) 3.If returns True  Print i 4.Otherwise Continue from step 1 for remaining array findWordInArrayRec 1.Match array[i+1] and word[k] 2.If matches Call findWordInArrayRec( array,word,i+1,k+1) 3.Otherwise return false 4.Base Case: End of word has been reached  return True findWordInArrayRec 1.Match array[i+1] and word[k] 2.If matches Call findWordInArrayRec( array,word,i+1,k+1) 3.Otherwise Continue from step 1 for remaining array 4.Base Case: End of word has been reached  return True Substring search Subsequence search Set A: Word Scramble 9

Problem Statement  Numeric Sequence: Finding maximum value from a sequence  Input 1.An array integers  Objectives 1.Whether the array is sorted in increasing sequence or not  Determine whether an array is sorted, if yes, then print the maximum element 2.Find the maximum value from a sequence which is at first increasing then decreasing  Things to learn: 1.Using recursion 2.Using binary search appropriately Set B: Numeric Sequence 10

Recursive Logic: Check if sorted  To check whether a complete array is sorted or not you need to check whether every two consecutive elements are sorted or not  Note that for this problem it is necessary to do linear scan – we need to check every element 5 10 15 25 2030 Not Sorted!! 11 Set B: Numeric Sequence

Recursive Logic: Find maximum element  Property of the array: 1.The sequence is increasing at first and then decreasing 2.There are no duplicate elements Maximum element is the peak element  Last element of the increasing sequence/first element of decreasing element  It is greater than the element before it  It is also greater than the element after it  There is always a single element in the array satisfying both the above properties 12 Set B: Numeric Sequence

Recursive Logic: Find maximum element  Binary search: 1.If the element is peak element return the element // base case 2.If the current element belongs to increasing sequence (arr[i-1] < arr[i] < arr[i+1])  search on the right side of array 3.If the current element belongs to decreasing sequence (arr[i-1] > arr[i] > arr[i+1])  search on the left side of array 1 3 5 9811 10 Belongs to increasing sequence – search on right Belongs to decreasing sequence – search on left Peak element! 13 Set B: Numeric Sequence

Program Flow checkIfSorted 1.If array contains single element return true 2.If value at index < value at index+1, check the rest of the array (from index+1) 3.Otherwise return false findMax 1.If array contains single element return the element 2.If array contains 2 elements, return the greater element 3.Check mid-point of array i.If peak element, return the value ii.If belongs to increasing seq, findMax in right half iii.If belongs to decreasing seq, findMax in left half 14 Set B: Numeric Sequence

END OF FILE 15

Download ppt "CS1020 Week 13: 16 th April 2015. Contents  Sit-in Lab #4 2."

Similar presentations