algorithm evaluation Performance

Similar documents
182 review 1. Course Goals

We can use a max-heap to sort data.

2/14/13. Outline. Part 5. Computational Complexity (2) Examples. (revisit) Properties of Growth-rate functions(1/3)

Divide and Conquer Algorithms: Advanced Sorting

Divide and Conquer Algorithms: Advanced Sorting. Prichard Ch. 10.2: Advanced Sorting Algorithms

Table ADT and Sorting. Algorithm topics continuing (or reviewing?) CS 24 curriculum

9/10/12. Outline. Part 5. Computational Complexity (2) Examples. (revisit) Properties of Growth-rate functions(1/3)

Introduction to Computers and Programming. Today

CS 171: Introduction to Computer Science II. Quicksort

SORTING. Comparison of Quadratic Sorts

COS 226 Algorithms and Data Structures Fall Midterm Solutions

CS221: Algorithms and Data Structures. Sorting Takes Priority. Steve Wolfman (minor tweaks by Alan Hu)

Sorting. Task Description. Selection Sort. Should we worry about speed?

Unit Outline. Comparing Sorting Algorithms Heapsort Mergesort Quicksort More Comparisons Complexity of Sorting 2 / 33

Lecture Notes on Quicksort

CSE 373: Data Structures and Algorithms

Sorting. Sorting in Arrays. SelectionSort. SelectionSort. Binary search works great, but how do we create a sorted array in the first place?

Topics Recursive Sorting Algorithms Divide and Conquer technique An O(NlogN) Sorting Alg. using a Heap making use of the heap properties STL Sorting F

Programming II (CS300)

Measuring algorithm efficiency

Divide and Conquer Sorting Algorithms and Noncomparison-based

Lecture 7 Quicksort : Principles of Imperative Computation (Spring 2018) Frank Pfenning

Mergesort again. 1. Split the list into two equal parts

Searching in General

Problem. Input: An array A = (A[1],..., A[n]) with length n. Output: a permutation A of A, that is sorted: A [i] A [j] for all. 1 i j n.

Algorithm Efficiency & Sorting. Algorithm efficiency Big-O notation Searching algorithms Sorting algorithms

COMP2012H Spring 2014 Dekai Wu. Sorting. (more on sorting algorithms: mergesort, quicksort, heapsort)

Sorting. CSE 143 Java. Insert for a Sorted List. Insertion Sort. Insertion Sort As A Card Game Operation. CSE143 Au

Programming II (CS300)

Sorting & Searching (and a Tower)

26 How Many Times Must a White Dove Sail...

Merge Sort. Algorithm Analysis. November 15, 2017 Hassan Khosravi / Geoffrey Tien 1

Sorting. CSC 143 Java. Insert for a Sorted List. Insertion Sort. Insertion Sort As A Card Game Operation CSC Picture

Key question: how do we pick a good pivot (and what makes a good pivot in the first place)?

Better sorting algorithms (Weiss chapter )

QuickSort

COMP Data Structures

Data Structures and Algorithms

Sorting Goodrich, Tamassia Sorting 1

About this exam review

Merge Sort Goodrich, Tamassia Merge Sort 1

UNIT 5C Merge Sort Principles of Computing, Carnegie Mellon University

CSE 373 NOVEMBER 8 TH COMPARISON SORTS

CS 137 Part 8. Merge Sort, Quick Sort, Binary Search. November 20th, 2017

CSE373: Data Structure & Algorithms Lecture 18: Comparison Sorting. Dan Grossman Fall 2013

MPATE-GE 2618: C Programming for Music Technology. Unit 4.2

CIS 121 Data Structures and Algorithms with Java Spring Code Snippets and Recurrences Monday, January 29/Tuesday, January 30

CS2223: Algorithms Sorting Algorithms, Heap Sort, Linear-time sort, Median and Order Statistics

Sorting Algorithms. + Analysis of the Sorting Algorithms

Day 10. COMP1006/1406 Summer M. Jason Hinek Carleton University

DIVIDE AND CONQUER ALGORITHMS ANALYSIS WITH RECURRENCE EQUATIONS

Lecture Notes on Quicksort

CSE 332: Data Structures & Parallelism Lecture 12: Comparison Sorting. Ruth Anderson Winter 2019

CSE 373: Data Structures and Algorithms

Lecture 19 Sorting Goodrich, Tamassia

IS 709/809: Computational Methods in IS Research. Algorithm Analysis (Sorting)

Sorting. Order in the court! sorting 1

