CSC148 Lecture 8. Algorithm Analysis Binary Search Sorting



Similar documents
CSC 180 H1F Algorithm Runtime Analysis Lecture Notes Fall 2015

Analysis of Binary Search algorithm and Selection Sort algorithm

Sorting revisited. Build the binary search tree: O(n^2) Traverse the binary tree: O(n) Total: O(n^2) + O(n) = O(n^2)

Data Structures. Algorithm Performance and Big O Analysis

The Tower of Hanoi. Recursion Solution. Recursive Function. Time Complexity. Recursive Thinking. Why Recursion? n! = n* (n-1)!

Efficiency of algorithms. Algorithms. Efficiency of algorithms. Binary search and linear search. Best, worst and average case.

In mathematics, it is often important to get a handle on the error term of an approximation. For instance, people will write

Binary search algorithm

Algorithms. Margaret M. Fleck. 18 October 2010

SIMS 255 Foundations of Software Design. Complexity and NP-completeness

Many algorithms, particularly divide and conquer algorithms, have time complexities which are naturally

Quick Sort. Implementation

6. Standard Algorithms

Binary Heaps * * * * * * * / / \ / \ / \ / \ / \ * * * * * * * * * * * / / \ / \ / / \ / \ * * * * * * * * * *

Outline BST Operations Worst case Average case Balancing AVL Red-black B-trees. Binary Search Trees. Lecturer: Georgy Gimel farb

Data Structure [Question Bank]

Binary Heap Algorithms

The Running Time of Programs

Sorting Algorithms. Nelson Padua-Perez Bill Pugh. Department of Computer Science University of Maryland, College Park

Algorithm Analysis [2]: if-else statements, recursive algorithms. COSC 2011, Winter 2004, Section N Instructor: N. Vlajic

Converting a Number from Decimal to Binary

Class Overview. CSE 326: Data Structures. Goals. Goals. Data Structures. Goals. Introduction

CS473 - Algorithms I

CS473 - Algorithms I

CSE373: Data Structures and Algorithms Lecture 3: Math Review; Algorithm Analysis. Linda Shapiro Winter 2015

Zabin Visram Room CS115 CS126 Searching. Binary Search

Why? A central concept in Computer Science. Algorithms are ubiquitous.

APP INVENTOR. Test Review

CS711008Z Algorithm Design and Analysis

The Union-Find Problem Kruskal s algorithm for finding an MST presented us with a problem in data-structure design. As we looked at each edge,

the recursion-tree method

Algorithm Design and Recursion

Analysis of Computer Algorithms. Algorithm. Algorithm, Data Structure, Program

DATA STRUCTURES USING C

CSE 326, Data Structures. Sample Final Exam. Problem Max Points Score 1 14 (2x7) 2 18 (3x6) Total 92.

A binary heap is a complete binary tree, where each node has a higher priority than its children. This is called heap-order property

Recursive Algorithms. Recursion. Motivating Example Factorial Recall the factorial function. { 1 if n = 1 n! = n (n 1)! if n > 1

Loop Invariants and Binary Search

Binary Search Trees CMPSC 122

2. (a) Explain the strassen s matrix multiplication. (b) Write deletion algorithm, of Binary search tree. [8+8]

1) The postfix expression for the infix expression A+B*(C+D)/F+D*E is ABCD+*F/DE*++

CSI 333 Lecture 1 Number Systems

Binary Search Trees. A Generic Tree. Binary Trees. Nodes in a binary search tree ( B-S-T) are of the form. P parent. Key. Satellite data L R

Complexity Theory. IE 661: Scheduling Theory Fall 2003 Satyaki Ghosh Dastidar

Data Structures and Data Manipulation

Data Structures, Practice Homework 3, with Solutions (not to be handed in)

What Is Recursion? Recursion. Binary search example postponed to end of lecture

Analysis of Algorithms I: Optimal Binary Search Trees

1 Review of Newton Polynomials

Introduction to Algorithms March 10, 2004 Massachusetts Institute of Technology Professors Erik Demaine and Shafi Goldwasser Quiz 1.

