Presentation is loading. Please wait.

Presentation is loading. Please wait.

Compsci 201 Comparators, Sorting

Similar presentations


Presentation on theme: "Compsci 201 Comparators, Sorting"— Presentation transcript:

1 Compsci 201 Comparators, Sorting
Owen Astrachan November 7, 2018 11/7/2018 Compsci 201, Fall 2018, Sorting + Comparators

2 Compsci 201, Fall 2018, Sorting + Comparators
R is for … Recursion Base case of wonderful stuff Refactoring No new functionality 11/7/2018 Compsci 201, Fall 2018, Sorting + Comparators

3 Compsci 201, Fall 2018, Sorting + Comparators
Plan for Today Sorting: From Algorithms to APIs Top to bottom and back again Review: invariants, helper methods, all-green How do you develop programs? Midterm performance and APT quiz Recursion and more 11/7/2018 Compsci 201, Fall 2018, Sorting + Comparators

4 Compsci 201, Fall 2018, Sorting + Comparators
Sorting: 201 in a Algorithms: traditionally foundation of compsci Study, analyze, develop, use APIs: tested, proven, configurable Algorithms encapsulated and usable You should know how to write your own sort You should know how to call library sort 11/7/2018 Compsci 201, Fall 2018, Sorting + Comparators

5 Sorting: From Theory to Practice
Why do we study more than one algorithm? Paradigms of trade-offs and algorithmic design Know your history How do you use and configure library sort? Not Yogi 11/7/2018 Compsci 201, Fall 2018, Sorting + Comparators

6 Compsci 201, Fall 2018, Sorting + Comparators
Simple, O(n2) sorts Selection sort --- n2 comparisons, n swaps Find min, swap to front, increment front, repeat Insertion sort --- n2 comparisons, no swap, shift stable, fast on sorted data, slide into place Bubble sort --- n2 everything, slow* Catchy name, but slow and ugly* *this isn't everyone's opinion, but it should be Shell sort: quasi-insertion, fast in practice Not quadratic with some tweaks 11/7/2018 Compsci 201, Fall 2018, Sorting + Comparators

7 Case Study: SelectionSort
Canonical O(n2) algorithm/code 11/7/2018 Compsci 201, Fall 2018, Sorting + Comparators

8 Case Study: SelectionSort
Invariant: on jth pass, [0,j) is in final sorted order Nested loop re-establishes invariant Final order unexamined j public void sort(List<T> list) { 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); 11/7/2018 Compsci 201, Fall 2018, Sorting + Comparators

9 Reminder: Loop Invariant
Statement: true each time loop begins to execute 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 Helps to reason formally and informally about the code you’re writing Can I explain the invariant to someone? 11/7/2018 Compsci 201, Fall 2018, Sorting + Comparators

10 Bubblesort isn't much code
Swap adjacent elements when out of order From beginning to end, then end-1, end-2, … After n passes, last n-elements in place 11/7/2018 Compsci 201, Fall 2018, Sorting + Comparators

11 Timing of n2 and other sorts
11/7/2018 Compsci 201, Fall 2018, Sorting + Comparators

12 More efficient O(n log n) sorts
Divide and conquer sorts: Quick sort: fast in practice, O(n2) worst case Merge sort: stable, fast, extra storage Timsort: Other sorts: Heap sort, TBDL: priority queue sorting Radix sort: uses digits/characters (no compare) O(n log n) is optimal for comparing But, Radix is O(n) ?? 11/7/2018 Compsci 201, Fall 2018, Sorting + Comparators

13 Compsci 201, Fall 2018, Sorting + Comparators
Stable, Stability Stable: respect order of equal keys when sorting First sort by shape, then by color: Stable! Why are numeric examples so popular? 11/7/2018 Compsci 201, Fall 2018, Sorting + Comparators

14 Quicksort: fast in practice
Invented in 1962 by Tony Hoare, didn't understand recursion: Canonical T(n) = 2T(n/2)+O(n), but Worst case is O(n2), 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); doQuick(list,piv+1,last); } <= X > X X pivot index 11/7/2018 Compsci 201, Fall 2018, Sorting + Comparators