Sorting Pearson Education, Inc. All rights reserved.

Unit-2 Divide and conquer 2016

Sorting. Bringing Order to the World

CSC Design and Analysis of Algorithms

Cpt S 122 Data Structures. Sorting

Unit 6 Chapter 15 EXAMPLES OF COMPLEXITY CALCULATION

CSC Design and Analysis of Algorithms. Lecture 6. Divide and Conquer Algorithm Design Technique. Divide-and-Conquer

DIVIDE & CONQUER. Problem of size n. Solution to sub problem 1

MERGESORT & QUICKSORT cs2420 Introduction to Algorithms and Data Structures Spring 2015

8/5/10 TODAY'S OUTLINE. Recursion COMP 10 EXPLORING COMPUTER SCIENCE. Revisit search and sorting using recursion. Recursion WHAT DOES THIS CODE DO?

Sorting. Order in the court! sorting 1

Chapter 4. Divide-and-Conquer. Copyright 2007 Pearson Addison-Wesley. All rights reserved.

// walk through array stepping by step amount, moving elements down for (i = unsorted; i >= step && item < a[i-step]; i-=step) { a[i] = a[i-step];

Hiroki Yasuga, Elisabeth Kolp, Andreas Lang. 25th September 2014, Scientific Programming

Mergesort again. 1. Split the list into two equal parts

Quicksort (Weiss chapter 8.6)

Teach A level Compu/ng: Algorithms and Data Structures

CSE 373 MAY 24 TH ANALYSIS AND NON- COMPARISON SORTING

Objectives To estimate algorithm efficiency using the Big O notation ( 18.2).

Sorting. Sorting. Stable Sorting. In-place Sort. Bubble Sort. Bubble Sort. Selection (Tournament) Heapsort (Smoothsort) Mergesort Quicksort Bogosort

Divide and Conquer. Algorithm Fall Semester

Algorithm for siftdown(int currentposition) while true (infinite loop) do if the currentposition has NO children then return

Advanced Algorithms and Data Structures

Data structures. More sorting. Dr. Alex Gerdes DIT961 - VT 2018

CSE 143. Two important problems. Searching and Sorting. Review: Linear Search. Review: Binary Search. Example. How Efficient Is Linear Search?

Algorithms. MIS, KUAS, 2007 Ding

Sorting is ordering a list of objects. Here are some sorting algorithms

Review implementation of Stable Matching Survey of common running times. Turn in completed problem sets. Jan 18, 2019 Sprenkle - CSCI211

Overview of Sorting Algorithms

Sorting L7.2 Recall linear search for an element in an array, which is O(n). The divide-and-conquer technique of binary search divides the array in ha

Sorting. CPSC 259: Data Structures and Algorithms for Electrical Engineers. Hassan Khosravi

Chapter 10. Sorting and Searching Algorithms. Fall 2017 CISC2200 Yanjun Li 1. Sorting. Given a set (container) of n elements

Computational Complexity Analysis

CSCI 2132 Software Development Lecture 18: Implementation of Recursive Algorithms

Sorting and Selection

Faster Sorting Methods

ITEC2620 Introduction to Data Structures

CS 112 Introduction to Computing II. Wayne Snyder Computer Science Department Boston University

Outline. Quadratic-Time Sorting. Linearithmic-Time Sorting. Conclusion. Bubble/Shaker Sort Insertion Sort Odd-Even Sort

Lesson 12: Recursion, Complexity, Searching and Sorting. Modifications By Mr. Dave Clausen Updated for Java 1_5

7. Sorting I. 7.1 Simple Sorting. Problem. Algorithm: IsSorted(A) 1 i j n. Simple Sorting

Merge Sort fi fi fi 4 9. Merge Sort Goodrich, Tamassia

Plan of the lecture. Quick-Sort. Partition of lists (or using extra workspace) Quick-Sort ( 10.2) Quick-Sort Tree. Partitioning arrays

Transcription:

algorithm evaluation sorting, Big 0 1 Performance formal experimental Correctness / understandability qualitative software metrics Big O, for initial design considerations "big" high level analysis, "O" on order (magnitude) measure representative cases (probabilistic variations), for time / space measurements: performance system code review / structured walkthroughs for maintenance / modification measure, KLOC (thousands lines of code) complexity (graphs,...), for maintenance / modification Size of problem determines degree of algorithm evaluation required.

