Computer Science 101 A Survey of Computer Science

Slides:



Advertisements
Similar presentations
Algorithms Analysis Lecture 6 Quicksort. Quick Sort Divide and Conquer.
Advertisements

CS4413 Divide-and-Conquer
Sorting Algorithms and Average Case Time Complexity
CPSC 171 Introduction to Computer Science More Efficiency of Algorithms.
Algorithm Design Strategy Divide and Conquer. More examples of Divide and Conquer  Review of Divide & Conquer Concept  More examples  Finding closest.
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.
General Computer Science for Engineers CISC 106 James Atlas Computer and Information Sciences 10/23/2009.
CHAPTER 11 Sorting.
MergeSort Source: Gibbs & Tamassia. 2 MergeSort MergeSort is a divide and conquer method of sorting.
1 Search Algorithms Sequential Search (Linear Search) Binary Search Rizwan Rehman Centre for Computer Studies Dibrugarh University.
1 Divide and Conquer Binary Search Mergesort Recurrence Relations CSE Lecture 4 – Algorithms II.
Computer Science 101 Fast Searching and Sorting. Improving Efficiency We got a better best case by tweaking the selection sort and the bubble sort We.
Searching Given a collection and an element (key) to find… Output –Print a message (“Found”, “Not Found) –Return a value (position of key ) Don’t modify.
C++ Programming: From Problem Analysis to Program Design, Second Edition Chapter 19: Searching and Sorting.
Ch 18 – Big-O Notation: Sorting & Searching Efficiencies Our interest in the efficiency of an algorithm is based on solving problems of large size. If.
CSC 211 Data Structures Lecture 13
Java Methods Big-O Analysis of Algorithms Object-Oriented Programming
Computer Science 101 A Survey of Computer Science Timing Problems.
Chapter 8 Sorting and Searching Goals: 1.Java implementation of sorting algorithms 2.Selection and Insertion Sorts 3.Recursive Sorts: Mergesort and Quicksort.
Divide-and-Conquer The most-well known algorithm design strategy: 1. Divide instance of problem into two or more smaller instances 2.Solve smaller instances.
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.
Divide and Conquer Strategy
Chapter 9 Sorting. The efficiency of data handling can often be increased if the data are sorted according to some criteria of order. The first step is.
1. Searching The basic characteristics of any searching algorithm is that searching should be efficient, it should have less number of computations involved.
Searching Topics Sequential Search Binary Search.
Search Algorithms Written by J.J. Shepherd. Sequential Search Examines each element one at a time until the item searched for is found or not found Simplest.
1Computer Sciences Department. 2 QUICKSORT QUICKSORT TUTORIAL 5.
WHICH SEARCH OR SORT IS BETTER?. COMPARING ALGORITHMS Time efficiency refers to how long it takes an algorithm to run Space efficiency refers to the amount.
Section 1.7 Comparing Algorithms: Big-O Analysis.
Fundamentals of Algorithms MCS - 2 Lecture # 11
Searching and Sorting Algorithms
Subject Name: Design and Analysis of Algorithm Subject Code: 10CS43
CMSC201 Computer Science I for Majors Lecture 23 – Sorting
Searching Given a collection and an element (key) to find… Output
Last Class We Covered Sorting algorithms Searching algorithms
Introduction to complexity
COMP 53 – Week Seven Big O Sorting.
Data Structures in Java with JUnit ©Rick Mercer
Sorting by Tammy Bailey
Chapter 4 Divide-and-Conquer
Divide and Conquer divide and conquer algorithms typically recursively divide a problem into several smaller sub-problems until the sub-problems are.
And now for something completely different . . .
MergeSort Source: Gibbs & Tamassia.
Adapted from slides by Marty Stepp and Stuart Reges
Divide-and-Conquer The most-well known algorithm design strategy:
Quick Sort (11.2) CSE 2011 Winter November 2018.
Quicksort analysis Bubble sort
Merge sort merge sort: Repeatedly divides the data in half, sorts each half, and combines the sorted halves into a sorted whole. The algorithm: Divide.
Data Structures Review Session
Topic: Divide and Conquer
24 Searching and Sorting.
Divide-and-Conquer The most-well known algorithm design strategy:
Divide and Conquer Algorithms Part I
Chapter 4.
Quick-Sort 2/25/2019 2:22 AM Quick-Sort     2
Algorithm Efficiency and Sorting
CSC 380: Design and Analysis of Algorithms
CPS120: Introduction to Computer Science
Merge sort merge sort: Repeatedly divides the data in half, sorts each half, and combines the sorted halves into a sorted whole. The algorithm: Divide.
CPS120: Introduction to Computer Science
CSC 380: Design and Analysis of Algorithms
Algorithms Recurrences.
Computer Science 3 03A-Searching
Computer Science 101 A Survey of Computer Science
Algorithm Efficiency and Sorting
Divide and Conquer Merge sort and quick sort Binary search
Algorithm Efficiency and Sorting
Presentation transcript:

Computer Science 101 A Survey of Computer Science Efficiency of Divide and Conquer

Background - Logarithms (Base 2) Definition. The logarithm to base 2 of n is that number k such that 2k=n. Example: 25=32 so Lg(32)=5. Another way to think of this is that Lg(n) is the number of times we must divide n by 2 until we get 1. Note: Lg(n) is usually a fraction, but the closest integer will work for us.

Base 2 Logarithms - Table n Lg(n) n Lg(n) 1 0 1024 10 2 1 2048 11 4 2 4096 12 8 3 8192 13 16 4 1,048,576 20 32 5 64 6 128 7 256 8 512 9

Quicksort - Rough Analysis For simplification, assume that we always get even splits when we partition. When we partition the entire list, each element is compared with the pivot - approximately n comparisons.

Quicksort - Rough Analysis (cont.) Each of the “halves” is partitioned, each taking about n/2 comparisons, thus about n more comparisons. Each of the “fourths” is partitioned,each taking about n/4 comparisons - n more.

Quicksort - Rough Analysis (cont.) How many “levels” of “about n comparisons” do we get? Roughly, we keep splitting until the pieces are about size 1.

Quicksort - Rough Analysis (cont.) How many times must we divide n by 2 before we get 1? Lg(n) times, of course. Thus Comparisons  n Lg(n) Quicksort is O(n Lg(n)) and T(n)  KnLg(n) (Linearithmic)

Growth rates

Binary Search Algorithm Note: For searching sorted list Given: n, N1,N2,…Nn (sorted) and target T Want: Position where T is located or message indicating that T is not in list

Binary Search - The algorithm Set Found to false Set B to 1 Set E to n While not Found and B ≤ E Set M to (B+E)/2 (whole part) If N(M)=T then Print M Set Found to true else If T<NM then Set E to M-1 else Set B to M+1 end-of-loop If not Found then Print “Target not in list”

Binary Search Example 4 8 20 26 31 39 40 42 50 63 70 Target 50 85 4 8 E=n 85 4 8 20 26 31 39 40 42 50 63 70 M=(B+E)/2 85 4 8 20 26 31 39 40 42 50 63 70 T>NM B=M+1 85 40 42 50 63 70 M=(B+E)/2 85 Output location 9

Binary Search Example 4 8 20 26 31 39 40 42 50 63 70 Target 31 85 4 8 B=1,E=n 85 4 8 20 26 31 39 40 42 50 63 70 M=(B+E)/2 85 39 40 42 50 63 70 4 8 20 26 31 T<NM E=M-1 85 4 8 20 26 31 M=(B+E)/2 4 8 20 26 31 T>NM B=M+1

Binary Search Example (cont.) 26 31 M=(B+E)/2 26 31 T>NM B=M+1 31 M=(B+E)/2 Output location 5

Efficiency - Binary Search Note with 1000 elements, Sequential Search would have a worst case number of comparisons of 1000. After 1 comparison, Binary Search would be left with at most 500 elements. After 2 comparisons 250 at worst After 3 comparisons 125 at worst After 4 comparisons 62 at worst After 5 comparisons 31 at worst After 6 comparisons 15 at worst After 7 comparisons 7 at worst After 8 comparisons 3 at worst After 9 comparisons 1 at worst

Efficiency - Binary Search (cont) Each time we make a comparison, we cut the list in half. How many times must we do this to end up with 1 element? Lg(n), of course. Binary search is O(Lg(n)) T(n)  K Lg(n)

Growth rates

If it takes that long for 100, we'll dang well be here all night!