Biostatistics 615/815

Previous Lectures. B-Trees. External storage. Two types of memory. B-trees. Main principles

CS 575 Parallel Processing

B-Trees. Algorithms and data structures for external memory as opposed to the main memory B-Trees. B -trees

Chapter Objectives. Chapter 9. Sequential Search. Search Algorithms. Search Algorithms. Binary Search

Chapter 13: Query Processing. Basic Steps in Query Processing

Learning Outcomes. COMP202 Complexity of Algorithms. Binary Search Trees and Other Search Trees

Data Structures and Algorithms

Computational Geometry. Lecture 1: Introduction and Convex Hulls

Closest Pair of Points. Kleinberg and Tardos Section 5.4

Randomized algorithms

Lecture Notes on Binary Search Trees

Questions 1 through 25 are worth 2 points each. Choose one best answer for each.

Diagonalization. Ahto Buldas. Lecture 3 of Complexity Theory October 8, Slides based on S.Aurora, B.Barak. Complexity Theory: A Modern Approach.

Information Theory and Coding Prof. S. N. Merchant Department of Electrical Engineering Indian Institute of Technology, Bombay

Binary Search Trees. Data in each node. Larger than the data in its left child Smaller than the data in its right child

Summit Public Schools Summit, New Jersey Grade Level / Content Area: Mathematics Length of Course: 1 Academic Year Curriculum: AP Computer Science A

Recursion. Slides. Programming in C++ Computer Science Dept Va Tech Aug., Barnette ND, McQuain WD

Closest Pair Problem

from Recursion to Iteration

FOPR-I1O23 - Fundamentals of Programming

AP Computer Science AB Syllabus 1

Computer Science 210: Data Structures. Searching

From Last Time: Remove (Delete) Operation

1/1 7/4 2/2 12/7 10/30 12/25

Binary Trees and Huffman Encoding Binary Search Trees

Class : MAC 286. Data Structure. Research Paper on Sorting Algorithms

Lecture 6: Binary Search Trees CSCI Algorithms I. Andrew Rosenberg

14:440:127 Introduction to Computers for Engineers. Notes for Lecture 06

4.2 Sorting and Searching

5.2 The Master Theorem

Applied Algorithm Design Lecture 5

Algorithm Design and Analysis

The Goldberg Rao Algorithm for the Maximum Flow Problem

Lecture 3: Finding integer solutions to systems of linear equations

Welcome to... Problem Analysis and Complexity Theory , 3 VU

Binary Bug - Automatic Binary Trading

Heaps & Priority Queues in the C++ STL 2-3 Trees

CompSci-61B, Data Structures Final Exam

Algorithms and Data Structures

CS104: Data Structures and Object-Oriented Design (Fall 2013) October 24, 2013: Priority Queues Scribes: CS 104 Teaching Team

Estimating the Average Value of a Function

To My Parents -Laxmi and Modaiah. To My Family Members. To My Friends. To IIT Bombay. To All Hard Workers

THE WINNING ROULETTE SYSTEM by

Lecture Notes on Linear Search

Persistent Data Structures

1. The memory address of the first element of an array is called A. floor address B. foundation addressc. first address D.

Transcription:

CSC148 Lecture 8 Algorithm Analysis Binary Search Sorting

Algorithm Analysis Recall definition of Big Oh: We say a function f(n) is O(g(n)) if there exists positive constants c,b such that f(n) <= c*g(n) for all n >= B Let T(n) be the worst case running time of an algorithm on input size n. (In this context, running time means the number of steps that the algorithm takes.)

Algorithm Analysis Loosely speaking, we approximate T(n) by finding a function g(n) such that T(n) is O(g(n)). Saying that this is an approximation for the running time isn't entirely accurate. Consider the algorithm for summing the numbers from 1 to n that we saw last week.

