Chapter 11 1
Recursion
Chapter 11
Recursion Chapter 11 Chapter 11 1 Reminders Project 6 is over. - - PowerPoint PPT Presentation
Recursion Chapter 11 Chapter 11 1 Reminders Project 6 is over. Project 7 has begun Start on time! Not much code for milestone, but a great deal of thought Chapter 11 2 Exam 2 Problem Problems Problem 2) Be careful about
Chapter 11 1
Chapter 11
Chapter 11 2
Chapter 11 3
Chapter 11 4
Chapter 11 5
Chapter 11 6
Chapter 11 7
Chapter 11 8
Chapter 11 9
Chapter 11 10
Chapter 11 11
Chapter 11 12
Chapter 11 13
Chapter 11 14
Chapter 11 15
Chapter 11 16
mid = (first + last)/2 if (first > last) return -1; else if (target == a[mid]) return mid; else if (target < a[mid] search a[first] through a[mid-1] else search a[mid + 1] through a[last]
Chapter 11 17
Chapter 11 18
Chapter 11 19
Chapter 11 20
If the array has only one element, stop. Otherwise Copy the first half of the elements into an array named front. Copy the second half of the elements into an array named tail. Sort array front recursively. Sort array tail recursively. Merge arrays front and tail.
Chapter 11 21
1 3 7 15 16
Chapter 11 22
Chapter 11 23
int frontIndex = 0, tailIndex = 0, aIndex = 0; while ((frontIndex < front.length) && (tailIndex < tail.length)) { if(front[frontIndex] < tail[tailIndex]} { a[aIndex] = front[frontIndex]; aIndex++; frontIndex++; }
Chapter 11 24
else { a[aIndex] = tail[tailIndex]; aIndex++; tailIndex++ } }
Chapter 11 25
Chapter 11 26
Chapter 11 27
Chapter 11 28