SORTING AND ASYMPTOTIC COMPLEXITY Lecture 12 CS2110 – Spring 2014 File searchSortAlgorithms.zip on course website (lecture notes for lectures 12, 13) contains.

Slides:



Advertisements
Similar presentations
Order of complexity. Consider four algorithms 1.The naïve way of adding the numbers up to n 2.The smart way of adding the numbers up to n 3.A binary search.
Advertisements

Chapter 14 Recursion Lecture Slides to Accompany An Introduction to Computer Science Using Java (2nd Edition) by S.N. Kamin, D. Mickunas,, E. Reingold.
For(int i = 1; i
SORTING Lecture 12B CS2110 – Spring InsertionSort 2 pre: b 0 b.length ? post: b 0 b.length sorted inv: or: b[0..i-1] is sorted b 0 i b.length sorted.
Chapter 7 Sorting Part II. 7.3 QUICK SORT Example left right pivot i j 5 > pivot and should go to the other side. 2 < pivot and should go to.
Winter 2006CISC121 - Prof. McLeod1 Stuff Assn 5 is available and due Friday, 7pm. Winding down! Possible Remaining topics: –Shell and Radix sorts (not.
SEARCHING AND SORTING HINT AT ASYMPTOTIC COMPLEXITY Lecture 9 CS2110 – Spring 2015 We may not cover all this material.
CS 162 Intro to Programming II Quick Sort 1. Quicksort Maybe the most commonly used algorithm Quicksort is also a divide and conquer algorithm Advantage.
Recitation on analysis of algorithms. runtimeof MergeSort /** Sort b[h..k]. */ public static void mS(Comparable[] b, int h, int k) { if (h >= k) return;
CS 280 Data Structures Professor John Peterson. Project Questions? /CIS280/f07/project5http://wiki.western.edu/mcis/index.php.
1 Sorting Algorithms (Part II) Overview  Divide and Conquer Sorting Methods.  Merge Sort and its Implementation.  Brief Analysis of Merge Sort.  Quick.
CS2420: Lecture 10 Vladimir Kulyukin Computer Science Department Utah State University.
S: Application of quicksort on an array of ints: partitioning.
Radix Sort CSC 172 SPRING 2004 LECTURE 25. Theoretical bound? A * n logn = t A* 524,288 * 19 = 111 A = 1.1*10^-5 1.1*10^-5 * 134,217,728 * 27 = 40,380.
CS2420: Lecture 8 Vladimir Kulyukin Computer Science Department Utah State University.
Selection Sort
1 Algorithmic analysis Introduction. This handout tells you what you are responsible for concerning the analysis of algorithms You are responsible for:
CS2420: Lecture 11 Vladimir Kulyukin Computer Science Department Utah State University.
CS1101: Programming Methodology Aaron Tan.
CSE 221/ICT221 Analysis and Design of Algorithms Lecture 05: Analysis of time Complexity of Sorting Algorithms Dr.Surasak Mungsing
Lecture 6: An Introduction to Trees Neil Ghani University of Strathclyde.
Recitation 11 Analysis of Algorithms and inductive proofs 1.
SEARCHING.  This is a technique for searching a particular element in sequential manner until the desired element is found.  If an element is found.
CS 61B Data Structures and Programming Methodology July 28, 2008 David Sun.
ASYMPTOTIC COMPLEXITY CS2111 CS2110 – Fall
CS-2851 Dr. Mark L. Hornick 1 CS-2852 Data Structures Dr. Mark L. Hornick Office: L341 Phone: web: people.msoe.edu/hornick/
Introduction to Programming (in C++) Complexity Analysis of Algorithms
CS 100Lecture 131 Announcements Exam stats P3 due on Thursday.
ITEC 2620M Introduction to Data Structures Instructor: Prof. Z. Yang Course Website: ec2620m.htm Office: Tel 3049.
Chapter 5 Algorithms (2) Introduction to CS 1 st Semester, 2015 Sanghyun Park.
Selection Sort
CS 284a, 29 October 1997 Copyright (c) , John Thornley1 CS 284a Lecture Tuesday, 29 October, 1997.
Recitation on analysis of algorithms. Formal definition of O(n) We give a formal definition and show how it is used: f(n) is O(g(n)) iff There is a positive.
Data Structures and Algorithms Dr. Manuel E. Bermudez Alter ego to Dr. Sartaj Sahni.
Sorting 1. Insertion Sort
1 Data Structures CSCI 132, Spring 2014 Lecture 1 Big Ideas in Data Structures Course website:
CSC 201 Analysis and Design of Algorithms Lecture 05: Analysis of time Complexity of Sorting Algorithms Dr.Surasak Mungsing
Data Structures, Algorithms, & Applications Instructor: Babak Alipour Slides and book: Sartaj Sahni.
CS 162 Intro to Programming II Insertion Sort 1. Assume the initial sequence a[0] a[1] … a[k] is already sorted k = 0 when the algorithm starts Insert.
In The Name of God. Parallel processing Course Evaluation  Final Exam is closed book( 14 Scores)  Research and Presentation, Quizzes (5 Scores)  No.
0 Introduction to asymptotic complexity Search algorithms You are responsible for: Weiss, chapter 5, as follows: 5.1 What is algorithmic analysis? 5.2.
1 CSE1301 Computer Programming: Where are we now in the CSE1301 syllabus?
QUICKSORT Quicksort is a sort-in-place algorithm that uses “divide-and-conquer”. First, partition the array into two subarrays A and B such that all in.
QuickSort Algorithm 1. If first < last then begin 2. Partition the elements in the subarray first..last so that the pivot value is in place (in position.
Sorting – Lecture 3 More about Merge Sort, Quick Sort.
Lecture No.45 Data Structures Dr. Sohail Aslam.
Midterm review October 02, 2017 Geoffrey Tien.
Programming in Java: lecture 10
Data Structures and Algorithms I
Week 12 - Wednesday CS221.
Chapter 12: Analysis of Algorithms
Animation of Bubble Sort
Complexity Analysis of Algorithms
Sorting.
class PrintOnetoTen { public static void main(String args[]) {
Algorithms Lecture #07 Dr.Sohail Aslam.
Template Functions Lecture 9 Fri, Feb 9, 2007.
ITEC 2620M Introduction to Data Structures
Algorithms Dr. Youn-Hee Han April-May 2013
Order Statistics Def: Let A be an ordered set containing n elements. The i-th order statistic is the i-th smallest element. Minimum: 1st order statistic.
"Organizing is what you do before you do something, so that when you do it, it is not all mixed up." ~ A. A. Milne Sorting Lecture 11 CS2110 – Spring 2019.
Binary Search and Loop invariants
CS100J Lecture 16 Previous Lecture This Lecture Programming concepts
Sorting and Asymptotic Complexity
Searching and Sorting Hint at Asymptotic Complexity
Analysis of Algorithms
Searching and Sorting Hint at Asymptotic Complexity
CS100J Lecture 16 Previous Lecture This Lecture Programming concepts
Searching and Sorting Hint at Asymptotic Complexity
Searching and Sorting Hint at Asymptotic Complexity
Presentation transcript:

SORTING AND ASYMPTOTIC COMPLEXITY Lecture 12 CS2110 – Spring 2014 File searchSortAlgorithms.zip on course website (lecture notes for lectures 12, 13) contains ALL searching/sorting algorithms. Download it and look at algorithms

Execution of logarithmic-space Quicksort 2 /** Sort b[h..k]. */ public static void QS(int[] b, int h, int k) { int h1= h; int k1= k; // inv; b[h..k] is sorted if b[h1..k1] is while (size of b[h1..k1] > 1) { int j= partition(b, h1, k1); // b[h1..j-1] <= b[j] <= b[j+1..k1] if (b[h1..j-1] smaller than b[j+1..k1]) { QS(b, h, j-1); h1= j+1; } else {QS(b, j+1, k1); k1= j-1; } } Last lecture ended with presenting this algorithm. There was no time to explain it. We now show how it is executed in order to illustrate how the invariant is maintained

Call QS(b, 0, 11); 3 public static void QS(int[] b, int h, int k) { int h1= h; int k1= k; // inv; b[h..k] is sorted if b[h1..k1] is while (size of b[h1..k1] > 1) { int j= partition(b, h1, k1); // b[h1..j-1] <= b[j] <= b[j+1..k1] if (b[h1..j-1] smaller than b[j+1..k1]) { QS(b, h, j-1); h1= j+1; } else {QS(b, j+1, k1); k1= j-1; } } h 0 k 11 h1 0 k1 11 Initially, h is 0 and k is 11. The initialization stores 0 and 11 in h1 and k1. The invariant is true since h = h1 and k = k1. j ?

Call QS(b, 0, 11); 4 public static void QS(int[] b, int h, int k) { int h1= h; int k1= k; // inv; b[h..k] is sorted if b[h1..k1] is while (size of b[h1..k1] > 1) { int j= partition(b, h1, k1); // b[h1..j-1] <= b[j] <= b[j+1..k1] if (b[h1..j-1] smaller than b[j+1..k1]) { QS(b, h, j-1); h1= j+1; } else {QS(b, j+1, k1); k1= j-1; } } j 11 h 0 k 11 h1 0 k1 11 The assignment to j partitions b, making it look like what is below. The two partitions are underlined j 2

Call QS(b, 0, 11); 5 public static void QS(int[] b, int h, int k) { int h1= h; int k1= k; // inv; b[h..k] is sorted if b[h1..k1] is while (size of b[h1..k1] > 1) { int j= partition(b, h1, k1); // b[h1..j-1] <= b[j] <= b[j+1..k1] if (b[h1..j-1] smaller than b[j+1..k1]) { QS(b, h, j-1); h1= j+1; } else {QS(b, j+1, k1); k1= j-1; } } j 11 h 0 k 11 h1 0 k1 11 The left partition is smaller, so it is sorted recursively by this call. We have changed the partition to the result.

Call QS(b, 0, 11); 6 public static void QS(int[] b, int h, int k) { int h1= h; int k1= k; // inv; b[h..k] is sorted if b[h1..k1] is while (size of b[h1..k1] > 1) { int j= partition(b, h1, k1); // b[h1..j-1] <= b[j] <= b[j+1..k1] if (b[h1..j-1] smaller than b[j+1..k1]) { QS(b, h, j-1); h1= j+1; } else {QS(b, j+1, k1); k1= j-1; } } j 11 h 0 k 11 h1 3 k1 11 The assignment to h1 is done. j 2 Do you see that the inv is true again? If the underlined partition is sorted, then so is b[h..k]. Each iteration of the loop keeps inv true and reduces size of b[h1..k1].

Divide & Conquer! 7 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 We did this in Quicksort: Partition the array and then sort the two partitions.

MergeSort 8 Quintessential divide-and-conquer algorithm: Divide array into equal parts, sort each part (recursively), then merge Questions:  Q1: How do we divide array into two equal parts? A1: Find middle index: b.length/2  Q2: How do we sort the parts? A2: Call MergeSort recursively!  Q3: How do we merge the sorted subarrays? A3: It takes linear time.

Merging Sorted Arrays A and B into C 9 C: merged array Array B Array A k i j A[0..i-1] and B[0..j-1] have been copied into C[0..k-1]. C[0..k-1] is sorted. Next, put a[i] in c[k], because a[i] < b[j]. Then increase k and i. Picture shows situation after copying{4, 7} from A and {1, 3, 4, 6} from B into C

Merging Sorted Arrays A and B into C 10  Create array C of size: size of A + size of B  i= 0; j= 0; k= 0; // initially, nothing copied  Copy smaller of A[i] and B[j] into C[k]  Increment i or j, whichever one was used, and k  When either A or B becomes empty, copy remaining elements from the other array (B or A, respectively) into C This tells what has been done so far: A[0..i-1] and B[0..j-1] have been placed in C[0..k-1]. C[0..k-1] is sorted.

MergeSort 11 /** Sort b[h..k] */ public static void MS (int[] b, int h, int k) { if (k – h <= 1) return; MS(b, h, (h+k)/2); MS(b, (h+k)/2 + 1, k); merge(b, h, (h+k)/2, k); } merge 2 sorted arrays

QuickSort versus MergeSort 12 /** Sort b[h..k] */ public static void QS (int[] b, int h, int k) { if (k – h <= 1) return; int j= partition(b, h, k); QS(b, h, j-1); QS(b, j+1, k); } /** Sort b[h..k] */ public static void MS (int[] b, int h, int k) { if (k – h <= 1) return; MS(b, h, (h+k)/2); MS(b, (h+k)/2 + 1, k); merge(b, h, (h+k)/2, k); } One processes the array then recurses. One recurses then processes the array. merge 2 sorted arrays

MergeSort Analysis 13 Outline  Split array into two halves  Recursively sort each half  Merge two halves Merge: combine two sorted arrays into one sorted array:  Time: O(n) where n is the total size of the two arrays Runtime recurrence T(n): time to sort array of size n T(1) = 1 T(n) = 2T(n/2) + O(n) Can show by induction that T(n) is O(n log n) Alternatively, can see that T(n) is O(n log n) by looking at tree of recursive calls

MergeSort Notes 14  Asymptotic complexity: O(n log n) Much faster than O(n 2 )  Disadvantage  Need extra storage for temporary arrays  In practice, can be a disadvantage, even though MergeSort is asymptotically optimal for sorting  Can do MergeSort in place, but very tricky (and slows execution significantly)  Good sorting algorithm that does not use so much extra storage? Yes: QuickSort —when done properly, uses log n space.

QuickSort Analysis 15 Runtime analysis (worst-case)  Partition can produce this:  Runtime recurrence: T(n) = T(n–1) + n  Can be solved to show worst-case T(n) is O(n 2 )  Space can be O(n) —max depth of recursion Runtime analysis (expected-case)  More complex recurrence  Can be solved to show expected T(n) is O(n log n) Improve constant factor by avoiding QuickSort on small sets  Use InsertionSort (for example) for sets of size, say, ≤ 9  Definition of small depends on language, machine, etc. p> p

Sorting Algorithm Summary 16 We discussed  InsertionSort  SelectionSort  MergeSort  QuickSort Other sorting algorithms  HeapSort (will revisit)  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: Ins, Sel, Hea Fastest in practice: Qui Least data movement: Sel A sorting algorithm is stable if: equal values stay in same order: b[i] = b[j] and i < j means that b[i] will precede b[j] in result

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

Comparison Trees 18  Comparison-based algorithms make decisions based on comparison of data elements  Gives a comparison tree  If algorithm fails to terminate for some input, comparison tree is infinite  Height of comparison tree represents worst-case number of comparisons for that algorithm  Can show: Any correct comparison- based algorithm must make at least n log n comparisons in the worst case a[i] < a[j] yes no

Lower Bound for Comparison Sorting 19  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 20 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

Interface java.lang.Comparable 21 public int compareTo(T x);  Return 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, …  Class implements Comparable? Its method compareTo is considered to define that class’s natural ordering Comparison-based sorting methods should work with Comparable for maximum generality