SORTING AND ASYMPTOTIC COMPLEXITY Lecture 13 CS2110 – Fall 2009.

Slides:



Advertisements
Similar presentations
Introduction to Algorithms Quicksort
Advertisements

SEARCHING AND SORTING HINT AT ASYMPTOTIC COMPLEXITY Lecture 9 CS2110 – Spring 2015 We may not cover all this material.
Algorithms Analysis Lecture 6 Quicksort. Quick Sort Divide and Conquer.
CS Section 600 CS Section 002 Dr. Angela Guercio Spring 2010.
Sorting Comparison-based algorithm review –You should know most of the algorithms –We will concentrate on their analyses –Special emphasis: Heapsort Lower.
ADA: 5. Quicksort1 Objective o describe the quicksort algorithm, it's partition function, and analyse its running time under different data conditions.
Quick Sort, Shell Sort, Counting Sort, Radix Sort AND Bucket Sort
CS4413 Divide-and-Conquer
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
DIVIDE AND CONQUER APPROACH. General Method Works on the approach of dividing a given problem into smaller sub problems (ideally of same size).  Divide.
Quicksort CSE 331 Section 2 James Daly. Review: Merge Sort Basic idea: split the list into two parts, sort both parts, then merge the two lists
Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu Lecture 5.
Spring 2015 Lecture 5: QuickSort & Selection
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.
Lecture 8 Jianjun Hu Department of Computer Science and Engineering University of South Carolina CSCE350 Algorithms and Data Structure.
CS 171: Introduction to Computer Science II Quicksort.
Sorting Heapsort Quick review of basic sorting methods Lower bounds for comparison-based methods Non-comparison based sorting.
Data Structures, Spring 2006 © L. Joskowicz 1 Data Structures – LECTURE 4 Comparison-based sorting Why sorting? Formal analysis of Quick-Sort Comparison.
Merge sort, Insertion sort
TTIT33 Algorithms and Optimization – Dalg Lecture 2 HT TTIT33 Algorithms and optimization Lecture 2 Algorithms Sorting [GT] 3.1.2, 11 [LD] ,
Quicksort CIS 606 Spring Quicksort Worst-case running time: Θ(n 2 ). Expected running time: Θ(n lg n). Constants hidden in Θ(n lg n) are small.
Divide and Conquer Sorting
Merge sort, Insertion sort. Sorting I / Slide 2 Sorting * Selection sort or bubble sort 1. Find the minimum value in the list 2. Swap it with the value.
CSE 326: Data Structures Sorting Ben Lerner Summer 2007.
Analysis of Algorithms CS 477/677
Chapter 7 (Part 2) Sorting Algorithms Merge Sort.
Design and Analysis of Algorithms – Chapter 51 Divide and Conquer (I) Dr. Ying Lu RAIK 283: Data Structures & Algorithms.
Sorting Chapter 6 Chapter 6 –Insertion Sort 6.1 –Quicksort 6.2 Chapter 5 Chapter 5 –Mergesort 5.2 –Stable Sorts Divide & Conquer.
CS2420: Lecture 11 Vladimir Kulyukin Computer Science Department Utah State University.
Sorting Lower Bound1. 2 Comparison-Based Sorting (§ 4.4) Many sorting algorithms are comparison based. They sort by making comparisons between pairs of.
Computer Algorithms Lecture 10 Quicksort Ch. 7 Some of these slides are courtesy of D. Plaisted et al, UNC and M. Nicolescu, UNR.
Sorting and Asymptotic Complexity
Sorting (Part II: Divide and Conquer) CSE 373 Data Structures Lecture 14.
Computer Algorithms Lecture 11 Sorting in Linear Time Ch. 8
Sorting in Linear Time Lower bound for comparison-based sorting
CSE 373 Data Structures Lecture 15
Ch. 8 & 9 – Linear Sorting and Order Statistics What do you trade for speed?
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.
Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “ Introduction to the Design & Analysis of Algorithms, ” 2 nd ed., Ch. 1 Chapter.
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.
Sorting Fun1 Chapter 4: Sorting     29  9.
CS 61B Data Structures and Programming Methodology July 28, 2008 David Sun.
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 Using C++ 2E Chapter 10 Sorting Algorithms.
Merge sort, Insertion sort. Sorting I / Slide 2 Sorting * Selection sort (iterative, recursive?) * Bubble sort.
Mudasser Naseer 1 11/5/2015 CSC 201: Design and Analysis of Algorithms Lecture # 8 Some Examples of Recursion Linear-Time Sorting Algorithms.
1 Sorting Algorithms Sections 7.1 to Comparison-Based Sorting Input – 2,3,1,15,11,23,1 Output – 1,1,2,3,11,15,23 Class ‘Animals’ – Sort Objects.
Chapter 18: Searching and Sorting Algorithms. Objectives In this chapter, you will: Learn the various search algorithms Implement sequential and binary.
Quicksort CSE 2320 – Algorithms and Data Structures Vassilis Athitsos University of Texas at Arlington 1.
Review 1 Selection Sort Selection Sort Algorithm Time Complexity Best case Average case Worst case Examples.
Divide And Conquer A large instance is solved as follows:  Divide the large instance into smaller instances.  Solve the smaller instances somehow. 
Sorting Fundamental Data Structures and Algorithms Aleks Nanevski February 17, 2004.
1 Ch. 2: Getting Started. 2 About this lecture Study a few simple algorithms for sorting – Insertion Sort – Selection Sort (Exercise) – Merge Sort Show.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Sorting.
Today’s Material Sorting: Definitions Basic Sorting Algorithms
Quicksort This is probably the most popular sorting algorithm. It was invented by the English Scientist C.A.R. Hoare It is popular because it works well.
QuickSort. Yet another sorting algorithm! Usually faster than other algorithms on average, although worst-case is O(n 2 ) Divide-and-conquer: –Divide:
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.
Prof. Amr Goneid, AUC1 CSCE 210 Data Structures and Algorithms Prof. Amr Goneid AUC Part 8b. Sorting(2): (n log n) Algorithms.
CS 367 Introduction to Data Structures Lecture 11.
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.
CS6045: Advanced Algorithms Sorting Algorithms. Sorting Input: sequence of numbers Output: a sorted sequence.
Sorting and Asymptotic Complexity
Presentation transcript:

SORTING AND ASYMPTOTIC COMPLEXITY Lecture 13 CS2110 – Fall 2009

InsertionSort 2  Many people sort cards this way  Invariant: everything to left of i is already sorted  Works especially well when input is nearly sorted  Worst-case is O(n 2 )  Consider reverse-sorted input  Best-case is O(n)  Consider sorted input  Expected case is O(n 2 )  Expected number of inversions is n(n–1)/4 //sort a[], an array of int for (int i = 1; i < a.length; i++) { int temp = a[i]; int k; for (k = i; 0 < k && temp < a[k–1]; k––) a[k] = a[k–1]; a[k] = temp; }

SelectionSort 3  To sort an array of size n:  Examine a[0] to a[n–1] ; find the smallest one and swap it with a[0]  Examine a[1] to a[n–1] ; find the smallest one and swap it with a[1]  In general, in step i, examine a[i] to a[n–1] ; find the smallest one and swap it with a[i]  This is the other common way for people to sort cards  Runtime  Worst-case O(n 2 )  Best-case O(n 2 )  Expected-case O(n 2 )

Divide & Conquer? 4  It often pays to  Break the problem into smaller subproblems,  Solve the subproblems separately, and then  Assemble a final solution  This technique is called divide-and-conquer  Caveat: It won’t help unless the partitioning and assembly processes are inexpensive  Can we apply this approach to sorting?

MergeSort 5  Quintessential divide-and-conquer algorithm  Divide array into equal parts, sort each part, then merge  Questions:  Q1: How do we divide array into two equal parts?  A1: Find middle index: a.length/2  Q2: How do we sort the parts?  A2: call MergeSort recursively!  Q3: How do we merge the sorted subarrays?  A3: We have to write some (easy) code

Merging Sorted Arrays A and B 6  Create an array C of size = size of A + size of B  Keep three indices:  i into A  j into B  k into C  Initialize all three indices to 0 (start of each array)  Compare element A[i] with B[j], and move the smaller element into C[k]  Increment i or j, whichever one we took, and k  When either A or B becomes empty, copy remaining elements from the other array ( B or A, respectively) into C

Merging Sorted Arrays C = merged array B A k i j

MergeSort Analysis 8  Outline (detailed code on the website)  Split array into two halves  Recursively sort each half  Merge the two halves  Merge = combine two sorted arrays to make a single sorted array  Rule: always choose the smallest item  Time: O(n) where n is the combined size of the two arrays  Runtime recurrence  Let T(n) be the time to sort an array of size n T(n) = 2T(n/2) + O(n) T(1) = 1  Can show by induction that T(n) is O(n log n)  Alternately, can see that T(n) is O(n log n) by looking at tree of recursive calls

MergeSort Notes 9  Asymptotic complexity: O(n log n)  Much faster than O(n 2 )  Disadvantage  Need extra storage for temporary arrays  In practice, this can be a disadvantage, even though MergeSort is asymptotically optimal for sorting  Can do MergeSort in place, but this is very tricky (and it slows down the algorithm significantly)  Are there good sorting algorithms that do not use so much extra storage?  Yes: QuickSort

