Applied Combinatorics, 4th Ed. Alan Tucker

Slides:



Advertisements
Similar presentations
© Copyright 2012 by Pearson Education, Inc. All Rights Reserved. 1 Chapter 17 Sorting.
Advertisements

Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 24 Sorting.
Sorting and Searching. Searching List of numbers (5, 9, 2, 6, 3, 4, 8) Find 3 and tell me where it was.
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.
CS 104 Introduction to Computer Science and Graphics Problems Data Structure & Algorithms (3) Recurrence Relation 11/11 ~ 11/14/2008 Yang Song.
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.
03/01/2005Tucker, Sec Applied Combinatorics, 4th Ed. Alan Tucker Section 3.1 Properties of Trees Prepared by Joshua Schoenly and Kathleen McNamara.
The Fundamentals: Algorithms, the Integers & Matrices.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 26 Sorting.
New Mexico Computer Science For All Sorting Algorithms Maureen Psaila-Dombrowski.
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.
CS223 Advanced Data Structures and Algorithms 1 Priority Queue and Binary Heap Neil Tang 02/09/2010.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 23 Algorithm Efficiency.
Internal and External Sorting External Searching
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 25 Sorting.
CSE 326: Data Structures Lecture 23 Spring Quarter 2001 Sorting, Part 1 David Kaplan
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.
Sorting & Lower Bounds Jeff Edmonds York University COSC 3101 Lecture 5.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 26 Sorting.
Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 23 Sorting.
Priority Queues and Heaps. John Edgar  Define the ADT priority queue  Define the partially ordered property  Define a heap  Implement a heap using.
The Growth of Functions: Selected Exercises
Chapter 23 Sorting Jung Soo (Sue) Lim Cal State LA.
Chapter 24 Sorting.
Advanced Algorithms Analysis and Design
Recursive Algorithms Section 5.4.
Searching and Sorting Algorithms
Growth of Functions & Algorithms
B-Trees B-Trees.
Introduction to Analysis of Algorithms
Introduction to Analysis of Algorithms
Randomized Algorithms
Chapter 4 Divide-and-Conquer
The Growth of Functions
Design and Analysis of Algorithms
Divide and Conquer.
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Lecture 16: Parallel Algorithms I
Sorting Algorithms Written by J.J. Shepherd.
(2,4) Trees 11/15/2018 9:25 AM Sorting Lower Bound Sorting Lower Bound.
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Algorithm design and Analysis
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Lower Bound Theory.
Section 10 Questions Heaps.
Complexity Present sorting methods. Binary search. Other measures.
CSC212 Data Structure - Section RS
Priority Queue and Binary Heap Neil Tang 02/12/2008
Randomized Algorithms
ITEC 2620M Introduction to Data Structures
CE 221 Data Structures and Algorithms
CS200: Algorithms Analysis
Algorithmic Complexity
Divide and Conquer Algorithms Part I
(2,4) Trees 2/28/2019 3:21 AM Sorting Lower Bound Sorting Lower Bound.
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Time Complexity Lecture 15 Mon, Feb 27, 2006.
Intro to Computer Science CS1510 Dr. Sarah Diesburg
CENG 351 Data Management and File Structures
Mark Redekopp David Kempe
CSC 380: Design and Analysis of Algorithms
Heaps & Multi-way Search Trees
Algorithms.
Analysis of Algorithms CS 477/677
Presentation transcript:

Applied Combinatorics, 4th Ed. Alan Tucker Section 3.4 Tree Analysis of Sorting Algorithms Prepared by: Nathan Rounds and David Miller 2/24/2019 Tucker, Sec. 3.4

“Big O” Notation − If we have a function f (x) defined f (x) = 5x³ + x – 4, we can approximate its value as a constant multiple of x³ for large values of x, since 5x³ is the dominating term in the function (it grows the fastest as x gets large). − For x = 5, f (x) = 626 and the constant multiple 5(x³) = 625 which is a very close estimate. − Therefore, we say that f (x) in “Big O” notation is O(x³). 2/24/2019 Tucker, Sec. 3.4

Theorem In the worst case, the number of binary comparisons required to sort n items is at least O(nlog2n). This number is a constant multiple of (nlog2n) which approximates the height of a binary testing tree (log2n!) for large values of n. 2/24/2019 Tucker, Sec. 3.4

