CS 104 Introduction to Computer Science and Graphics Problems Data Structure & Algorithms (3) Recurrence Relation 11/11 ~ 11/14/2008 Yang Song.

Slides:



Advertisements
Similar presentations
Introduction to Computer Science Theory
Advertisements

Garfield AP Computer Science
A simple example finding the maximum of a set S of n numbers.
CS4413 Divide-and-Conquer
Recurrences. What is a Recurrence Relation? A system of equations giving the value of a function from numbers to numbers in terms of the value of the.
Search algorithm In computer science, a search algorithm is an algorithm that takes a problem as input and returns a solution to the problem, usually after.
Ver. 1.0 Session 5 Data Structures and Algorithms Objectives In this session, you will learn to: Sort data by using quick sort Sort data by using merge.
CS 206 Introduction to Computer Science II 12 / 09 / 2009 Instructor: Michael Eckmann.
Search and Recursion CS221 – 2/23/09. List Search Algorithms Linear Search: Simple search through unsorted data. Time complexity = O(n) Binary Search:
Chapter 11 Sorting and Searching. Copyright © 2005 Pearson Addison-Wesley. All rights reserved Chapter Objectives Examine the linear search and.
Sorting and Searching. Searching List of numbers (5, 9, 2, 6, 3, 4, 8) Find 3 and tell me where it was.
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.
Wednesday, 11/25/02, Slide #1 CS 106 Intro to CS 1 Wednesday, 11/25/02  QUESTIONS??  Today:  More on sorting. Advanced sorting algorithms.  Complexity:
CHAPTER 11 Sorting.
Objectives Learn how to implement the sequential search algorithm Explore how to sort an array using the selection sort algorithm Learn how to implement.
ALGORITHM ANALYSIS AND DESIGN INTRODUCTION TO ALGORITHMS CS 413 Divide and Conquer Algortihms: Binary search, merge sort.
Divide-and-Conquer 7 2  9 4   2   4   7
Recursion, Complexity, and Searching and Sorting By Andrew Zeng.
Chapter 16: Searching, Sorting, and the vector Type.
CHAPTER 09 Compiled by: Dr. Mohammad Omar Alhawarat Sorting & Searching.
Recursion, Complexity, and Sorting By Andrew Zeng.
计算机科学概述 Introduction to Computer Science 陆嘉恒 中国人民大学 信息学院
Chapter 12 Recursion, Complexity, and Searching and Sorting
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 19: Searching and Sorting Algorithms.
1 Searching. 2 Searching Searching refers to the operation of finding an item from a list of items based on some key value. Two Searching Methods (1)
Divide-and-Conquer1 7 2  9 4   2  2 79  4   72  29  94  4.
Merge Sort. What Is Sorting? To arrange a collection of items in some specified order. Numerical order Lexicographical order Input: sequence of numbers.
C++ Programming: From Problem Analysis to Program Design, Second Edition Chapter 19: Searching and Sorting.
LAB#7. Insertion sort In the outer for loop, out starts at 1 and moves right. It marks the leftmost unsorted data. In the inner while loop, in starts.
Heapsort. Heapsort is a comparison-based sorting algorithm, and is part of the selection sort family. Although somewhat slower in practice on most machines.
CS 361 – Chapters 8-9 Sorting algorithms –Selection, insertion, bubble, “swap” –Merge, quick, stooge –Counting, bucket, radix How to select the n-th largest/smallest.
Chapter 12 Binary Search and QuickSort Fundamentals of Java.
CS 61B Data Structures and Programming Methodology July 21, 2008 David Sun.
Chapter 18: Searching and Sorting Algorithms. Objectives In this chapter, you will: Learn the various search algorithms Implement sequential and binary.
Searching and Sorting Recursion, Merge-sort, Divide & Conquer, Bucket sort, Radix sort Lecture 5.
Recurrences David Kauchak cs161 Summer Administrative Algorithms graded on efficiency! Be specific about the run times (e.g. log bases) Reminder:
1 Introduction to Sorting Algorithms Sort: arrange values into an order Alphabetical Ascending numeric Descending numeric Two algorithms considered here.
1 Sorting (Bubble Sort, Insertion Sort, Selection Sort)
Review 1 Selection Sort Selection Sort Algorithm Time Complexity Best case Average case Worst case Examples.
Computer Science 101 Fast Algorithms. What Is Really Fast? n O(log 2 n) O(n) O(n 2 )O(2 n )
1 Searching and Sorting Searching algorithms with simple arrays Sorting algorithms with simple arrays –Selection Sort –Insertion Sort –Bubble Sort –Quick.
Course Code #IDCGRF001-A 5.1: Searching and sorting concepts Programming Techniques.
Young CS 331 D&A of Algo. Topic: Divide and Conquer1 Divide-and-Conquer General idea: Divide a problem into subprograms of the same kind; solve subprograms.
Concepts of Algorithms CSC-244 Unit 15 & 16 Divide-and-conquer Algorithms ( Binary Search and Merge Sort ) Shahid Iqbal Lone Computer College Qassim University.
Sorting & Searching Geletaw S (MSC, MCITP). Objectives At the end of this session the students should be able to: – Design and implement the following.
Chapter 16: Searching, Sorting, and the vector Type.
INTRO2CS Tirgul 8 1. Searching and Sorting  Tips for debugging  Binary search  Sorting algorithms:  Bogo sort  Bubble sort  Quick sort and maybe.
Sort Algorithm.
Chapter 16: Searching, Sorting, and the vector Type
Searching and Sorting Algorithms
Divide-and-Conquer 6/30/2018 9:16 AM
Divide and Conquer.
Adapted from slides by Marty Stepp and Stuart Reges
Merge Sort Merge sort is a recursive algorithm for sorting that decomposes the large problem.
CSc 110, Spring 2017 Lecture 39: searching.
MSIS 655 Advanced Business Applications Programming
Divide-and-Conquer 7 2  9 4   2   4   7
CSE 373 Data Structures and Algorithms
Divide and Conquer Algorithms Part I
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Divide-and-Conquer 7 2  9 4   2   4   7
CSE 373 Data Structures and Algorithms
CPS120: Introduction to Computer Science
Quick-Sort 4/25/2019 8:10 AM Quick-Sort     2
CPS120: Introduction to Computer Science
Divide-and-Conquer 7 2  9 4   2   4   7
Divide and Conquer Merge sort and quick sort Binary search
Presentation transcript:

