1 / 8

CS 341 Programming Language Design and Implementation

CS 341 Programming Language Design and Implementation. Administrative HW #5 is out ― due Monday, 2/17 @ 9pm Quiz #2? Also next Monday, 2/17 Switching back to F#... Programming day! Let's start with sorting…. Startup an F# environment , either will work: http://ideone.com Visual Studio

tyrone
Download Presentation

CS 341 Programming Language Design and Implementation

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CS 341 Programming Language Design and Implementation • Administrative • HW #5 is out ― due Monday, 2/17 @ 9pm • Quiz #2? Also next Monday, 2/17 • Switching back to F#... • Programming day! • Let's start with sorting… CS 341 -- 12 Feb 2014

  2. Startup an F# environment, either will work: • http://ideone.com • Visual Studio • Copy-paste timing code: • "Sorting.fs" • http://www.joehummel.net/uploads/cs341-sorting.fs • Take a minute to run & review code… • start with Main • creates 2 lists, sorts using built-in List.sort function CS 341 -- 12 Feb 2014

  3. Exercise 1 • Selection sort: • Algorithm: find min, move to front, sort the rest… • note that min(L) and remove(e, L) are defined for you • use ordinary (not tail) recursion, 4-5 lines of code… // // selection sort: // let rec selectionsort(L)= . . let Main argv = . . printfn "" runit("selection sort", selectionsort, list1) runit("selection sort", selectionsort, list2) CS 341 -- 12 Feb 2014

  4. Exercise 2 Hint: to split lists, use List.filter — like map, but keeps only those elements where function returns true. 7-8 lines of code… • Quicksort… • select first element as pivot • split remainder into two lists L1, L2: elements <= pivot, elements > pivot • sort L1 and L2 recursively • put pieces back together into a single list // // quicksort: // let rec quicksort(L)= . . let Main argv = . . printfn "" runit("quick sort", quicksort, list1) runit("quick sort", quicksort, list2) CS 341 -- 12 Feb 2014

  5. Timing… • Start to increase the size of the generated list… • 1,000 elements • 10,000 elements • 100,000 elements • 1,000,000 elements comment out call to selectionsort at this point :-) CS 341 -- 12 Feb 2014

  6. Why is quicksort so much slower? • It's not the recursive calls • the recursion is not that deep • It's the excessive list building • the @ operator is building new lists every time… • Solution? • merge sort using :: operator as much as possible… . . quicksort(left) @ [pivot] @ quicksort(right) element :: list // doesn't require constructing a new list: CS 341 -- 12 Feb 2014

  7. Tail recursion? • There was an earlier question about tail recursion: • Automatically, no… • "single-call" recursion (e.g. min) can be rewritten — using helper function and extra "accumulator" parameter • "multi-call" recursion (e.g. quicksort) is not easily rewritten • and rewrite requires use of a stack, so there's no benefit to the TR "Can all recursive functions be rewritten as tail-recursive?" CS 341 -- 12 Feb 2014

  8. Exercise 3 // // standard deviation of L = list of numbers: // // Hint 1: think higher-order // Hint 2: System.Math.Sqrt(…) // let stddev(L)= . . • Standard deviation: CS 341 -- 12 Feb 2014

More Related