CSC 2300 Data Structures & Algorithms March 27, 2007 Chapter 7. Sorting.

Slides:



Advertisements
Similar presentations
Introduction to Algorithms Quicksort
Advertisements

Algorithms Analysis Lecture 6 Quicksort. Quick Sort Divide and Conquer.
Analysis of Algorithms CS 477/677 Linear Sorting Instructor: George Bebis ( Chapter 8 )
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.
1 Selection --Medians and Order Statistics (Chap. 9) The ith order statistic of n elements S={a 1, a 2,…, a n } : ith smallest elements Also called selection.
Chapter 4: Divide and Conquer Master Theorem, Mergesort, Quicksort, Binary Search, Binary Trees The Design and Analysis of Algorithms.
Using Divide and Conquer for Sorting
Spring 2015 Lecture 5: QuickSort & Selection
Quicksort CS 3358 Data Structures. Sorting II/ Slide 2 Introduction Fastest known sorting algorithm in practice * Average case: O(N log N) * Worst case:
25 May Quick Sort (11.2) CSE 2011 Winter 2011.
CSC 2300 Data Structures & Algorithms March 23, 2007 Chapter 7. Sorting.
Sorting Chapter Sorting Consider list x 1, x 2, x 3, … x n We seek to arrange the elements of the list in order –Ascending or descending Some O(n.
Lecture 7COMPSCI.220.FS.T Algorithm MergeSort John von Neumann ( 1945 ! ): a recursive divide- and-conquer approach Three basic steps: –If the.
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.
Lower bound for sorting, radix sort COMP171 Fall 2006.
CS 253: Algorithms Chapter 8 Sorting in Linear Time Credit: Dr. George Bebis.
Data Structures, Spring 2006 © L. Joskowicz 1 Data Structures – LECTURE 4 Comparison-based sorting Why sorting? Formal analysis of Quick-Sort Comparison.
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.
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.
TTIT33 Algorithms and Optimization – Dalg Lecture 2 HT TTIT33 Algorithms and optimization Lecture 2 Algorithms Sorting [GT] 3.1.2, 11 [LD] ,
Analysis of Algorithms CS 477/677
CSC 2300 Data Structures & Algorithms March 20, 2007 Chapter 7. Sorting.
1 Today’s Material Lower Bounds on Comparison-based Sorting Linear-Time Sorting Algorithms –Counting Sort –Radix Sort.
Lecture 8 Sorting. Sorting (Chapter 7) We have a list of real numbers. Need to sort the real numbers in increasing order (smallest first). Important points.
Tirgul 4 Order Statistics Heaps minimum/maximum Selection Overview
Sorting Rearrange n elements into ascending order. 7, 3, 6, 2, 1  1, 2, 3, 6, 7.
DAST 2005 Week 4 – Some Helpful Material Randomized Quick Sort & Lower bound & General remarks…
Selection1. 2 The Selection Problem Given an integer k and n elements x 1, x 2, …, x n, taken from a total order, find the k-th smallest element in this.
The Complexity of Algorithms and the Lower Bounds of Problems
Chapter 7 (Part 2) Sorting Algorithms Merge Sort.
Lecture 8 Sorting. Sorting (Chapter 7) We have a list of real numbers. Need to sort the real numbers in increasing order (smallest first). Important points.
Sorting Lower Bound1. 2 Comparison-Based Sorting (§ 4.4) Many sorting algorithms are comparison based. They sort by making comparisons between pairs of.
CS 146: Data Structures and Algorithms July 14 Class Meeting Department of Computer Science San Jose State University Summer 2015 Instructor: Ron Mak
Sorting in Linear Time Lower bound for comparison-based sorting
CSE 373 Data Structures Lecture 15
Divide-And-Conquer Sorting Small instance.  n
1 Chapter 1 Analysis Basics. 2 Chapter Outline What is analysis? What to count and consider Mathematical background Rates of growth Tournament method.
Order Statistics The ith order statistic in a set of n elements is the ith smallest element The minimum is thus the 1st order statistic The maximum is.
Merge Sort. What Is Sorting? To arrange a collection of items in some specified order. Numerical order Lexicographical order Input: sequence of numbers.
CSC 41/513: Intro to Algorithms Linear-Time Sorting Algorithms.
Order Statistics. Order statistics Given an input of n values and an integer i, we wish to find the i’th largest value. There are i-1 elements smaller.
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 Fun1 Chapter 4: Sorting     29  9.
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.
Data Structures and Algorithms (AT70.02) Comp. Sc. and Inf. Mgmt. Asian Institute of Technology Instructor: Prof. Sumanta Guha Slide Sources: CLRS “Intro.
Princeton University COS 423 Theory of Algorithms Spring 2001 Kevin Wayne Average Case Analysis.
CS 146: Data Structures and Algorithms July 14 Class Meeting Department of Computer Science San Jose State University Summer 2015 Instructor: Ron Mak
Divide And Conquer A large instance is solved as follows:  Divide the large instance into smaller instances.  Solve the smaller instances somehow. 
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,
CSC 413/513: Intro to Algorithms Solving Recurrences Continued The Master Theorem Introduction to heapsort.
Sorting Fundamental Data Structures and Algorithms Aleks Nanevski February 17, 2004.
1 Merge Sort 7 2  9 4   2  2 79  4   72  29  94  4.
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.
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.
Chapter 11 Sorting Acknowledgement: These slides are adapted from slides provided with Data Structures and Algorithms in C++, Goodrich, Tamassia and Mount.
Sorting We have actually seen already two efficient ways to sort:
CS200: Algorithm Analysis
Topic 5: Heap data structure heap sort Priority queue
Lower bound for sorting, radix sort
The Selection Problem.
Sorting We have actually seen already two efficient ways to sort:
Presentation transcript:

CSC 2300 Data Structures & Algorithms March 27, 2007 Chapter 7. Sorting

Today – Sorting Quicksort  Implementation Quickselect  Algorithm Decision Trees Bucket Sort Summary

Quicksort – Efficient Implementation Is recursion good for all values of N? No. What should we do for small values of N?

Worst Case Bound Quicksort: O(N 2 ). Heapsort: O(N log N). Can we combine the two algorithms to achieve a worst-case O(N log N) bound? Problem Modify quicksort to call heapsort if the level of recursion has reached 2 log N. Why would this work? Consider worst case analysis.

Quicksort Code Lines 19 and 20 show why Quicksort is so fast.

Selection Problem Page 1 of text. Given a set of N numbers, determine the k th largest number. How to solve this problem? Sort the numbers in decreasing order, and return the number in the k th position. Can you improve on this scheme?

Selection Problem – Modified Sort Problem: Given a set of N numbers, determine the k th largest number. How to do better than just sorting the numbers? Read the first k numbers into an array, and sort the numbers in decreasing order. Next, each remaining number is read one by one. Do you know how to complete the algorithm?

Selection Problem – Heapsort Problem: Given a set of N numbers, determine the k th largest number. What is the time bound if we use a heap? Furthermore, what is the bound if k = N/2 (i.e., if we want to find the median)?

Selection Problem – Quicksort Problem: Given a set of N numbers, determine the k th largest number. Quicksort is very fast in sorting N numbers. Thus, quicksort should be very fast in selecting the k th largest number. What is the work that we can save?

Quicksort – Algorithm 1. If the number of elements in S is 0 or 1, then return. 2. Pick any element v in S. This is called the pivot. 3. Partition S – {v} into two disjoint groups: S 1 = { x ε S – {v} | x ≤ v} and S 2 = { x ε S – {v} | x ≥ v}. 4. Return { quicksort(S 1 ) followed by v followed by quicksort(S 2 )}.  Where can we save work if we just want to find the k th largest number?  Quickselect makes only one recursive call (instead of two).

Quickselect – Code

Quicksort – Analysis Time Bounds –  Worst case  Best case  Average case What would be the corresponding bounds for Quickselect? In particular, the average case for Quickselect?

Average Case Analysis Assume that each of the sizes for S 1 is equally likely and thus has probability 1/N. The average value of T(i) is thus (1/N) ∑ T(j). Quicksort recurrence becomes T(N) = (2/N) ∑ T(j) + cN. What is recurrence for Quickselect? Quickselect recurrence is T(N) = (1/N) ∑ T(j) + cN. See Problem What is the answer?

Decision Tree A decision tree is an abstraction used to prove lower bounds. In our context, it is a binary tree. Each node represents a set of possible orderings. The results of the comparisons are the tree edges.

Decision Tree – Example Sorting three numbers.

Decision Tree Every algorithm that sorts by using only comparisons can be represented by a decision tree. The number of comparisons used the sorting algorithm is equal to the depth of the deepest leaf. The average number of comparisons used is equal to the average depth of the leaves.

Theory Lemma 7.1. let T be a binary tree of depth d. Then T has at most 2 d leaves. Lemma 7.2. A binary tree with L leaves must have depth at least [log L]. Theorem 7.6. Any sorting algorithm that uses only comparisons between elements requires at least [log (N!)] comparisons in the worst case. [ ] represents ceiling above. Stirling’s formula in Problem Theorem 7.7. any sorting algorithm that uses only comparisons between elements requires Ω(N log N) comparisons.

Linear Time Sorting We have shown that any general sorting algorithm that uses only comparisons requires Ω(N log N) time in the worst case. Now we describe bucket sort, which is a linear time algorithm. Is this a contradiction?

Bucket Sort Say the input A 1, A 2, …,A N consists of only positive integers smaller than M. Keep an array called count, of size M, which is initialized to all 0’s. Thus, count has M cells, or buckets, which are initially empty. When A i is read, increment count[A i ] by 1. After all the input is read, scan the count array, printing out a representation of the sorted list. How much time does the algorithm require? What if M = O(N)? Have we violated the Ω(N log N) lower bound?

Summary