MIT1_204S10_lec09

MIT1_204S10_lec09 - 1.204 Lecture 9 Divide and conquer:...

Info iconThis preview shows pages 1–5. Sign up to view the full content.

View Full Document Right Arrow Icon
1.204 Lecture 9 Divide and conquer: binary search, quicksort, selection Divide and conquer Divide-and-conquer (or divide-and-combine) approach to solving problems solving problems: method DivideAndConquer(Arguments) if (SmallEnough(Arguments)) // Termination return Answer else // “Divide” Identity= Combine( SomeFunc(Arguments), DivideAndConquer(SmallerArguments)) return Identity // “Combine” Divide and conquer solves a large problem as the combination of solutions of smaller problems We implement divide and conquer either with recursion or iteration 1
Background image of page 1

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

View Full DocumentRight Arrow Icon
Binary search public class BinarySearch { public static int binSearch(int a[], int x) { // a is sorted int low = 0, high = a.length - 1; while (low <= high) { int mid = (low + high) / 2; if (x < a[mid]) high = mid - 1; else if (x > a[mid]) low = mid + 1; else return mid; } return Integer.MIN_VALUE; } // Easy to write recursively too (2 more arguments) Example: -55 -9 -7 -5 -3 -1 2 3 4 6 9 98 309 Binary search example public static void main(String[] args) { int[] a= {-1, -3, -5, -7, -9, 2, 6, 9, 3, 4, 98, 309, -55}; Arrays.sort(a); // Quicksort for (int i : a) System.out.print(" " + i); System.out.println(); System.out.println(“Location of -1 is " + binSearch(a, -1)); System.out.println(“Location of -55 is "+ binSearch(a,-55)); System.out.println(“Location of 98 is " + binSearch(a, 98)); System.out.println(“Location of -7 is " + binSearch(a, -7)); System.out.println(“Location of 8 is " + binSearch(a, 8)); } // Output -55 -9 -7 -5 -3 -1 2 3 4 6 9 98 309 BinSrch location of -1 is 5 BinSrch location of -55 is 0 BinSrch location of 98 is 11 BinSrch location of -7 is 2 BinSrch location of 8 is -2147483648 2
Background image of page 2
Binary search performance Each iteration cuts search space in half Analogous to tree search Maximum number of steps is O(lg n) There are n/2 k values left to search after each step k Successful searches take between 1 and ~lg n steps Unsuccessful searches take ~lg n steps every time We have to sort the array before searching it Quicksort takes O(n lg n) steps This is the bottleneck step If we have to sort before each search, this is too slow Use binary search tree instead: O(lg n) add, O(lg n) find Binary search used on data that doesn’t change (or that arrives sorted) Sort once, search many times Quicksort overview Most efficient general purpose sort, O(n lg n) Simple quicksort has worst case of O(n 2 ) which can be avoided ), which can be avoided Basic strategy Split array (or list) of data to be sorted into 2 subarrays so that: Everything in first subarray is smaller than a known value Everything in second subarray is larger than that value Technique is called ‘partitioning’ Known value is called the ‘pivot element’ Once we’ve partitioned, pivot element will be located in its final position Then we continue splitting the subarrays into smaller subarrays, until the resulting pieces have only one element (using recursion) 3
Background image of page 3

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

View Full DocumentRight Arrow Icon
Quicksort algorithm 1. Choose an element as pivot. We use right element 2. Start indexes at left and (right-1) elements 3. Move left index until we find an element> pivot 4.
Background image of page 4
Image of page 5
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 12/04/2011 for the course ESD 1.204 taught by Professor Georgekocur during the Spring '10 term at MIT.

Page1 / 17

MIT1_204S10_lec09 - 1.204 Lecture 9 Divide and conquer:...

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

View Full Document Right Arrow Icon
Ask a homework question - tutors are online