CSC 213 Lecture 13: Writing Code & Sorting Lowest Bound.

Slides:



Advertisements
Similar presentations
CPSC 411 Design and Analysis of Algorithms Set 2: Sorting Lower Bound Prof. Jennifer Welch Spring 2011 CPSC 411, Spring 2011: Set 2 1.
Advertisements

Introduction to Algorithms Quicksort
110/6/2014CSE Suprakash Datta datta[at]cse.yorku.ca CSE 3101: Introduction to the Design and Analysis of Algorithms.
Lower Bounds for Sorting, Searching and Selection
Analysis of Algorithms
Sorting Comparison-based algorithm review –You should know most of the algorithms –We will concentrate on their analyses –Special emphasis: Heapsort Lower.
MS 101: Algorithms Instructor Neelima Gupta
Lower bound for sorting, radix sort COMP171 Fall 2005.
Discrete Structure Li Tak Sing( 李德成 ) Lectures
1 Sorting Problem: Given a sequence of elements, find a permutation such that the resulting sequence is sorted in some order. We have already seen: –Insertion.
© 2004 Goodrich, Tamassia Sorting Lower Bound1. © 2004 Goodrich, Tamassia Sorting Lower Bound2 Comparison-Based Sorting (§ 10.3) Many sorting algorithms.
Lower bound for sorting, radix sort COMP171 Fall 2006.
Lecture 5: Linear Time Sorting Shang-Hua Teng. Sorting Input: Array A[1...n], of elements in arbitrary order; array size n Output: Array A[1...n] of the.
CS 253: Algorithms Chapter 8 Sorting in Linear Time Credit: Dr. George Bebis.
2 -1 Analysis of algorithms Best case: easiest Worst case Average case: hardest.
© 2004 Goodrich, Tamassia Sorting Lower Bound1. © 2004 Goodrich, Tamassia Sorting Lower Bound2 Comparison-Based Sorting (§ 10.3) Many sorting algorithms.
CPSC 411, Fall 2008: Set 2 1 CPSC 411 Design and Analysis of Algorithms Set 2: Sorting Lower Bound Prof. Jennifer Welch Fall 2008.
CPSC 411, Fall 2008: Set 2 1 CPSC 311 Analysis of Algorithms Sorting Lower Bound Prof. Jennifer Welch Fall 2009.
CSE 326: Data Structures Sorting Ben Lerner Summer 2007.
Sorting Lower Bound Andreas Klappenecker based on slides by Prof. Welch 1.
Analysis of Algorithms CS 477/677
1 Today’s Material Lower Bounds on Comparison-based Sorting Linear-Time Sorting Algorithms –Counting Sort –Radix Sort.
DAST 2005 Week 4 – Some Helpful Material Randomized Quick Sort & Lower bound & General remarks…
Sorting Lower Bound1. 2 Comparison-Based Sorting (§ 4.4) Many sorting algorithms are comparison based. They sort by making comparisons between pairs of.
Lower Bounds for Comparison-Based Sorting Algorithms (Ch. 8)
CSC 213 – Large Scale Programming. Today’s Goals  Review past discussion of data sorting algorithms  Weaknesses of past approaches & when we use them.
Data Structure & Algorithm Lecture 7 – Linear Sort JJCAO Most materials are stolen from Prof. Yoram Moses’s course.
Sorting in Linear Time Lower bound for comparison-based sorting
CSE 373 Data Structures Lecture 15
Computational Complexity Polynomial time O(n k ) input size n, k constant Tractable problems solvable in polynomial time(Opposite Intractable) Ex: sorting,
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.
CSC 213 Lecture 12: Quick Sort & Radix/Bucket Sort.
CSCE 3110 Data Structures & Algorithm Analysis Sorting (I) Reading: Chap.7, Weiss.
David Luebke 1 10/13/2015 CS 332: Algorithms Linear-Time Sorting Algorithms.
CSC 41/513: Intro to Algorithms Linear-Time Sorting Algorithms.
The Binary Heap. Binary Heap Looks similar to a binary search tree BUT all the values stored in the subtree rooted at a node are greater than or equal.
Sorting Fun1 Chapter 4: Sorting     29  9.
Sorting Lower Bounds Amihood Amir Bar-Ilan University 2014.
Mudasser Naseer 1 11/5/2015 CSC 201: Design and Analysis of Algorithms Lecture # 8 Some Examples of Recursion Linear-Time Sorting Algorithms.
Princeton University COS 423 Theory of Algorithms Spring 2001 Kevin Wayne Average Case Analysis.
Sorting. Sorting Terminology Sort Key –each element to be sorted must be associated with a sort key which can be compared with other keys e.g. for any.
Review 1 Selection Sort Selection Sort Algorithm Time Complexity Best case Average case Worst case Examples.
Sorting 1. Insertion Sort
CSCE 411H Design and Analysis of Algorithms Set 10: Lower Bounds Prof. Evdokia Nikolova* Spring 2013 CSCE 411H, Spring 2013: Set 10 1 * Slides adapted.
1 CSC 421: Algorithm Design & Analysis Spring 2014 Complexity & lower bounds  brute force  decision trees  adversary arguments  problem reduction.
FALL 2005CENG 213 Data Structures1 Priority Queues (Heaps) Reference: Chapter 7.
CS6045: Advanced Algorithms Sorting Algorithms. Heap Data Structure A heap (nearly complete binary tree) can be stored as an array A –Root of tree is.
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 AND ASYMPTOTIC COMPLEXITY Lecture 13 CS2110 – Fall 2009.
Lecture 5 Algorithm Analysis Arne Kutzner Hanyang University / Seoul Korea.
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.
CSE 250 – Data Structures. Today’s Goals  First review the easy, simple sorting algorithms  Compare while inserting value into place in the vector 
David Luebke 1 6/26/2016 CS 332: Algorithms Linear-Time Sorting Continued Medians and Order Statistics.
Sorting and Runtime Complexity CS255. Sorting Different ways to sort: –Bubble –Exchange –Insertion –Merge –Quick –more…
1 Chapter 8-1: Lower Bound of Comparison Sorts. 2 About this lecture Lower bound of any comparison sorting algorithm – applies to insertion sort, selection.
Sorting Lower Bound 4/25/2018 8:49 PM
CPSC 411 Design and Analysis of Algorithms
CPSC 411 Design and Analysis of Algorithms
Introduction to Algorithms
(2,4) Trees 11/15/2018 9:25 AM Sorting Lower Bound Sorting Lower Bound.
Lecture 5 Algorithm Analysis
Lecture 5 Algorithm Analysis
(2,4) Trees 12/4/2018 1:20 PM Sorting Lower Bound Sorting Lower Bound.
CS200: Algorithm Analysis
Linear-Time Sorting Algorithms
Lecture 5 Algorithm Analysis
(2,4) Trees 2/28/2019 3:21 AM Sorting Lower Bound Sorting Lower Bound.
Copyright © Aiman Hanna All rights reserved
CPSC 411 Design and Analysis of Algorithms
Lecture 5 Algorithm Analysis
Presentation transcript:

CSC 213 Lecture 13: Writing Code & Sorting Lowest Bound

Writing Code Need Not Be Hard Writing code can be difficult Need to figure out how to start, what to do next, how to test, etc. But you already know many things Algorithms, how to draw concepts, what you will need for the node class Key is to use what these ideas, pictures, and algorithms to do the hard work

Our Node Class public class Node { // Usual fields, constructors included public void setLeft(Node n) {... } public void setRight(Node n) {... } public void setParent(Node n) {... } public void setRed(boolean b) {... } public Node getLeft() {... } public Node getRight() {... } public Node getParent() {... } public boolean getRed() {... } public boolean isLeftChild() {... } public int getHeight() {... } }

Working from Concepts We can always create imaginary situations and draw the picture Suppose we must write rightRightSplay Make simple tree that provides examples Note: This is also an excellent test case

Working from Concepts Suppose we must write rightRightSplay Draw Splay tree after we insert 88 Note: This is also a good test case

Working From Analogues May find similar, but not identical, example Could try working from picture or draw actual example we are interested in Code rightRotate and have this picture

Working From Pictures Easiest case: work from picture in book Now lets write rightLeftDRed(Node z)

Time Needed for n 2 Sorts

Time Needed for n log n Sorts

Comparison-Based Sorting (§ 10.3) Most sorting algorithms compare elements Examples: bubble-sort, selection-sort, insertion- sort, heap-sort, merge-sort, quick-sort,... What is minimum running time of algorithm that sorts n elements, x 1, x 2, …, x n ? Is x i < x j ? yes no

Counting Comparisons Consider a run as path in decision tree We will only count number of comparisons Height of tree is lower-bound on running time Is x i < x j ? yes no

Decision Tree Height Each input combination leads to unique leaf node Else make same decisions for two input combinations  E.g. In sequence 4, 5 and sequence 5, 4 we determine (4 < 5) == (5 < 4) How many different ways can we arrange n numbers in a Sequence?

Decision Tree Height If there are n ! leaf nodes, the decision tree height is _______

The Lower Bound Comparison-based sorting algorithms takes at least log( n !) time, but: n ! = n * n-1 * n-2 * n-3 * … * 2 * 1 n ! > (n/2) n/2, since n / 2 numbers bigger than n / 2 log( n !) > log (n/2) n/2 log (n/2) n/2 = n/2 * log (n/2) So, log( n !) > n/2 * log (n/2)

Lower Bound on Sorting Minimum number of comparisons is equal to the height of the tree The height of the tree is log( n !) But log( n !) > n/2 * log (n/2) So, height of tree is larger than O ( n log n ) Therefore, any comparison-based sort will take at least O ( n log n ) time!