15 Compsci 201, Fall 2018, Sorting + Comparators
Pivot is O(n) Invariant: [first,p] <= list.get(first) Invariant: (p,k) > list.get(first) private int pivot(List<T> list, int first, int last){ T piv = list.get(first); int p = first; for(int k=first+1; k <= last; k++){ if (list.get(k).compareTo(piv) <= 0){ p++; swap(list,k,p); } swap(list,p,first); return p; <= [first] ??? k > [first] first p <= X > X X pivot index 11/7/2018 Compsci 201, Fall 2018, Sorting + Comparators

16 https://en.wikipedia.org/wiki/Timsort
Stable, O(n log n) in average and worst, O(n) best! In practice lots of data is "close" to sorted Invented by Tim Peters for Python, now in Java Replaced merge sort which is also stable Engineered to be correct, fast, useful in practice Theory and explanation not so simple 11/7/2018 Compsci 201, Fall 2018, Sorting + Comparators

17 Summary of O(n log n) sorts
Timsort: hybrid of merge and insertion? Fast in real world: Python, Java 7+, Android What’s the best O(n log n) sort to call? The one in the library you have access to Arrays.sort or Collections.sort Changing how you sort: .compareTo() or .compare() 11/7/2018 Compsci 201, Fall 2018, Sorting + Comparators

18 sortingwoto or ginooorsttw
11/7/2018 Compsci 201, Fall 2018, Sorting + Comparators

19 Compsci 201, Fall 2018, Sorting + Comparators
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. 11/7/2018 Compsci 201, Fall 2018, Sorting + Comparators

20 Compsci 201, Fall 2018, Sorting + Comparators
Sorting APTs Call sort on class that implements Comparable Need.compareTo(..), type of parameter? 11/7/2018 Compsci 201, Fall 2018, Sorting + Comparators

21 Compsci 201, Fall 2018, Sorting + Comparators
Comparable.compareTo Return < 0, == 0, > 0 when … Sort by last name, when equal first name 11/7/2018 Compsci 201, Fall 2018, Sorting + Comparators

22 The Comparator Interface
When you can't access/change a class Supply separate Comparator object int compare(Person a, Person b) Also use class method names as arguments See discussion documents Arrays.sort(list, Comparator.comparing(Person::getLast) .thenComparing(Person::getFirst); 11/7/2018 Compsci 201, Fall 2018, Sorting + Comparators

23 SortByFreqs, SortedFreqs
Use TreeMap in both solutions Keys are in sorted order Could change Tree Comparator, but … 11/7/2018 Compsci 201, Fall 2018, Sorting + Comparators

24 Using TreeMap and Map.Entry
Iterate over keySet and get keys in sorted order Underneath a balanced red/black binary tree All map operations are O(log n) Sorting map entries: Map.Entry<Key,Value> Careful analysis of each part of code below 11/7/2018 Compsci 201, Fall 2018, Sorting + Comparators

25 Compsci 201, Fall 2018, Sorting + Comparators
Midterm 2 Mean was 64/74 or 86% Do NOT judge your score against others I do not judge your score against others Most difficult problems were coding In particular, many lost points on tree problem Understanding recursion and trees 11/7/2018 Compsci 201, Fall 2018, Sorting + Comparators

26 Compsci 201, Fall 2018, Sorting + Comparators
Regrade and APT quiz Read question carefully, read rubrics carefully Look for clues, e.g., with midterm question, read document about maxPath Make recursive calls. Think about subtree returns If you know the maxPath in subtrees, … Can you determine max through you? My info is 8, left–max is 22, right-max is 21 11/7/2018 Compsci 201, Fall 2018, Sorting + Comparators

27 Bubble Sort, A Personal Odyssey
11/7/2018 Compsci 201, Fall 2018, Sorting + Comparators

28 Compsci 201, Fall 2018, Sorting + Comparators
Steve and Rachel, Duke 1997 11/7/2018 Compsci 201, Fall 2018, Sorting + Comparators

29 Compsci 201, Fall 2018, Sorting + Comparators
11/08/77 11/7/2018 Compsci 201, Fall 2018, Sorting + Comparators

30 Compsci 201, Fall 2018, Sorting + Comparators
17 Nov 75 Not needed Can be tightened considerably 11/7/2018 Compsci 201, Fall 2018, Sorting + Comparators

31 Jim Gray (Turing 1998) Bubble sort is a good argument for analyzing algorithm performance. It is a perfectly correct algorithm. But it's performance is among the worst imaginable. So, it crisply shows the difference between correct algorithms and good algorithms. (italics ola’s)

32 Brian Reid (Hopper Award 1982)
Feah. I love bubble sort, and I grow weary of people who have nothing better to do than to preach about it. Universities are good places to keep such people, so that they don't scare the general public. (continued)

33 Brian Reid (Hopper 1982) I am quite capable of squaring N with or without a calculator, and I know how long my sorts will bubble. I can type every form of bubble sort into a text editor from memory. If I am writing some quick code and I need a sort quick, as opposed to a quick sort, I just type in the bubble sort as if it were a statement. I'm done with it before I could look up the data type of the third argument to the quicksort library. I have a dual-processor 1.2 GHz Powermac and it sneers at your N squared for most interesting values of N. And my source code is smaller than yours. Brian Reid who keeps all of his bubbles sorted anyhow.

34 Niklaus Wirth (Turing award 1984)
I have read your article and share your view that Bubble Sort has hardly any merits. I think that it is so often mentioned, because it illustrates quite well the principle of sorting by exchanging. I think BS is popular, because it fits well into a systematic development of sorting algorithms. But it plays no role in actual applications. Quite in contrast to C, also without merit (and its derivative Java), among programming codes.

35 Compsci 201, Fall 2018, Sorting + Comparators
Sorting Conundrums You want to sort a million 32-bit integers You're an advisor to Obama 11/7/2018 Compsci 201, Fall 2018, Sorting + Comparators


Download ppt "Compsci 201 Comparators, Sorting"

Similar presentations


Ads by Google