CS 104 Introduction to Computer Science and Graphics Problems Data Structure & Algorithms (3) Recurrence Relation 11/11 ~ 11/14/2008 Yang Song

Divide-and-Conquer It is an algorithm design technique Example: find the max of n elements Iteration Method: Sequential So, we need n-1 comparison Divide-and-conquer Find out the max of each half, then compare Let the number of comparisons is C(n), and n = 2 k then C(n) = { 0, (n=1) 2 C(n/2) + 1 (n>1) This way to represent C(n) is called Recurrence Relation.

Recurrence Relation We need the recurrence relation to measure the complexity of the algorithm. How to solve it? Repeated Substitution Guess it, then verify the solution by induction proof. One more example: Binary Search

Binary Search Binary Search is to search some element from a SORTED list. It works by repeatedly checking the middle point of this list. If the element is bigger than the middle point, the first half of the list will be dumped, otherwise dump the second half. The algorithm gets its name from the way we always deal with half of the remaining list. The recurrence relation is: (number of comparisons) C(n) = 1 (when n=1) C(n) = C(n/2) + 1 (when n>1, to simplify the question, we make n = 2 k ) With the Repeated Substitution, we figure out the exact solution is C(n) = 1+log 2 n (which means the time complexity is O(log 2 n) Make some extra points with induction proof of this solution.

Bubble Sort Bubble sort is a simple sorting algorithm. It works by repeatedly stepping through the list to be sorted, comparing two items at a time and swapping them if they are in the wrong order. The pass through the list is repeated until no swaps are needed, which indicates that the list is sorted. It gets its name from the way smaller elements "bubble" to the top of the list. Example: sort a list [5, 1, 4, 2, 3] The recurrence relation is: (number of comparisons) C(n) = 0 (when n=1) C(n) = C(n-1) + (n-1) (when n>1) Worst case is O(n 2 ). Solve it with repeated substitution and verify the solution with induction proof?

Merge Sort Merge sort preserves the input order of equal elements in the sorted output. It is an example of the divide and conquer technique. It was invented by John Von Neumann in Example: sort a list [5, 2, 8, 1, 7, 4, 6, 3] Basic Steps: Divide the unsorted list into two sublists of about half the size Divide each of the two sublists recursively until we have list sizes of length 1, in which case the list itself is returned Merge the two sublists back into one sorted list.

Merge Sort (Cont.) The recurrence relation is: (number of comparisons) C(n) = 0 (when n=1) C(n) = 2*C(n/2) + (n-1) (when n>1) For time complexity, no best or worst case It is always about O(n*log n ). Solve it with repeated substitution and verify with induction proof?

More Sorting Algorithms Heap Sort Quick Sort Integer Sort Bucket sort Radix sort