SEARCHING AND SORTING HINT AT ASYMPTOTIC COMPLEXITY Lecture 9 CS2110 – Spring 2015 We may not cover all this material.

Slides:



Advertisements
Similar presentations
Introduction to Algorithms Quicksort
Advertisements

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.
MATH 224 – Discrete Mathematics
CSE Lecture 3 – Algorithms I
Algorithms Analysis Lecture 6 Quicksort. Quick Sort Divide and Conquer.
CS Section 600 CS Section 002 Dr. Angela Guercio Spring 2010.
Spring 2015 Lecture 5: QuickSort & Selection
Sorting. “Sorting” When we just say “sorting,” we mean in ascending order (smallest to largest) The algorithms are trivial to modify if we want to sort.
Quicksort CS 3358 Data Structures. Sorting II/ Slide 2 Introduction Fastest known sorting algorithm in practice * Average case: O(N log N) * Worst case:
Quicksort COMP171 Fall Sorting II/ Slide 2 Introduction * Fastest known sorting algorithm in practice * Average case: O(N log N) * Worst case: O(N.
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;
Quicksort. Quicksort I To sort a[left...right] : 1. if left < right: 1.1. Partition a[left...right] such that: all a[left...p-1] are less than a[p], and.
Quicksort.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 23 Algorithm Efficiency.
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.
Quicksort
Data Structure Algorithm Analysis TA: Abbas Sarraf
Chapter 7 (Part 2) Sorting Algorithms Merge Sort.
Analysis of Algorithm.
1 Algorithmic analysis Introduction. This handout tells you what you are responsible for concerning the analysis of algorithms You are responsible for:
Computer Algorithms Lecture 10 Quicksort Ch. 7 Some of these slides are courtesy of D. Plaisted et al, UNC and M. Nicolescu, UNR.
SEARCHING, SORTING, AND ASYMPTOTIC COMPLEXITY CS2110 – Spring 2015 Lecture 10 size n … Constant time n ops n + 2 ops 2n + 2 ops n*n ops Pic: Natalie.
Sorting and Asymptotic Complexity
(c) , University of Washington
1 Chapter 24 Developing Efficient Algorithms. 2 Executing Time Suppose two algorithms perform the same task such as search (linear search vs. binary search)
Recitation 11 Analysis of Algorithms and inductive proofs 1.
Analysis of Algorithms
Merge Sort. What Is Sorting? To arrange a collection of items in some specified order. Numerical order Lexicographical order Input: sequence of numbers.
 Pearson Education, Inc. All rights reserved Searching and Sorting.
Searching. RHS – SOC 2 Searching A magic trick: –Let a person secretly choose a random number between 1 and 1000 –Announce that you can guess the number.
C++ Programming: From Problem Analysis to Program Design, Second Edition Chapter 19: Searching and Sorting.
CS 61B Data Structures and Programming Methodology July 28, 2008 David Sun.
ASYMPTOTIC COMPLEXITY CS2111 CS2110 – Fall
CSC 211 Data Structures Lecture 13
CS 100Lecture 131 Announcements Exam stats P3 due on Thursday.
Java Methods Big-O Analysis of Algorithms Object-Oriented Programming
Review 1 Selection Sort Selection Sort Algorithm Time Complexity Best case Average case Worst case Examples.
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.
1 CS November 2010 insertion sort, selection sort, quick sort Do exercises on pp to get familiar with concepts and develop skill. Practice.
SEARCHING, SORTING, AND ASYMPTOTIC COMPLEXITY Lecture 10 CS2110 – Fall
1 CS April 2010 binary search, linear search insertion sort, selection sort, quick sort Do exercises on pp to get familiar with concepts.
1 CS April 2009 Sorting: insertion sort, selection sort, quick sort Do exercises on pp to get familiar with concepts and develop skill.
0 Introduction to asymptotic complexity Search algorithms You are responsible for: Weiss, chapter 5, as follows: 5.1 What is algorithmic analysis? 5.2.
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.
 2006 Pearson Education, Inc. All rights reserved. 1 Searching and Sorting.
Searching CS 110: Data Structures and Algorithms First Semester,
1 CS November 2008 Sorting: insertion sort, selection sort, quick sort Do exercises on pp to get familiar with concepts and develop skill.
SORTING AND ASYMPTOTIC COMPLEXITY Lecture 13 CS2110 – Fall 2009.
329 3/30/98 CSE 143 Searching and Sorting [Sections 12.4, ]
CORRECTNESS ISSUES AND LOOP INVARIANTS Lecture 8 CS2110 – Fall 2014.
1 CS April 2010 insertion sort, selection sort, quick sort Do exercises on pp to get familiar with concepts and develop skill. Practice.
CORRECTNESS ISSUES AND LOOP INVARIANTS Lecture 8 CS2110 – Fall 2015.
Teach A level Computing: Algorithms and Data Structures
Analysis of Algorithms
Algorithm design and Analysis
"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 – Fall 2018.
CS100A Lecture 15, 17 22, 29 October 1998 Sorting
Asymptotic complexity Searching/sorting
"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
Asymptotic complexity
Sorting and Asymptotic Complexity
Quicksort.
Searching and Sorting Hint at Asymptotic Complexity
CS100A Lecture 15, 17 22, 29 October 1998 Sorting
Searching and Sorting Hint at Asymptotic Complexity
Searching and Sorting Hint at Asymptotic Complexity
Searching and Sorting Hint at Asymptotic Complexity
Presentation transcript:

SEARCHING AND SORTING HINT AT ASYMPTOTIC COMPLEXITY Lecture 9 CS2110 – Spring 2015 We may not cover all this material

Last lecture: binary search 2 ? pre: b 0 (sorted) b.length post: b 0 h b.length v inv: b 0 h t b.length v h= –1; t= b.length; while (h != t–1) { int e= (h+t)/2; if (b[e] <= v) h= e; else t= e; } Methodology: 1.Draw the invariant as a combination of pre and post 2.Develop loop using 4 loopy questions. Practice doing this! (sorted)

Binary search: find position h of v = pre: array is sorted post: <= v> v h h = -1 t = 11 h = -1t = 5 h = 2t = 5 h = 3t = 5 h = 3t = Loop invariant: entries h and below are <= v entries t and above are > v entries between h and t are sorted 5

Binary search: an O(log n) algorithm 4 inv: b 0 h t b.length = n v h= –1; t= b.length; while (h != t–1) { int e= (h+t)/2; if (b[e] <= v) h= e; else t= e; } Suppose initially: b.length = 2 k – 1 Initially, h = -1, t = 2 k -1, t - h = 2 k Can show that one iteration sets h or t so that t - h = 2 k-1 e.g. Set e to (h+t)/2 = (2 k – 2)/2 = 2 k-1 – 1 Set t to e, i.e. to 2 k-1 – 1 Then t - h = 2 k-1 – = 2 k-1 Careful calculation shows that: each iteration halves t – h !! Initially t - h = 2 k Loop iterates exactly k times

Binary search: an O(log n) algorithm Search array with elements, only 15 iterations! 5 Bsearch: h= –1; t= b.length; while (h != t–1) { int e= (h+t)/2; if (b[e] <= v) h= e; else t= e; } If n = 2 k, k is called log(n) That’s the base 2 logarithm n log(n) 1 = = = = = Each iteration takes constant time (a few assignments and an if). Bsearch executes ~log n iterations for an array of size n. So the number of assignments and if-tests made is proportional to log n. Therefore, Bsearch is called an order log n algorithm, written O(log n). (We’ll formalize this notation later.)

Linear search: Find first position of v in b (if present) 6 pre: b 0 n = b.length ? v not here ? post: b 0 h n and h = n or b[h] = v inv: b 0 h n v not here ? Find 5 h = 0 h = 1 h = 2 h = 3

7 pre: b 0 n = b.length ? inv: b 0 h n v not here ? h= 0; while (h != b.length && b[h] != v) h= h+1; Worst case: for array of size n, requires n iterations, each taking constant time. Worst-case time: O(n). Expected or average time? n/2 iterations. O(n/2) —is also O(n) v not here ? post: b 0 h n and h = n or b[h] = v Linear search: Find first position of v in b (if present)

Looking at execution speed 8 Process an array of size n size n … Number of operations executed Constant time n ops n + 2 ops 2n + 2 ops n*n ops 2n+2, n+2, n are all “order n” O(n)

9 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 ? A loop that processes elements of an array in increasing order has this invariant inv: b 0 i b.length processed ? InsertionSort

10 inv: b 0 i b.length sorted ? b 0 i b.length ? e.g. Push b[i] to its sorted position in b[0..i], then increase i b 0 i b.length ? What to do in each iteration? ? ? ? ? Loop body (inv true before and after)

InsertionSort 11 Many people sort cards this way Works well when input is nearly sorted Note English statement in body. Abstraction. Says what to do, not how. This is the best way to present it. Later, we can figure out how to implement it with a loop // sort b[], an array of int // inv: b[0..i-1] is sorted for (int i= 1; i < b.length; i= i+1) { Push b[i] to its sorted position in b[0..i] }

InsertionSort 12  Worst-case: O(n 2 ) (reverse-sorted input)  Best-case: O(n) (sorted input)  Expected case: O(n 2 ) Takes time proportional to number of swaps. Finding the right place for b[i] can take i swaps. Worst case takes … n-1 = (n-1)*n/2 swaps. Let n = b.length // sort b[], an array of int // inv: b[0..i-1] is sorted for (int i= 1; i < b.length; i= i+1) { Push b[i] to its sorted position in b[0..i] }

13 pre: b 0 b.length ? post: b 0 b.length sorted inv: b 0 i b.length sorted, = b[0..i-1]Additional term in invariant Keep invariant true while making progress? e.g.: b 0 i b.length Increasing i by 1 keeps inv true only if b[i] is min of b[i..] SelectionSort 13

SelectionSort 14 Another common way for people to sort cards Runtime  Worst-case O(n 2 )  Best-case O(n 2 )  Expected-case O(n 2 ) //sort b[], an array of int // inv: b[0..i-1] sorted // b[0..i-1] <= b[i..] for (int i= 1; i < b.length; i= i+1) { int m= index of minimum of b[i..]; Swap b[i] and b[m]; } sorted, smaller values larger values b 0 i length Each iteration, swap min value of this section into b[i] 14

QuickSort : a recursive algorithm 15 pre: b 0 b.length ? post: b 0 b.length sorted x = x partition step recursion step x [QuickSort]

Partition algorithm of QuickSort 16 Idea Using the pivot value x that is in b[h]: Swap array values around until b[h..k] looks like this: x ? h h+1 k = x h j k pre: post: x is called the pivot

pivot partition j Not yet sorted these can be in any order The 20 could be in the other partition

Partition algorithm x ? h h+1 k = x h j k b b = x h j t k b pre: post: Combine pre and post to get an invariant 18

Partition algorithm 19 = x h j t k b j= h; t= k; while (j < t) { if (b[j+1] <= b[j]) { // Append b[j + 1] to prefix Swap b[j+1] and b[j]; j= j+1; } else { // Prepend b[j + 1] to suffix Swap b[j+1] and b[t]; t= t-1; } Terminate when j = t, so the “?” segment is empty, so diagram looks like result diagram Initially, with j = h and t = k, this diagram looks like the start diagram Takes linear time: O(k+1-h)

Partition algorithm pre: post: t j = x h j t k b inv:

/** Sort b[h..k]. */ public static void QS(int[] b, int h, int k) { if (b[h..k] has < 2 elements) return; Function does the partition algorithm and returns position j of pivot int j= partition(b, h, k); // We know b[h..j–1] <= b[j] <= b[j+1..k] } QuickSort procedure 21 Base case // Sort b[h..j-1] and b[j+1..k] QS(b, h, j-1); QS(b, j+1, k);

22 /** Sort b[h..k]. */ public static void QS(int[] b, int h, int k) { if (b[h..k] has < 2 elements) return; int j= partition(b, h, k); // We know b[h..j–1] <= b[j] <= b[j+1..k] // Sort b[h..j-1] and b[j+1..k] QS(b, h, j-1); QS(b, j+1, k); } Worst-case: quadratic Average-case: O(n log n) Worst-case space: O(n)! --depth of recursion can be n Can rewrite it to have space O(log n) Average-case: O(log n) QuickSort procedure

Worst case quicksort: pivot always smallest value 23 x0 >= x0 j x0 x1 >= x1 j x0 x1 x2 >= x2 j partioning at depth 0 partioning at depth 1 partioning at depth 2

Best case quicksort: pivot always middle value 24 = x0 0 j n depth 0. 1 segment of size ~n to partition. = x1 x0 =x2 Depth 2. 2 segments of size ~n/2 to partition. Depth 3. 4 segments of size ~n/4 to partition. Max depth: about log n. Time to partition on each level: ~n Total time: O(n log n). Average time for Quicksort: n log n. Difficult calculation

25 QuickSort was developed by Sir Tony Hoare, who received the Turing Award in He developed QuickSort in 1958, but could not explain it to his colleague, and gave up on it. Later, he saw a draft of the new language Algol 68 (which became Algol 60). It had recursive procedures, for the first time in a programming language. “Ah!,” he said. “I know how to write it better now.” 15 minutes later, his colleague also understood it. QuickSort

26 Key issue: How to choose a pivot? Choosing pivot  Ideal pivot: the median, since it splits array in half But computing median of unsorted array is O(n), quite complicated Popular heuristics: Use  first array value (not good)  middle array value  median of first, middle, last, values GOOD!  Choose a random element Partition algorithm

Problem is that if the pivot value is always the smallest (or always the largest), the depth of recursion is the size of the array to sort. Eliminate this problem by doing some of it iteratively and some recursively 27 QuickSort with logarithmic space

28 /** Sort b[h..k]. */ public static void QS(int[] b, int h, int k) { int h1= h; int k1= k; // invariant b[h..k] is sorted if b[h1..k1] is sorted while (b[h1..k1] has more than 1 element) { Reduce the size of b[h1..k1], keeping inv true } QuickSort with logarithmic space

29 /** Sort b[h..k]. */ public static void QS(int[] b, int h, int k) { int h1= h; int k1= k; // invariant b[h..k] is sorted if b[h1..k1] is sorted while (b[h1..k1] has more than 1 element) { 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; } } Only the smaller segment is sorted recursively. If b[h1..k1] has size n, the smaller segment has size < n/2. Therefore, depth of recursion is at most log n QuickSort with logarithmic space