QuickSort 10  Intuitive idea  Given an array A to sort, choose a pivot value p  Partition A into two subarrays, AX and AY AX contains only elements ≤ p AY contains only elements ≥ p  Sort subarrays AX and AY separately  Concatenate (not merge!) sorted AX and AY to get sorted A Concatenation is easier than merging – O(1)

pivot partition QuickSort concatenate

QuickSort Questions 12  Key problems  How should we choose a pivot?  How do we partition an array in place?  Partitioning in place  Can be done in O(n) time (next slide)  Choosing a pivot  Ideal pivot is the median, since this splits array in half  Computing the median of an unsorted array is O(n), but algorithm is quite complicated  Popular heuristics:  Use first value in array (usually not a good choice)  Use middle value in array  Use median of first, last, and middle values in array  Choose a random element

In-Place Partitioning 13 How can we move all the blues to the left of all the reds? Keep two indices, LEFT and RIGHT Initialize LEFT at start of array and RIGHT at end of array Invariant:all elements to left of LEFT are blue all elements to right of RIGHT are red Keep advancing indices until they pass, maintaining invaria nt

14 Now neither LEFT nor RIGHT can advance and maintain invariant. We can swap red and blue pointed to by LEFT and RIGHT indices. After swap, indices can continue to advance until next conflict. swap

15  Once indices cross, partitioning is done  If you replace blue with ≤ p and red with ≥ p, this is exactly what we need for QuickSort partitioning  Notice that after partitioning, array is partially sorted  Recursive calls on partitioned subarrays will sort subarrays  No need to copy/move arrays, since we partitioned in place

QuickSort Analysis 16  Runtime analysis (worst-case)  Partition can work badly, producing this:  Runtime recurrence T(n) = T(n–1) + n  This can be solved to show worst-case T(n) is O(n 2 )  Runtime analysis (expected-case)  More complex recurrence  Can solve to show expected T(n) is O(n log n)  Improve constant factor by avoiding QuickSort on small sets  Switch to InsertionSort (for example) for sets of size, say,  9  Definition of small depends on language, machine, etc. p> p

Sorting Algorithm Summary 17  The ones we have discussed  InsertionSort  SelectionSort  MergeSort  QuickSort  Other sorting algorithms  HeapSort (will revisit this)  ShellSort (in text)  BubbleSort (nice name)  RadixSort  BinSort  CountingSort  Why so many? Do computer scientists have some kind of sorting fetish or what?  Stable sorts: Ins, Sel, Mer  Worst-case O(n log n): Mer, Hea  Expected O(n log n): Mer, Hea, Qui  Best for nearly-sorted sets: Ins  No extra space needed: Ins, Sel, Hea  Fastest in practice: Qui  Least data movement: Sel

Lower Bound for Comparison Sorting 18  Goal: Determine the minimum time required to sort n items  Note: we want worst-case, not best-case time  Best-case doesn’t tell us much; for example, we know Insertion Sort takes O(n) time on already-sorted input  Want to know the worst-case time for the best possible algorithm  But how can we prove anything about the best possible algorithm?  We want to find characteristics that are common to all sorting algorithms  Let’s limit attention to comparison- based algorithms and try to count number of comparisons

Comparison Trees 19  Comparison-based algorithms make decisions based on comparison of data elements  This gives a comparison tree  If the algorithm fails to terminate for some input, then the comparison tree is infinite  The height of the comparison tree represents the worst-case number of comparisons for that algorithm  Can show that any correct comparison-based algorithm must make at least n log n comparisons in the worst case a[i] < a[j] yesno

Lower Bound for Comparison Sorting 20  Say we have a correct comparison-based algorithm  Suppose we want to sort the elements in an array B[]  Assume the elements of B[] are distinct  Any permutation of the elements is initially possible  When done, B[] is sorted  But the algorithm could not have taken the same path in the comparison tree on different input permutations

Lower Bound for Comparison Sorting 21  How many input permutations are possible? n! ~ 2 n log n  For a comparison-based sorting algorithm to be correct, it must have at least that many leaves in its comparison tree  to have at least n! ~ 2 n log n leaves, it must have height at least n log n (since it is only binary branching, the number of nodes at most doubles at every depth)  therefore its longest path must be of length at least n log n, and that it its worst-case running time

java.lang.Comparable Interface 22  public int compareTo(T x);  Returns a negative, zero, or positive value  negative if this is before x  0 if this.equals(x)  positive if this is after x  Many classes implement Comparable  String, Double, Integer, Character, Date,…  If a class implements Comparable, then its compareTo method is considered to define that class’s natural ordering  Comparison-based sorting methods should work with Comparable for maximum generality