Download presentation
Presentation is loading. Please wait.
Published bySharlene Elisabeth Preston Modified over 9 years ago
1
Basic Design Techniques iteration versus recursion divide-and-conquer an example: merge sort
2
Iteration versus Recursion how? what? When an operation needs to be repeatedly performed, …… We may use either iteration or recursion Iteration = looping ◘ keep doing S until a condition is true Recursion = a procedure calling itself ◘ do(n) {if(n==0) stop else … do(n-1)}
3
An example sum of a list of numbers Iteration sum(L) { s = 0; while(L is not empty) { s = s + first(L); L = tail(L); } } first(L) returns the 1 st item of the list L Recursion sum(L) { if(L is empty) return 0; else return (first(L) + sum(tail(L)); } tail(L) returns sublist of L with its 1 st item removed
4
Binary Search Tree insertion – using recursion public void insertInOrder(String input_item) { if(value==null) {value = input_item;}// insert here else if(Func.lessThan(input_item, value)) { // insert to left subtree if(left==null) left = new BinTree(input_item); else left.insertInOrder(input_item); } else { // insert to right subtree if(right==null) right = new BinTree(input_item); else right.insertInOrder(input_item); }
5
Binary Search Tree insertion – using iteration public void insertInOrder(String input_item) { BinTree t = this; for(;;) { // loop until find the place to insert if(t.value==null) {t.value = input_item; break;} else if(Func.lessThan(input_item, value)) { // move to left subtree if(t.left==null){ t.left = new BinTree(input_item); break; } else t = t.left; } else { // move to right subtree if(t.right==null){ t.right = new BinTree(input_item); break;} else t= t.right; }
6
Recursion – Powerful but Simple Based on inductive definitions: ◘ define a base case (when terminate) ◘ assume that the problem can be solved for size (n-1), define how to solve the problem of size n Iterations can be replaced by recursions In many cases, recursions CANNOT be replaced by iterations
7
Divide-and-conquer The most widely applicable techniques for designing efficient algorithms: Divide - breaking a problem into smaller problems of same kind Conquer - from solutions to the smaller problems constructing a solution to the entire problem.
8
Binary search – seq. search O(n) O(log n) N posts, ordered by their heights Find one which has exactly same height as a given post solution – keep dividing posts into 2 halves … ?
9
King and his wines using binary search idea 000 001 010 011 100 101 110 111 0xx x0x xx0 All alive All diedTester 1 and 3 died 1 2 3
10
Merge sort input – an unordered list output – an ordered list Phase 1 – dividing the input list into 2, 4, 8, … until nothing can be divided Phase 2 – merge the split lists into right order, pair by pair
11
Merge sort 85 24 63 45 17 31 96 9017 24 31 45 50 63 85 96 85 24 63 4517 31 96 90 85 2463 45 85 24 63 45 17 31 96 90 17 3196 90 85 24 63 45 17 31 96 90 24 8545 6317 3190 96 24 45 63 85 17 31 90 96 divide conquer
12
Merging two sorted lists merge(list1,list2,list3) { while(both list1 and list2 are not empty) { insert min(head(list1), head(list2)) to the end of list3, remove this min value from list1 or list2 } if(either list1 or list2 is not empty) add it to list3 } 4 9 17 5 7 18 4 30 10
13
Merge two sorted lists java code for List.java merge(int fm1, int to1, int fm2, int to2) // assume fm2 = to1+1 {int i = fm1;int j = fm2;int k = fm1; while(i <= to1 && j <= to2) { if(Func.lessThan(data[i], data[j])) tmp[k++] = data[i++]; else tmp[k++] = data[j++]; } while(i <= to1) { tmp[k++] = data[i++];} while(j <= to2) { tmp[k++] = data[j++];} i = j = fm1; // copy them back to input data list for(; j<k; j++) data[i++] = tmp[j]; } // we use tmp[] as a buffer, it is defined in List class // if we don’t use tmp[], we can use a local array. It is slower.
14
Merge sort - java code public void mergesort() { mergesort(0, current_size-1); } public void mergesort(int fm, int to) { if(fm==to) return; // reached the bottom, do nothing int middle = ?? // find the middle position mergesort(fm,middle); mergesort(middle+1,to); merge(fm,middle,middle+1,to); } // can you rewrite it by using iteration?
Similar presentations
© 2025 SlidePlayer.com Inc.
All rights reserved.