CSE 373 Data Structures and Algorithms

Slides:



Advertisements
Similar presentations
College of Information Technology & Design
Advertisements

Garfield AP Computer Science
A simple example finding the maximum of a set S of n numbers.
CSE 373: Data Structures and Algorithms Lecture 5: Math Review/Asymptotic Analysis III 1.
CS4413 Divide-and-Conquer
CSC 331: Algorithm Analysis Divide-and-Conquer Algorithms.
CS223 Advanced Data Structures and Algorithms 1 Divide and Conquer Neil Tang 4/15/2010.
Recursion & Merge Sort Introduction to Algorithms Recursion & Merge Sort CSE 680 Prof. Roger Crawfis.
CSE 373: Data Structures and Algorithms
Divide-and-Conquer1 7 2  9 4   2  2 79  4   72  29  94  4.
Divide-and-Conquer1 7 2  9 4   2  2 79  4   72  29  94  4.
CS 104 Introduction to Computer Science and Graphics Problems Data Structure & Algorithms (3) Recurrence Relation 11/11 ~ 11/14/2008 Yang Song.
1 © 2006 Pearson Addison-Wesley. All rights reserved Searching and Sorting Linear Search Binary Search -Reading p
Building Java Programs Chapter 13 Searching reading: 13.3.
Chapter 11- Recursion. Overview n What is recursion? n Basics of a recursive function. n Understanding recursive functions. n Other details of recursion.
Analysis of Algorithms
1 Lecture 16: Lists and vectors Binary search, Sorting.
Project 2 due … Project 2 due … Project 2 Project 2.
1 Searching and Sorting Linear Search Binary Search.
1 TCSS 143, Autumn 2004 Lecture Notes Recursion Koffman/Wolfgang Ch. 7, pp ,
Searching and Sorting Recursion, Merge-sort, Divide & Conquer, Bucket sort, Radix sort Lecture 5.
+ David Kauchak cs312 Review. + Midterm Will be posted online this afternoon You will have 2 hours to take it watch your time! if you get stuck on a problem,
1 Searching and Sorting Searching algorithms with simple arrays Sorting algorithms with simple arrays –Selection Sort –Insertion Sort –Bubble Sort –Quick.
Binary search and complexity reading:
1 Recursive algorithms Recursive solution: solve a smaller version of the problem and combine the smaller solutions. Example: to find the largest element.
Vishnu Kotrajaras, PhD.1 Data Structures
BINARY SEARCH CS16: Introduction to Data Structures & Algorithms Thursday February 12,
Searching and Sorting Searching algorithms with simple arrays
Lecture 25: Searching and Sorting
Analysis of Algorithms CS 477/677
Introduction to Algorithms: Divide-n-Conquer Algorithms
Using recursion for Searching and Sorting
Fundamentals of Algorithms MCS - 2 Lecture # 11
CSc 110, Autumn 2016 Lecture 36: searching.
Analysis of Algorithms CS 477/677
Subject Name: Design and Analysis of Algorithm Subject Code: 10CS43
Modeling with Recurrence Relations
Analysis of Algorithms
Divide-and-Conquer 6/30/2018 9:16 AM
CS 3343: Analysis of Algorithms
Divide-and-Conquer The most-well known algorithm design strategy:
Chapter 4 Divide-and-Conquer
Searching & Sorting "There's nothing hidden in your head the sorting hat can't see. So try me on and I will tell you where you ought to be." -The Sorting.
Divide and Conquer.
Intro to Recursion.
Divide-and-Conquer The most-well known algorithm design strategy:
Growth Functions Algorithms Lecture 8
CS 3343: Analysis of Algorithms
Building Java Programs
Lecture 14: binary search and complexity reading:
CSE 143 Lecture 5 Binary search; complexity reading:
CSc 110, Spring 2017 Lecture 39: searching.
Lecture 15: binary search reading:
Data Structures Review Session
Divide-and-Conquer 7 2  9 4   2   4   7
Topic: Divide and Conquer
Searching: linear & binary
Linear Search Binary Search Tree
Divide-and-Conquer The most-well known algorithm design strategy:
CSE 2010: Algorithms and Data Structures
Chapter 4.
Solving Recurrence Relations
And now for something completely different . . .
Divide-and-Conquer 7 2  9 4   2   4   7
CSE 373 Data Structures and Algorithms
Sum this up for me Let’s write a method to calculate the sum from 1 to some n public static int sum1(int n) { int sum = 0; for (int i = 1; i
Algorithms Recurrences.
Divide and Conquer Merge sort and quick sort Binary search
Divide-and-Conquer 7 2  9 4   2   4   7
Presentation transcript:

CSE 373 Data Structures and Algorithms Lecture 6: Searching / Running times in practice

Searching and recursion Problem: Given a sorted array of integers and an integer i, find the index of any occurrence of i if it appears in the array. If not, return -1. We could solve this problem using a standard iterative search; starting at the beginning, and looking at each element until we find i What is the runtime of an iterative search? Since the array is sorted, we can do better.

Binary search algorithm Algorithm idea: Start in the middle, and only search the portions of the array that might contain the element i. Eliminate half of the array from consideration at each step. Can be written iteratively, but is harder to get right Called binary search because it chops the area to examine in half each time Implemented in Java as Arrays.binarySearch in java.util package

Binary search example i = 16 min mid (too big!) max 1 2 3 4 5 6 4 7 16 1 2 3 4 5 6 4 7 16 20 37 38 43 min mid (too big!) max

Binary search example i = 16 min mid (too small!) max 1 2 3 4 5 6 4 7 1 2 3 4 5 6 4 7 16 20 37 38 43 min mid (too small!) max

Binary search example i = 16 min, mid, max (found it!) 1 2 3 4 5 6 4 7 1 2 3 4 5 6 4 7 16 20 37 38 43 min, mid, max (found it!)

Binary search pseudocode binary search array a for value i: if all elements have been searched, result is -1. examine middle element a[mid]. if a[mid] equals i, result is mid. if a[mid] is greater than i, binary search left half of a for i. if a[mid] is less than i, binary search right half of a for i.

Divide-and-conquer divide-and-conquer algorithm: a means for solving a problem that first separates the main problem into 1 or more smaller problems, then solves each of the smaller problems, then uses those sub-solutions to solve the original problem 1: "divide" the problem up into pieces 2: "conquer" each smaller piece 3: (if necessary) combine the pieces at the end to produce the overall solution binary search is one such algorithm

Runtime of binary search How do we analyze the runtime of binary search and recursive functions in general? Binary search either exits immediately, when input size <= 1 or value found (base case), or executes itself on 1/2 as large an input (rec. case) T(1) = c T(2) = T(1) + c T(4) = T(2) + c T(8) = T(4) + c ... T(n) = T(n/2) + c How many times does this division in half take place? For more rigorous proof, lookup “recurrence relation” and “Master theorem” In binary sort, log n divisions take place.

Master Theorem (for reference only) A recurrence written in the form T(n) = a * T(n / b) + f(n) (where f(n) is a function that is O(nk) for some power k) has a solution such that This form of recurrence is very common for divide-and- conquer algorithms

Runtime (for reference only) Binary search is of the correct format: T(n) = a * T(n / b) + f(n) T(n) = T(n/2) + c a = 1, b = 2 f(n) = c = O(1) = O(n0) ... therefore k = 0 a = bk 1 = 20, therefore: T(n) = O(n0 log n) = O(log n)