Big 0 simplifications sorting, Big 0 2 Big O is "on order" analysis, efficiency of algorithms estimate, statement of algorithms efficiency growth rate Time && Memory fn(critical operations, frequency of operations) O(...) simplification of fn(critical operation, frequency of operation) remove constants, assume critical operation(s) are constant ignore constants O(n) O(n -1) ignore low order terms O(n 3 ) O(n 3 + n 2 + n) ignore multiplicative constants O(n 2 ) O(7 * n 2 ) combine growth rates O(n 2 + n) O(n 2 ) + O(n) ignore log base O(log n) O(log 2 n)

Big 0 values sorting, Big 0 3 O(1) constant index into an array, hash functions, pop/push stack, add/remove queue O(n) linear search unsorted array or linked list array, list, or BST traversal O(n 2 ) quadratic simple sorts: selection, insertion 2 nested loop through n items: duplicates! sort O(log n) logarithmic Binary search in a sorted list BST insert, find, Heap insert, remove O(n log n) "n log n" mergesort, quicksort recursive divide and conquer algs O(a n ) exponential tower of Hanoi, recursive fibonacci, where a > 1 permutations

growth rate sorting, Big 0 4 from text, Prichard & Carrano, page 512

Sort && Algorithm analysis Sorting algorithms are good examples for algorithm analysis number of items compare number of times items are swapped Consider the insertion sort v[] // assume v is filled with random numbers for (i = 0; i < v.length -1 ; i++) { // n 1 min = i for (j = i + 1; j < v.length; j++) // n/2 if (v[min] > v[j]) min = j if (v[i] > v[min]) swap(v[i], v[min]) // n/2-1 } sorting, Big 0 5 One would expect n = 1,000 expected simulation n 2 /2 -n/2 comparisons 499,500 499,500 n/2-1 swaps 999 988 n 2 /2-1 n 2 /2 -n/2 + n/2-1 O(n 2 )

Divide and Conquer Recursive Problems 6 MergeSort, sorts an array by divide: recursive split into halves until single "sorted" values conquer: merge the sorted "halves" into larger sorted "wholes", 38 17 42 27 39 12 16 38 17 42 27 39 12 16 38 17 42 27 39 12 16 38 17 27 39 12 16 17 38 27 39 12 16 17 38 42 12 16 27 39 12 16 17 27 38 39 42

Mergesort sorting, Big 0 7 MergeSort sorts within the array using subscripts requires a temporary array ( t[] ) for merging. a merge copies from v[] to t[] and then from t[] to v[] Element v[], t[]; // v's "Element" is comparable // Pre: v initially unsorted, t is empty // Post: v is sorted ms(v, t) split(0, v.length-1); split(int begin, int end) { if (begin < end) { // split and merge int mid = (begin + end) / 2; split(begin, mid); split(mid+1, end); merge(begin, mid, end); } }