Algorithm Analysis The first algorithm, which loops through all the numbers from 1 to n, has time complexity O(n). The second algorithm, which uses a formula, has time complexity O(1). Is the following statement true: both algorithms have time complexity O(n^2)? It is! Consider the definition of Big Oh, and you will see why.

Algorithm Analysis Clearly neither algorithm takes anywhere near n^2 steps. We said that Big Oh notation is used to approximate T(n), but the last example demonstrates that the notation can lead to inaccurate approximations. What's going on?? In actuality, Big Oh notation gives us a convenient way of expressing an upper bound on the running time of an algorithm.

Algorithm Analysis Saying that the summation algorithms take O(n^2) time, although true, doesn't convey as much information as we'd like. To make our upper bound as meaningful as possible, we want to make it tight. Intuitively, O(g(n)) is a tight upper bound for T(n) if g(n) is the smallest and simplest function that satisfies the big oh criteria.

Algorithm Analysis For example, O(n) is a tight upper bound for 6n, but O(n^2) is not. More precisely, if for every function h(n) such that T(n) is O(h(n)) it is also true that g(n) is O(h(n)), then we say g(n) is a tight asymptotic bound on T(n). Think carefully about this definition. Why does it capture the intuition described on the previous slide?

Algorithm Analysis Big oh hierarchy on board Examples of analyzing algorithms.

Binary Search I'm thinking of a number between 1 and 100, each of which is equally likely. After you make a guess, I'll tell you if you guessed the number, or if the number is higher or lower than your guess. If you want to determine the number in as few guesses as possible, what strategy should you employ?

Binary Search A naïve approach would be to simply start guessing each number from 1 to N, ignoring the high/low information, until you guess the number. But there's a better way... You can always eliminate half the possible numbers by guessing the midpoint in the range of remaining possibilities. By eliminating half the remaining numbers in each guess, you can determine the number I'm thinking in no more than 7 steps.

Binary Search In general, if I'm thinking of a number from 1 to n, you can determine the number in no more than ceil(log 2 n) steps. We can apply this same idea to searching for an item in a sorted list. Given a sorted list of n items, you want to determine whether the item is in the list.

Binary Search A naïve approach is to search linearly for the item. Since the list is sorted, you can search the list more intelligently. As with the guessing numbers game, check to see if the item is at the midpoint of the list. If the item is at the midpoint, you are done. Otherwise, you know whether the item is in the first half or second half of the list. This means you can eliminate half the list from consideration.

Binary Search After you've eliminated half the items from consideration, recursively search for the item in the remaining half. If the item is NOT in the list, then eventually you'll try searching an empty list, at which point you are done. Binary search has time complexity O(log N), where N is the size of the list.

Sorting Sorting methods that you've seen in 108: Bubble sort Selection Sort Insertion sort These sorts all have time complexity O(n^2). We'll discuss a new sorting method, called merge sort, that has time complexity O(n log n).

Merge Sort Merge sort recursively sorts the first half of the list sorts the second half of the list merges the two halves into a newly sorted list Lets assume we have a list in which the first and second halves are sorted, but the whole list itself may not be sorted. How can we merge the two halves to create a new list that's sorted and contains all the elements of the original list?

Merge Sort Examples of merge on board.

Merge Sort Before we can actually use the merge procedure we just discussed, we have to somehow get to the point where the two halves of the list are sorted. This is done recursively. What is our base case?

Merge Sort A list containing 1 element is sorted. Lets develop mergesort in Wing.

Merge Sort Advantages: O(n log n) time compelxity see discussion on board for why mergesort has this time complexity Disadvantages requires additional space for the merged list

Quick Sort Recursive, like merge sort, sorting is in place. That is, additional space is not required. The main idea behind quicksort is contained in the partition procedure. It works by choosing a pivot element and finding the correct position of the pivot element in the final sorted list (this is called the split point ) moving elements less than the pivot before the split point, and other elements after the split point.

Quick Sort Quick sort works by partitioning the list (using the partition procedure described above), and then recursively sorting the lists before and after the split point.

Quick Sort Examples on board

Quick Sort Lets look at the quick sort procedure in Wing.