3/10/03Tucker, Sec.3-51 Tucker, Applied Combinatorics, Sec. 3.5, Jo E-M “Big O” Notation We say that a function is if for some constant c, when n is large.

Slides:



Advertisements
Similar presentations
Introduction to Algorithms Quicksort
Advertisements

Heaps1 Part-D2 Heaps Heaps2 Recall Priority Queue ADT (§ 7.1.3) A priority queue stores a collection of entries Each entry is a pair (key, value)
Lecture 3: Parallel Algorithm Design
Greedy Algorithms Greed is good. (Some of the time)
3.3 Spanning Trees Tucker, Applied Combinatorics, Section 3.3, by Patti Bodkin and Tamsen Hunter.
© Copyright 2012 by Pearson Education, Inc. All Rights Reserved. 1 Chapter 17 Sorting.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 24 Sorting.
CS 253: Algorithms Chapter 8 Sorting in Linear Time Credit: Dr. George Bebis.
Tirgul 10 Rehearsal about Universal Hashing Solving two problems from theoretical exercises: –T2 q. 1 –T3 q. 2.
Lecture 25 Selection sort, reviewed Insertion sort, reviewed Merge sort Running time of merge sort, 2 ways to look at it Quicksort Course evaluations.
Chapter 11 Sorting and Searching. Copyright © 2005 Pearson Addison-Wesley. All rights reserved Chapter Objectives Examine the linear search and.
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:
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 19: Heap Sort.
FALL 2006CENG 351 Data Management and File Structures1 External Sorting.
CHAPTER 11 Sorting.
Quicksort.
CS 104 Introduction to Computer Science and Graphics Problems Data Structure & Algorithms (3) Recurrence Relation 11/11 ~ 11/14/2008 Yang Song.
Analysis of Algorithms CS 477/677
B-Trees. CSM B-Trees 2 Motivation for B-Trees So far we have assumed that we can store an entire data structure in main memory What if we have so.
DAST 2005 Week 4 – Some Helpful Material Randomized Quick Sort & Lower bound & General remarks…
Priority Queues and Heaps Bryce Boe 2013/11/20 CS24, Fall 2013.
1 Time Analysis Analyzing an algorithm = estimating the resources it requires. Time How long will it take to execute? Impossible to find exact value Depends.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 19: Searching and Sorting Algorithms.
Merge Sort. What Is Sorting? To arrange a collection of items in some specified order. Numerical order Lexicographical order Input: sequence of numbers.
The Selection Problem. 2 Median and Order Statistics In this section, we will study algorithms for finding the i th smallest element in a set of n elements.
Sorting Algorithms 2. Quicksort General Quicksort Algorithm: Select an element from the array to be the pivot Select an element from the array to be the.
Heapsort. Heapsort is a comparison-based sorting algorithm, and is part of the selection sort family. Although somewhat slower in practice on most machines.
Chapter 7: Sorting Algorithms Insertion Sort. Sorting Algorithms  Insertion Sort  Shell Sort  Heap Sort  Merge Sort  Quick Sort 2.
Discrete Structures Lecture 12: Trees Ji Yanyan United International College Thanks to Professor Michael Hvidsten.
B-Trees. CSM B-Trees 2 Motivation for B-Trees So far we have assumed that we can store an entire data structure in main memory What if we have so.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 26 Sorting.
Complexity, etc. Homework. Comparison to computability. Big Oh notation. Sorting. Classwork/Homework: prepare presentation on specific sorts. Presentation.
Chapter 18: Searching and Sorting Algorithms. Objectives In this chapter, you will: Learn the various search algorithms Implement sequential and binary.
Priority Queues and Heaps. October 2004John Edgar2  A queue should implement at least the first two of these operations:  insert – insert item at the.
1 Lower Bound on Comparison-based Search We have now covered lots of searching methods –Contiguous Data (Arrays) Sequential search Binary Search –Dynamic.
3 – SIMPLE SORTING ALGORITHMS
Week 13 - Friday.  What did we talk about last time?  Sorting  Insertion sort  Merge sort  Started quicksort.
Midterm Review 1. Midterm Exam Thursday, October 15 in classroom 75 minutes Exam structure: –TRUE/FALSE questions –short questions on the topics discussed.
COSC 3101A - Design and Analysis of Algorithms 6 Lower Bounds for Sorting Counting / Radix / Bucket Sort Many of these slides are taken from Monica Nicolescu,
Tree Data Structures. Heaps for searching Search in a heap? Search in a heap? Would have to look at root Would have to look at root If search item smaller.
Chapter 9 Sorting 1. The efficiency of data handling can often be increased if the data are sorted according to some criteria of order. The first step.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 23 Algorithm Efficiency.
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 25 Sorting.
Searching Topics Sequential Search Binary Search.
Bushy Binary Search Tree from Ordered List. Behavior of the Algorithm Binary Search Tree Recall that tree_search is based closely on binary search. If.
Chapter 4, Part II Sorting Algorithms. 2 Heap Details A heap is a tree structure where for each subtree the value stored at the root is larger than all.
Week 13 - Wednesday.  What did we talk about last time?  NP-completeness.
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.
Sorting Lower Bounds n Beating Them. Recap Divide and Conquer –Know how to break a problem into smaller problems, such that –Given a solution to the smaller.
Sorting Algorithms Written by J.J. Shepherd. Sorting Review For each one of these sorting problems we are assuming ascending order so smallest to largest.
Computability Sort homework. Formal definitions of time complexity. Big 0. Homework: Exercises. Searching. Shuffling.
Data Structures and Algorithms Instructor: Tesfaye Guta [M.Sc.] Haramaya University.
CS6045: Advanced Algorithms Sorting Algorithms. Sorting So Far Insertion sort: –Easy to code –Fast on small inputs (less than ~50 elements) –Fast on nearly-sorted.
Chapter 15 Running Time Analysis. Topics Orders of Magnitude and Big-Oh Notation Running Time Analysis of Algorithms –Counting Statements –Evaluating.
Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 23 Sorting.
Chapter 23 Sorting Jung Soo (Sue) Lim Cal State LA.
Computational Geometry
Source: Muangsin / Weiss
Design and Analysis of Algorithms
Interval Heaps Complete binary tree.
Sorting Algorithms Written by J.J. Shepherd.
(2,4) Trees 11/15/2018 9:25 AM Sorting Lower Bound Sorting Lower Bound.
Complexity Present sorting methods. Binary search. Other measures.
(2,4) Trees 12/4/2018 1:20 PM Sorting Lower Bound Sorting Lower Bound.
Algorithmic Complexity
Applied Combinatorics, 4th Ed. Alan Tucker
(2,4) Trees 2/28/2019 3:21 AM Sorting Lower Bound Sorting Lower Bound.
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Intro to Computer Science CS1510 Dr. Sarah Diesburg
CENG 351 Data Management and File Structures
Presentation transcript:

3/10/03Tucker, Sec.3-51 Tucker, Applied Combinatorics, Sec. 3.5, Jo E-M “Big O” Notation We say that a function is if for some constant c, when n is large. For example, is since for n > 3. This is described by saying that is ‘on the order of’. Big O notation calls attention to the part of a function that grows the fastest, so gives a simple estimate of how many steps are required for an algorithm to run.

3/10/03Tucker, Sec.3-52 Binary Testing Tree Compare things (objects, subsets, lists, whatever) in some way with two outcomes. Keep comparing… Eventually arrive at one of the n! possible orders Since there are n! leaves on this tree, by Theorem 3 in section 3.1, the tree has height. Thus, whatever binary comparison technique is used, the worst case will require at least comparisons.

3/10/03Tucker, Sec.3-53 Bubble Sort For m from 2 to n, do For j from n (step –1) to m do If A j < A j-1, then interchange A j and A j m = 2, j = m = 2, j = m = 2, j = m = 2, j = m = 2, j = m = 3, j = m = 3, j = m = 3, j = 4 Moves the smallest number to the top Moves the next smallest number to the top

3/10/03Tucker, Sec.3-54 Complexity of the Bubble Sort When m = 2, j goes from n to 2, so you have to do n –1 comparisons. When m = 3, j goes from n to 3, so you have to do n –2 comparisons, and so on. Thus, the total number of comparisons is: Thus, the Bubble sort takes comparisons, which is a lot more than the theoretical bound of.

3/10/03Tucker, Sec.3-55 Merge Sort—Subdivision Tree First divide the set repeatedly, roughly in half, until only single elements are left at the leaves…

3/10/03Tucker, Sec.3-56 Merge Sort—Merging Tree …then merge the sequences in order. Note: This tree is drawn upside down, so that the root is at the bottom. Thus, the set {0, 5} is at level 3

3/10/03Tucker, Sec.3-57 Complexity of Merging A Simplifying Assumption Assume our set has n = 2 r elements. This means that: We always divide exactly in half in the subdivision tree. There are r levels All the leaves in both the subdivision and the merging tree are on level r. There are 2 k vertices on level k The sets on level k all have 2 r-k elements in them n = 2 3 * * * * * * * * * * *

3/10/03Tucker, Sec.3-58 Complexity of Merging Counting the Comparisons At each vertex on level k of the merging tree we merge the two children sets each of size 2 r-k-1. Merging two lists of length L into a single list of length 2L requires 2L-1 comparisons (class exercise). For each vertex on level k, this merging takes 2 (2 r-k-1 ) – 1 = 2 r-k – 1 comparisons There are 2 k vertices on level k, for a total of 2 k (2 r-k – 1 ) = 2 r - 2 k comparisons on each level. Since we must do this on each level, the total number of comparisons for a Merge Sort is Since n = 2 r, this becomes. (Still need to account for the subdivision preprocessing, but this isn’t bad) Thus Merge Sort is and achieves the theoretical bound of a binary search.

3/10/03Tucker, Sec.3-59 Use the first element in the list to partition the list, then put it at the end of the left hand child list. Worst case,, on average QUIK Sort

3/10/03Tucker, Sec Heap Sort A heap is a (nearly) binary tree so that a parent is always bigger than its children (root is biggest of all). Put the root at the beginning of a list, then move up the biggest grandchildren. start of list Preprocessing: Need to first construct the heap (homework…)

3/10/03Tucker, Sec Prove that it takes at most n – 1 comparisons to merge two sorted lists into a single sorted list of length n. Sort {4, 5, 2, 3, 0, 1} using each of a Bubble, Merge, QUIK, and Heap Sort. Class Exercises Note: if, and if, then don’t have to do the last comparison so get r l instead of r l + 1 Merge and where k + l = n. r i of the b’s Each a i must be compared to r i + 1 of the b’s for a total of