merge(int first, int mid, int last){ int first1 = first, last1 = mid; int first2 = mid + 1, last2 = last; int i = first1; sorting, Big 0 8 while((first1 <= last1) && (first2 <= last2)) { if (v[first1].compareto(v[first2]) < 0) { t[i] = v[first1]; first1++; i++; } // increment indicies else { t[i] = v[first2]; first2++; i++; } // increment indicies } while(first1 <= last1) { // copy rest first half t[i] = v[first1]; first1++; i++; } while(first2 < last2) { // copy rest second half t[i] = v[first2]; first2++; i++; } for(i = first; i <= last; i++) // update v with t v[i] = t[i]; }

Merge Sort v[10] sorting, Big 0 9 Before v: 9 1 9 8 7 2 7 3 1 6 split into v[0]..v[4] and v[5]..v[9] split into v[0]..v[2] and v[3]..v[4] split into v[0]..v[1] and v[2]..v[2] split into v[0]..v[0] and v[1]..v[1] merge(0 to 0 and 1 to 1): 1 9 9 8 7 2 7 3 1 6 merge(0 to 1 and 2 to 2): 1 9 9 8 7 2 7 3 1 6 split into v[3]..v[3] and v[4]..v[4] merge(3 to 3 and 4 to 4): 1 9 9 7 8 2 7 3 1 6 merge(0 to 2 and 3 to 4): 1 7 8 9 9 2 7 3 1 6 split into v[5]..v[7] and v[8]..v[9] split into v[5]..v[6] and v[7]..v[7] split into v[5]..v[5] and v[6]..v[6] merge(5 to 5 and 6 to 6): 1 7 8 9 9 2 7 3 1 6 merge(5 to 6 and 7 to 7): 1 7 8 9 9 2 3 7 1 6 split into v[8]..v[8] and v[9]..v[9] merge(8 to 8 and 9 to 9): 1 7 8 9 9 2 3 7 1 6 merge(5 to 7 and 8 to 9): 1 7 8 9 9 1 2 3 6 7 merge(0 to 4 and 5 to 9): 1 1 2 3 6 7 7 8 9 9 After v: 1 1 2 3 6 7 7 8 9 9

Mergesort performance sorting, Big 0 10 Split performance is recursive division Each "m" level 2 m calls Merge performance: merge step is algorithms "most effort" merge (begin, mid, last) 1. n -1 comparisons 2. move n items to t[] 3. move n items back to v[] merges n items first call 3 * n 1 operations each call creates 2 m calls with n/2 items 2 (3 * n/2-1) 3 * n 2 operations the 2 m calls 3 * (n 2 m ) O(n) Each recursion level requires O(n) operations with log 2 n levels O(log n) Mergesort is O ( n * log n)

Quicksort sorting, Big 0 11 Consider an array partitioned by a "pivot" point such that all elements (begin.. pivot-1) < pivot <= (pivot + 1... last) than sort the smaller subarrays values in sorted array < pivot values closer, smaller problem < pivot > = pivot pivot v[] qsort(int begin, int end) if (begin < end) pivot = partition(begin, end) qsort(begin, pivot -1) qsort(pivot + 1, end)

partition sorting, Big 0 12 Consider the array during partitioning There is the pivot, initially at the beginning The subarray S1 with values less than the pivot The subarray S2 with values greater than the pivot The subarray of values not yet "sorted" into S1 or S2 wrt pivot begin end S1 begin unknown end S1 S2 < pivot > = pivot unknown pivot

sorting, Big 0 13 Maintain the invariant S1 has values < pivot and S2 has values >= pivot begin v[] int partition(int begin, int end) pivot = begin low = begin pivot hi = end while (low < hi) while ( (low < hi) && (v[low] <= v[hi]) ) hi-- if (low < hi) swap(low, hi) pivot = hi low++ while ( (low < hi) && (v[low] < v[hi]) ) low++ if (low < hi) swap(low, hi) pivot = low hi-- return pivot unknown end

QuickSort v[10] sorting, Big 0 14 7 4 2 4 5 8 2 2 6 5 qsort(0, 9) Partition(0, 9): s(7, 5), 4 < 7, 2 < 7, 4 < 7, 5 < 7, s(8, 7), s(7, 6), 2 < 7, 2 < 7, pivot = 8 5 4 2 4 5 6 2 2 7 8 qsort(0, 7) Partition(0, 7): s(5, 2), 4 < 5, 2 < 5, 4 < 5, s(5, 5), s(5, 2), s(6, 5), pivot = 5 2 4 2 4 2 5 6 5 7 8 qsort(0, 4) Partition(0, 4): 2 <= 2, 2 <= 4, 2 <= 2, 2 <= 4, pivot = 0 2 4 2 4 2 5 6 5 7 8 qsort(0, -1), halt qsort(1, 4) Partition(1, 4): s(4, 2), 2 < 4, s(4, 4), pivot = 3 2 2 2 4 4 5 6 5 7 8 qsort(1, 2) Partition(1, 2): 2 <= 2, pivot = 1 2 2 2 4 4 5 6 5 7 8 qsort(1, 0), halt qsort(2, 2), halt qsort(4, 4), halt 7 4 2 4 5 8 2 2 6 5 5 4 2 4 5 8 2 2 6 7 5 4 2 4 5 7 2 2 6 8 5 4 2 4 5 6 2 2 7 8 2 4 2 4 5 6 2 5 7 8 2 4 2 4 5 6 2 5 7 8 2 4 2 4 2 6 5 5 7 8 2 4 2 4 2 5 6 5 7 8 2 4 2 4 2 5 6 5 7 8 2 2 2 4 4 5 6 5 7 8 2 2 2 4 4 5 6 5 7 8

sorting, Big 0 15 // 2 2 2 4 4 5 6 5 7 8 from previous page qsort(6, 7) Partition(6, 7): s(6, 5), :: pivot = 7 2 2 2 4 4 5 5 6 7 8 qsort(6, 6), halt qsort(8, 7), halt qsort(9, 9), halt 2 2 2 4 4 5 5 6 7 8 2 2 2 4 4 5 6 5 7 8 2 2 2 4 4 5 5 6 7 8