Bubble Sort { For m from 2 to n do: For i from n (step-1) to m do: If Ai Ai-1, then interchange items Ai and Ai-1. 1 4 2 6 3 5 1 4 6 2 3 5 4 1 6 3 2 5 4 6 3 1 2 5 1 4 6 3 2 5 4 6 1 3 2 5 4 6 3 1 5 2 1 2 4 6 3 5 i = 1 i = 2 i = 3 i = 4 i = 5 i = 6 This technique moves the smallest number to the top, then the next smallest number below that until the set of numbers is in numerical order. m = 3 i = 2 m = 3 i = 4 m = 3 i = 3 m = 3 i = 6 m = 3 i = 5 m = 2 i = 3 m = 2 i = 6 m = 2 i = 2 m = 2 i = 5 m = 2 i = 4 { 2/24/2019 Tucker, Sec. 3.4

Bubble Sort Comparisons When m=2, I goes from n to 2, which means you have to do (n-1) comparisons When m=3, I goes from n to 3, which means you have to do (n-2) comparisons Continuing this trend, the total number of comparisons must be: This shows that the Bubble Sort method will require comparisons, which is more then the theoretical bound of . 2/24/2019 Tucker, Sec. 3.4

Merge Sort – Setup Roughly splits the set in half, until only single elements are left. 5 4 0 9 2 6 7 1 3 8 5 4 0 9 2 6 7 1 3 8 5 4 0 9 2 67 1 3 8 5 4 9 2 6 7 1 3 8 5 4 6 7 2/24/2019 Tucker, Sec. 3.4

Note: The root of the tree Merge Sort - Ordering The set then gets “merged” into numerical order. 5 4 9 2 6 7 1 3 8 4 5 6 7 2 9 3 8 0 4 5 1 6 7 0 2 4 5 9 1 3 6 7 8 Note: The root of the tree is on the bottom 0 1 2 3 4 5 6 7 8 9 2/24/2019 Tucker, Sec. 3.4

Complexity of Merging Assume that every set has n = 2r elements There are r levels There are 2k vertices on level k On level k, each set will have 2r-k elements n = 24 ++++++++++++++++ ++++++++ ++++ ++ + Level 0 Level 1 Level 2 Level 3 Level 4 2/24/2019 Tucker, Sec. 3.4

Merge Sort Comparisons In general, at each vertex on level k two ordered sublists of 2r-k-1 items are merged into an ordered sublist of 2r-k items This merging requires 2r-k-1 comparisons The two ordered sublists on level one merge into the root The number of comparisons needed at level k is 2k (2r-k – 1 ) = 2r - 2k since there are 2k different vertices Totaling the number of comparisons needed for a Merge Sort is: With n = 2r this becomes: This shows that Merge Sort method requires comparisons, which achieves the theoretical bound of a binary search. 2/24/2019 Tucker, Sec. 3.4

QUIK Sort Takes the first element and compares it with the other elements to divide the list, putting smaller elements into a set on the left and the larger elements into a set on the right. 5 4 0 7 1 6 8 2 3 9 Puts the first element at the end of the left list 4 0 1 2 3 5 7 6 8 9 0 1 2 3 4 5 6 7 8 9 Once an element is alone there is no need to continue to divide it 2 1 3 4 6 7 8 9 1 2 3 4 1 2 3 4 2/24/2019 Tucker, Sec. 3.4

Heap Sort A binary tree so that the parent is always bigger than its children Put the root at the beginning of a list, then move up the next biggest grandchildren. 6 LIST: 5 3 4 2 1 2/24/2019 Tucker, Sec. 3.4

Class Exercise Sort the following set of numbers using a bubble sort and then do it using a merge sort. Which sort technique is more efficient for this set? 2/24/2019 Tucker, Sec. 3.4

Bubble Sort Solution . . . 5 3 8 23 -6 2 66 5 3 8 -6 23 2 66 5 3 -6 8 2 66 5 3 8 -6 23 2 66 5 3 -6 8 23 2 66 5 -6 3 8 23 2 66 -6 5 3 8 23 2 66 -6 5 3 2 8 23 66 -6 5 2 3 8 23 66 -6 2 5 3 8 23 66 -6 2 3 5 8 23 66 . . . 2/24/2019 Tucker, Sec. 3.4

Merge Sort Solution 5 3 8 23 -6 0 2 66 5 3 8 23 -6 0 2 66 -6 0 2 66 5 3 8 23 -6 0 2 66 5 3 8 23 -6 0 2 66 -6 0 2 66 8 23 5 3 3 23 -6 66 5 8 2 2/24/2019 Tucker, Sec. 3.4

Merge Sort Solution cont’d 3 23 -6 66 5 8 2 3 5 8 23 -6 0 2 66 3 5 8 23 -6 0 2 66 -6 0 2 3 5 8 23 66 2/24/2019 Tucker, Sec. 3.4

Something to Ponder How does one create an initial heap to perform a heap sort on? This will be on the homework.  2/24/2019 Tucker, Sec. 3.4