CS 146: Data Structures and Algorithms July 14 Class Meeting Department of Computer Science San Jose State University Summer 2015 Instructor: Ron Mak www.cs.sjsu.edu/~mak.

Slides:



Advertisements
Similar presentations
Introduction to Algorithms Quicksort
Advertisements

Algorithms Analysis Lecture 6 Quicksort. Quick Sort Divide and Conquer.
Sorting Comparison-based algorithm review –You should know most of the algorithms –We will concentrate on their analyses –Special emphasis: Heapsort Lower.
§7 Quicksort -- the fastest known sorting algorithm in practice 1. The Algorithm void Quicksort ( ElementType A[ ], int N ) { if ( N < 2 ) return; pivot.
Lower bound for sorting, radix sort COMP171 Fall 2005.
Quick Sort, Shell Sort, Counting Sort, Radix Sort AND Bucket Sort
CSCE 3110 Data Structures & Algorithm Analysis
Spring 2015 Lecture 5: QuickSort & Selection
© 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.
Chapter 7: Sorting Algorithms
Data Structures Data Structures Topic #13. Today’s Agenda Sorting Algorithms: Recursive –mergesort –quicksort As we learn about each sorting algorithm,
Faster Sorting Methods Chapter 12 Slides by Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall.
CSC 2300 Data Structures & Algorithms March 23, 2007 Chapter 7. Sorting.
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.
1 SORTING Dan Barrish-Flood. 2 heapsort made file “3-Sorting-Intro-Heapsort.ppt”
Sorting Heapsort Quick review of basic sorting methods Lower bounds for comparison-based methods Non-comparison based sorting.
CSC 2300 Data Structures & Algorithms March 27, 2007 Chapter 7. Sorting.
2 -1 Analysis of algorithms Best case: easiest Worst case Average case: hardest.
Analysis of Algorithms CS 477/677 Midterm Exam Review Instructor: George Bebis.
Sorting. Introduction Assumptions –Sorting an array of integers –Entire sort can be done in main memory Straightforward algorithms are O(N 2 ) More complex.
CSE 326: Data Structures Sorting Ben Lerner Summer 2007.
Analysis of Algorithms CS 477/677
CSC 2300 Data Structures & Algorithms March 20, 2007 Chapter 7. Sorting.
Chapter 7 (Part 2) Sorting Algorithms Merge Sort.
Sorting Lower Bound1. 2 Comparison-Based Sorting (§ 4.4) Many sorting algorithms are comparison based. They sort by making comparisons between pairs of.
David Luebke 1 8/17/2015 CS 332: Algorithms Linear-Time Sorting Continued Medians and Order Statistics.
Sorting in Linear Time Lower bound for comparison-based sorting
CSE 373 Data Structures Lecture 15
CS 146: Data Structures and Algorithms June 18 Class Meeting Department of Computer Science San Jose State University Summer 2015 Instructor: Ron Mak
Ch. 8 & 9 – Linear Sorting and Order Statistics What do you trade for speed?
CS 46B: Introduction to Data Structures July 30 Class Meeting Department of Computer Science San Jose State University Summer 2015 Instructor: Ron Mak.
CS 146: Data Structures and Algorithms July 21 Class Meeting
CS 46B: Introduction to Data Structures July 7 Class Meeting Department of Computer Science San Jose State University Summer 2015 Instructor: Ron Mak
CSE 221/ICT221 Analysis and Design of Algorithms Lecture 05: Analysis of time Complexity of Sorting Algorithms Dr.Surasak Mungsing
CS 146: Data Structures and Algorithms June 23 Class Meeting Department of Computer Science San Jose State University Summer 2015 Instructor: Ron Mak
CS 146: Data Structures and Algorithms June 23 Class Meeting Department of Computer Science San Jose State University Summer 2015 Instructor: Ron Mak
Merge Sort. What Is Sorting? To arrange a collection of items in some specified order. Numerical order Lexicographical order Input: sequence of numbers.
David Luebke 1 10/13/2015 CS 332: Algorithms Linear-Time Sorting Algorithms.
CSC 41/513: Intro to Algorithms Linear-Time Sorting Algorithms.
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.
Analysis of Algorithms CS 477/677
Sorting. Pseudocode of Insertion Sort Insertion Sort To sort array A[0..n-1], sort A[0..n-2] recursively and then insert A[n-1] in its proper place among.
Mudasser Naseer 1 11/5/2015 CSC 201: Design and Analysis of Algorithms Lecture # 8 Some Examples of Recursion Linear-Time Sorting Algorithms.
1 CSE 326: Data Structures A Sort of Detour Henry Kautz Winter Quarter 2002.
CS 146: Data Structures and Algorithms July 14 Class Meeting Department of Computer Science San Jose State University Summer 2015 Instructor: Ron Mak
CS 146: Data Structures and Algorithms July 9 Class Meeting Department of Computer Science San Jose State University Summer 2015 Instructor: Ron Mak
CS 46B: Introduction to Data Structures July 2 Class Meeting Department of Computer Science San Jose State University Summer 2015 Instructor: Ron Mak
1 COMMONWEALTH OF AUSTRALIA Copyright Regulations 1969 WARNING This material has been reproduced and communicated to you by or on behalf.
Sorting 1. Insertion Sort
CSC 201 Analysis and Design of Algorithms Lecture 05: Analysis of time Complexity of Sorting Algorithms Dr.Surasak Mungsing
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
CS 146: Data Structures and Algorithms July 28 Class Meeting Department of Computer Science San Jose State University Summer 2015 Instructor: Ron Mak
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.
Intro. to Data Structures Chapter 7 Sorting Veera Muangsin, Dept. of Computer Engineering, Chulalongkorn University 1 Chapter 7 Sorting Sort is.
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.
David Luebke 1 6/26/2016 CS 332: Algorithms Linear-Time Sorting Continued Medians and Order Statistics.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 26 Sorting.
David Luebke 1 7/2/2016 CS 332: Algorithms Linear-Time Sorting: Review + Bucket Sort Medians and Order Statistics.
Algorithm Design Techniques, Greedy Method – Knapsack Problem, Job Sequencing, Divide and Conquer Method – Quick Sort, Finding Maximum and Minimum, Dynamic.
Chapter 11 Sorting Acknowledgement: These slides are adapted from slides provided with Data Structures and Algorithms in C++, Goodrich, Tamassia and Mount.
Sorting.
Linear-Time Sorting Continued Medians and Order Statistics
Introduction to Algorithms
CMPE 180A Data Structures and Algorithms in C++ April 19 Class Meeting
Linear-Time Sorting Algorithms
Lower bound for sorting, radix sort
The Selection Problem.
Presentation transcript:

CS 146: Data Structures and Algorithms July 14 Class Meeting Department of Computer Science San Jose State University Summer 2015 Instructor: Ron Mak

Computer Science Dept. Summer 2015: July 14 CS 146: Data Structures and Algorithms © R. Mak 2 Review of Sorting Algorithms  Insertion sort  Shellsort  Heapsort  Mergesort  Quicksort  What is going on with these sorts? Demo

Computer Science Dept. Summer 2015: July 14 CS 146: Data Structures and Algorithms © R. Mak 3 Analysis of Quicksort  What is the running time to quicksort a list of N ?  Partition the array into two subarrays (constant cN time).  A recursive call on each subarray.  A recurrence relation: where i is the number of values in the left partition. T(N) = 1if N = 0 or 1 T(i) + T(N – i – 1) + cNif N > 1 {

Computer Science Dept. Summer 2015: July 14 CS 146: Data Structures and Algorithms © R. Mak 4 Analysis of Quicksort  The performance of quicksort is highly dependent on the quality of the choice of pivot.

Computer Science Dept. Summer 2015: July 14 CS 146: Data Structures and Algorithms © R. Mak 5 Quicksort: Worst Case Analysis  The pivot is always the smallest value of the partition, and so i = 0. Telescope: Add and cancel: T(N) = 1if N = 0 or 1 T(i) + T(N – i – 1) + cNif N > 1 {

Computer Science Dept. Summer 2015: July 14 CS 146: Data Structures and Algorithms © R. Mak 6 Quicksort: Worst Case Analysis, cont’d  The pivot is always the smallest value of the partition, and so i = 0. T(N) = 1if N = 0 or 1 T(i) + T(N – i – 1) + cNif N > 1 {  How does this explain the very bad behavior of quicksort when the data is already sorted?

Computer Science Dept. Summer 2015: July 14 CS 146: Data Structures and Algorithms © R. Mak Quicksort: Worst Case Analysis, cont’d 7 N = 100,000 ALGORITHM MOVES COMPARES MILLISECONDS Insertion sort 0 99,999 0 Shellsort suboptimal 0 1,500,006 4 Shellsort Knuth 0 967,146 4 Heap sort 1,900,851 3,882, Merge sort array 3,337, , Merge sort linked list 1,115, , Quicksort suboptimal 400,000 5,000,150,000 4,857 Quicksort optimal 400,000 1,968,946 6

Computer Science Dept. Summer 2015: July 14 CS 146: Data Structures and Algorithms © R. Mak 8 Quicksort: Best Case Analysis  The pivot is always the median. Each subarray is the same size. Divide through by N : Add and cancel (there are log N equations): Telescope: Therefore: T(N) = 1if N = 0 or 1 T(i) + T(N – i – 1) + cNif N > 1 {

Computer Science Dept. Summer 2015: July 14 CS 146: Data Structures and Algorithms © R. Mak 9 Quicksort: Average Case Analysis  Each size for a subarray after partitioning is equally likely, with probability 1/N: (a) (b) Subtract (a) – (b): T(N) = 1if N = 0 or 1 T(i) + T(N – i – 1) + cNif N > 1 { Since there are two partitions: Multiply by N : Substitute N by N-1 :

Computer Science Dept. Summer 2015: July 14 CS 146: Data Structures and Algorithms © R. Mak 10 Quicksort: Average Case Analysis Rearrange and drop the insignificant –c : Divide through by N(N+1): Telescope: Add and cancel

Computer Science Dept. Summer 2015: July 14 CS 146: Data Structures and Algorithms © R. Mak 11 Quicksort: Average Case Analysis Recall the harmonic number: Add and cancel: And so: Therefore:

Computer Science Dept. Summer 2015: July 14 CS 146: Data Structures and Algorithms © R. Mak 12 Assignment #5  Add heapsort, mergesort, and quicksort to your work for Assignment #4.  Do two versions of mergesort: Sort an array. Sort a linked list.  Do two versions of quicksort: Suboptimal first element as the pivot choice. Median-of-three pivot choice.

Computer Science Dept. Summer 2015: July 14 CS 146: Data Structures and Algorithms © R. Mak 13 Assignment #5  Total sorts: Insertion sort Shellsort (two versions, optimal and suboptimal h sequences) Heapsort Mergesort (two versions, array and linked list) Quicksort (two versions, optimal and suboptimal pivot choices)

Computer Science Dept. Summer 2015: July 14 CS 146: Data Structures and Algorithms © R. Mak 14 Assignment #5, cont’d  For each sort, your program should output: How much time it took. Count comparisons it made between two values. Count moves it made of the values.  Verify that your arrays are properly sorted!  You should output these results in a single table for easy comparison.

Computer Science Dept. Summer 2015: July 14 CS 146: Data Structures and Algorithms © R. Mak 15 Assignment #5, cont’d  You may choose a partner to work with you on this assignment. Both of you will receive the same score.  your answers to Subject line: CS 146 Assignment #5: Your Name(s) CC your partner when you your solution.  Due Friday, July 24 at 11:59 PM.

Computer Science Dept. Summer 2015: July 14 CS 146: Data Structures and Algorithms © R. Mak Break 16

Computer Science Dept. Summer 2015: July 14 CS 146: Data Structures and Algorithms © R. Mak 17 A General Lower Bound for Sorting  Any sorting algorithm that uses only comparisons requires Ω(N log N) comparisons in the worst case.  Prove: Any sorting algorithm that uses only comparisons requires comparisons in the worst case and log(N!) comparisons on average. log(N!) = Ω(N log N)

Computer Science Dept. Summer 2015: July 14 CS 146: Data Structures and Algorithms © R. Mak A General Lower Bound for Sorting, cont’d  Every sorting algorithm that uses only comparisons can be represented by a decision tree. The number of comparisons is equal to the depth of the deepest leaf. 18 Data Structures and Algorithms in Java, 3 rd ed. by Mark Allen Weiss Pearson Education, Inc., 2012 How many possible combinations for 3 elements? 3!

Computer Science Dept. Summer 2015: July 14 CS 146: Data Structures and Algorithms © R. Mak Some Decision Tree Properties  A binary tree of depth d has at most 2 d leaves.  A binary tree with L leaves must have depth at least.  Any sorting algorithm that uses only comparisons between elements requires at least comparisons in the worst case. A decision tree to sort N elements must have N ! leaves. 19

Computer Science Dept. Summer 2015: July 14 CS 146: Data Structures and Algorithms © R. Mak A General Lower Bound for Sorting, cont’d  Prove: Any sorting algorithm that uses only comparisons between elements requires Ω(N log N) comparisons. 20

Computer Science Dept. Summer 2015: July 14 CS 146: Data Structures and Algorithms © R. Mak 21 A General Lower Bound for Sorting Delete the first half of the terms: Replace each remaining term by the smallest one, log(N/2): There are N/2 of these log(N/2) terms: Therefore:

Computer Science Dept. Summer 2015: July 14 CS 146: Data Structures and Algorithms © R. Mak 22 A General Lower Bound for Sorting  Therefore, you cannot devise a sorting algorithm based on comparing elements that will be faster than Ω(N log N) in the worst case.

Computer Science Dept. Summer 2015: July 14 CS 146: Data Structures and Algorithms © R. Mak 23 Bucket Sort and Radix Sort  Bucket sorting relies on using a number of bins, or buckets, into which the values to be sorted are entered.  Sorting time is linear rather than O(N log N ). Does not rely on comparisons.  A form of bucket sort is the radix sort. Used to sort values each of which has a limited number of characters.  Example: 3-digit numbers. Radix sort was used by the old electromechanical IBM card sorters to sort punched cards.

Computer Science Dept. Summer 2015: July 14 CS 146: Data Structures and Algorithms © R. Mak 24 IBM 083 Card Sorter  1950s vacuum-tube and mechanical technology. Sorted up to 1000 cards per minute.

Computer Science Dept. Summer 2015: July 14 CS 146: Data Structures and Algorithms © R. Mak 25 IBM 083 Card Sorter  A punched card had up to 12 punches per column, numbered 0-9 and 11 and 12. The card sorter had 12 bins (plus a reject bin).

Computer Science Dept. Summer 2015: July 14 CS 146: Data Structures and Algorithms © R. Mak 26 IBM 083 Card Sorter  How to sort cards punched with 3-digit numbers (in the same columns): First sort on the units digit.  Each card drops into the appropriate bin based on the units digit.  Carefully remove the cards from the bins, keeping them in order. Next sort on the tens digit.  Each card drops into the appropriate bin based on the 10s digit.  Carefully remove the cards from the bins, keeping them in order. Finally sort on the hundreds digit.  Each card drops into the appropriate bin based on the 100s digit.  Carefully remove the cards from the bins, keeping them in order.

Computer Science Dept. Summer 2015: July 14 CS 146: Data Structures and Algorithms © R. Mak 27 Radix sorting with an old electromechanical punched card sorter.

Computer Science Dept. Summer 2015: July 14 CS 146: Data Structures and Algorithms © R. Mak 28 Magnetic Tape Sorting  Magnetic tape can be read and written in one direction only. You can also rewind a tape.

Computer Science Dept. Summer 2015: July 14 CS 146: Data Structures and Algorithms © R. Mak Magnetic Tape Sorting, cont’d 29

Computer Science Dept. Summer 2015: July 14 CS 146: Data Structures and Algorithms © R. Mak 30 Magnetic Tape Sorting, cont’d  Suppose you have data you want to sort.  The data records initially all reside on one magnetic tape.  You have 4 tape drives and 3 blank tapes.  The computer memory can hold and sort 3 data records at a time.  Perform an external merge sort.

Computer Science Dept. Summer 2015: July 14 CS 146: Data Structures and Algorithms © R. Mak 31 Magnetic Tape Sorting, cont’d T T2 T3 T4 T1 T2 T3 T4 T1 T2 T3 T Can you follow what’s happening?

Computer Science Dept. Summer 2015: July 14 CS 146: Data Structures and Algorithms © R. Mak 32 Magnetic Tape Sorting, cont’d T T T3 T4 T1 T2 T3 T4 T1 T2 T3 T