Compsci 201 201 More Sorti ting, B Backtra ktracking Par art 1 1 of of 4 Susan Rodger April 3, 2020 4/3/2020 Compsci 201, Spring 2020 1
T is for … • Alan Turing, Turing Test, Turing award • From WWII to philosophy to math to computing • Tree • From Search to Game to … 4/3/2020 Compsci 201, Spring 2020 2
Announcements • APT PT-6 due T Tuesd sday, A Apri ril 7 • Assig ignme ment P t P5 Percola latio tion • Tell us solo or partner form due yesterday • Code due Thursday, April 9 • Disc scuss ssion 11 Monday, A Apri ril 6 6 • Pre-discussion coming soon • Exam 2 i is A s Apri ril 10 • Online, flexible hours, extra time, open notes • Your own work • APT Q Quiz iz 2 2 is April 1 il 11-15 15 • Your own work 4/1/2020 Compsci 201, Spring 2020 3
If you need an extension for work • Fill ill o out ut t the f he form o on n the c he cour urse w web eb page, und under fo forms ms t tab • Then t n take t the e e extens ensio ion • If you u need need m more t e tim ime, t tak ake it it again • Try no not t to get t too far ar b behin ehind! 4/3/2020 Compsci 201, Spring 2020 4
PfFFiA • Finis ish/R h/Revie iew s sorting ing • Loop invariants with efficient and inefficient sorts • How a priority queue works • Bubblesort focus, Oh No! • Backtr ktracki king: NQuee eens ns and and m more • Canonical problem-solving and programming 4/3/2020 Compsci 201, Spring 2020 5
Simple, O(n 2 ) sorts t --- n 2 compa • Selectio tion s sort mparis isons, n n swaps ps • Find min, swap to front, increment front, repeat sort --- n 2 com • In Inse sertion sor ompariso sons, n no o sw swap, sh shift • stable ble , fast on sorted data, slide into place sort --- n 2 ever • Bubble bble s erythi hing ng, s slow* • Catchy name, but slow and ugly* *this isn't everyone's opinion, but it should be • Shell s ll sort : q : quasi-in insertio tion, f fast i in practi tice • Not quadratic with some tweaks 4/3/2020 Compsci 201, Spring 2020 6
Case Study: SelectionSort • Cano nonical O nical O(n 2 ) a algorit ithm hm/co /code e 4/3/2020 Compsci 201, Spring 2020 7
Case Study: SelectionSort th pass, [0,j) is • Invar ariant iant: o : on j th is in in final inal s sorted o order er • Nested loop re-establishes invariant Final order unexamined public void sort(List<T> list) { j for ( int j =0; j < list.size ()-1; j ++) { int min = j ; for ( int k = j +1; k < list.size (); k ++) { if (list.get(k).compareTo(list.get(min)) < 0){ min = k ; } } swap(list,min,j); } } 4/3/2020 Compsci 201, Spring 2020 8
Reminder: Loop Invariant • Statement ent: t : true e ue each t ch time l loop begins ins t to execut cute • During loop execution it may become false • The loop re-establishes the invariant • Typically stated in terms of loop index • Pictures can help reason about code/solution • Help elps t to rea eason f formally and and inf informally ab about the he code e you’re w e writing ing • Can I explain the invariant to someone? 4/3/2020 Compsci 201, Spring 2020 9
Bubblesort isn't much code • Swap ap ad adjacent elem elements w when hen o out ut o of order • From beginning to end, then end-1, end-2, … • After er n n passes es, las ast n n-ele element nts i in place ce 4/3/2020 Compsci 201, Spring 2020 10
Timing of n 2 and other sorts 4/3/2020 Compsci 201, Spring 2020 11
More efficient O(n log n) sorts • Div ivid ide and and co conq nquer s sorts: • Quick s k sort : fast in practice, O(n 2 ) worst case • Merg rge s sort : stable, fast, extra storage • Timsort : http://en.wikipedia.org/wiki/Timsort • Other s sor orts: • Heap sort: priority queue sorting • Radix sort: uses digits/characters (no compare) • O(n l log n) is o optimal f al for c comparing ing • But, Radix is O(n) ?? 4/3/2020 Compsci 201, Spring 2020 12
Stable, Stability • Stab able le: r : respec ect o order er o of equal k l keys when s sorting ing • First sort by shape, then by color: Stable! • Triangle < Square < Circle; Yellow < Green < Red 4/3/2020 Compsci 201, Spring 2020 13
Merge Sort • Idea: Divide and Conquer • Divide list into two halves • Sort both halves (smaller problem) • Merge the two sorted halves 9 5 1 4 3 6 2 7 4/3/2020 Compsci 201, Spring 2020 14
What does recursively sort mean? Merge Sort • Use the same Merge Sort algorithm – Divide list into two halves – Sort both halves (smaller problem) – Merge the two sorted halves 9 5 1 4 9 5 1 4 divide list into 2 halves 5 9 1 4 recursively sort each half 1 4 5 9 merge the two sorted list 4/3/2020 Compsci 201, Spring 2020 16
Merge two sorted lists • Both lists are sorted. 1 4 5 9 2 3 6 7 Find the smallest from front of two lists 4/3/2020 Compsci 201, Spring 2020 18
MergeSort idea for code mergesort(data) n = length of data if n is 1: return data else: d1 = mergesort(first half of data) d2 = mergesort(second half of data) return merge(d1, d2) 4/3/2020 Compsci 201, Spring 2020 27
Time for MergeSort n items: T(n) mergesort(data) n = length of data if n is 1: return data else: d1 = mergesort(first half of data) d2 = mergesort(second half of data) return merge(d1, d2) 4/3/2020 Compsci 201, Spring 2020 28
Quicksort - Idea • Pivot – select and adjust < pivot, pivot, > pivot – Select one of the elements – Put it where it belongs in sorted order – Put elements less than it, to its left – Put elements greater than it, to its right • Recursively sort the elements to its left • Recursively sort the elements to its right 4/3/2020 Compsci 201, Spring 2020 30
Quicksort - Idea • Pivot – select and adjust < pivot, pivot, > pivot • Recursively sort the elements to its left • Recursively sort the elements to its right 5 9 1 4 3 6 2 7 4/3/2020 Compsci 201, Spring 2020 31
Quicksort: fast in practice • Inv nvented in in 1962 b 1962 by Tony ny H Hoar are, d did idn' n't und under erstand recur cursio ion: n: • Canonical T(n) = 2T(n/2)+O(n), but • Worst case is O(n 2 ), bad pivot. Shuffle first? void doQuick(List<T> list, int first, int last) { if (first >= last) return ; int piv = pivot(list,first,last); doQuick(list,first,piv-1); <= X doQuick(list,piv+1,last); X > X } pivot index 4/3/2020 Compsci 201, Spring 2020 33
Pivot is O(n) • Invar ariant iant: : [first,p] <= list.get(first) • Invar ariant iant: : (p,k) > list.get(first) private int pivot(List<T> list, int first, int last){ T piv = list.get(first); ??? <= [first] > [first] int p = first; first p k for ( int k=first+1; k <= last; k++){ if (list.get(k).compareTo(piv) <= 0){ p ++; swap(list,k,p); } <= X X > X } swap(list,p,first); return p; } pivot index 4/3/2020 Compsci 201, Spring 2020 34
https://en.wikipedia.org/wiki/Timsort • Stable, O O(n n lo log n) n) in in average and and w worst, O O(n) n) b bes est! • In practice lots of data is "close" to sorted • Invent ented by Tim P Peter ers for P Pytho hon, n, now i in J Java • Replaced merge sort which is also stable • Engineer ineered ed t to be correct ect, f , fas ast, u useful i ul in pract ctic ice • Theory and explanation not so simple https://www.youtube.com/watch?v=NVIjHj-lrT4 4/3/2020 Compsci 201, Spring 2020 35
Summary of O(n log n) sorts • Ti Timsort: hy hybrid o of mer erge and and ins insertion? • Fast in real world: Python, Java 7+, Android • Wha hat’s the b he bes est O O(n lo n log n) n) sort t to call all? • The one in the library you have access to • Arrays.sort or Collections.sort • Chang anging ing how y you s sort: : • .compareTo() or .compare() 4/3/2020 Compsci 201, Spring 2020 36
sortingwoto or ginooorsttw • http://b //bit it.l .ly/2 /201sprin ing20-04 0403 03-1 4/3/2020 Compsci 201, Spring 2020 37
Brian Fox GNU Bash Shell (developer) First employee at Free Software Foundation First online banking system at Wells Fargo There’s nothing that I am better at than everyone else, except being me. There’s no secret to being me. Follow your interests and work hard at them. Then you will play bass better, program better, cook better, ride motorcycles better, or anything else that you really want to do. https://lifehacker.com/im-brian-fox-author-of-the-bash-shell-and-this-is-how-1820510600 4/3/2020 Compsci 201, Spring 2020 38
Compsci 201 201 More Sorti ting, B Backtra ktracking Par art 2 2 of of 4 Susan Rodger April 3, 2020 4/3/2020 Compsci 201, Spring 2020 39
How does a Priority Queue work? • Imple lemented w wit ith a h a Heap eap • Tree that is stored in an array. • It is NOT a tree • But easier to think of it as a tree • It REALLY is an array 4/3/2020 Compsci 201, Spring 2020 40
Recommend
More recommend