UVA CS 150 - Lecture 18- The Story So Far

Unformatted text preview:

1David Evanshttp://www.cs.virginia.edu/evansCS150: Computer ScienceUniversity of VirginiaComputer ScienceLecture 18: The Story So Far2Lecture 18: MutationMenu• Finish insert-sort-tree• Course roadmap• Introducing MutationA few people have extensions on Exam 1, sono talking about the exam questions until Wednesday.If you have an extension on Exam 1, don’t read Chapter 9 until you turn in the exam.3Lecture 18: Mutationinsert-one-tree(define (insert-one-tree cf el tree)(if (null? tree)(make-tree null el null)(if (cf el (get-element tree))(make-tree(insertel-tree cf el (get-left tree))(get-element tree) (get-right tree))(make-tree (get-left tree)(get-element tree)(insertel-tree cf el (get-right tree))))))Each time we callinsert-one-tree, the size of the tree approximately halves (if it is well balanced). Each application is constant time.The running time of insert-one-tree is in Θ (log n)where n is the number of elements in the input tree, which must be well-balanced.4Lecture 18: Mutationinsert-sort-helper(define (insert-sort-helper cf lst)(if (null? lst) null(insert-one-tree cf (car lst) (insert-sort-helper cf (cdr lst)))))No change (other than using insert-one-tree)…but evaluates to a tree not a list!(((() 1 ()) 2 ()) 5 (() 8 ()))5Lecture 18: Mutationextract-elementsWe need to make a list of all the tree elements, from left to right.(define (extract-elements tree)(if (null? tree) null(append (extract-elements (get-left tree))(cons (get-element tree)(extract-elements (get-right tree))))))6Lecture 18: MutationRunning time of insert-sort-tree(define (insert-one-tree cf el tree)(if (null? tree)(make-tree null el null)(if (cf el (get-element tree))(make-tree (insert-one-tree cf el (get-left tree))(get-element tree)(get-right tree))(make-tree (get-left tree)(get-element tree)(insert-one-tree cf el (get-right tree))))))(define (insert-sort-tree cf lst)(define (insert-sort-helper cf lst)(if (null? lst) null(insert-one-treecf (car lst) (insert-sort-helper cf (cdr lst)))))(extract-elements (insert-sort-helper cf lst)))Θ(log n)n = number ofelements in treeΘ(n log n)n = number ofelements in lst27Lecture 18: Mutation0200040006000800010000120002101826344250586674829098n log2ninsert-sort-treen2insert-sortGrowth of time to sort random list8Lecture 18: MutationWhat if tree is not well-balanced?235813A pathologically unbalanced tree is as bad as a list!insert-one worst case requires n recursive applications,so insert-sort-tree worst case is in Θ(n2)9Lecture 18: MutationComparing sorts> (testgrowth best-first-sort)n = 250, time = 110n = 500, time = 371n = 1000, time = 2363n = 2000, time = 8162n = 4000, time = 31757(3.37 6.37 3.45 3.89)> (testgrowth insert-sort)n = 250, time = 40n = 500, time = 180n = 1000, time = 571n = 2000, time = 2644n = 4000, time = 11537(4.5 3.17 4.63 4.36)> (testgrowth insert-sort-halves)n = 250, time = 251n = 500, time = 1262n = 1000, time = 4025n = 2000, time = 16454n = 4000, time = 66137(5.03 3.19 4.09 4.02)> (testgrowth insert-sort-tree)n = 250, time = 30n = 500, time = 250n = 1000, time = 150n = 2000, time = 301n = 4000, time = 1001(8.3 0.6 2.0 3.3)10Lecture 18: MutationCan we do better?• Making all those trees is a lot of work• Can we divide the problem in two halves, without making trees?This is the famous “Quicksort”algorithm invented by Sir Tony Hoare. See Chapter 8.There are lots of ways to do a little bit better, but no way to doasymptotically better. All possible sort procedure have running timesin Ω(n log n). (We’ll explain why later in the course...)11Lecture 18: MutationCourse RoadmapSynthesisAnalysisCh 2: LanguageCh 3: ProgrammingCh 4: ProceduresCh 5: DataCh 6: CostCh 7: TimeCh 8: Sorting and SequencingPS5, Ch 9: StatePS6, Ch 10: ObjectsCh 13: TractabilityCh 11: ModelsPS7, Ch 13: Meta-LanguageCh 12: ComputabilityPS8, 9: Building Web ApplicationsYou are here12Lecture 18: MutationComputer Science: CS150 so far• How to describe information processes by defining procedures– Programming with procedures, lists, recursion– Chapters 3, 4, 5• How to predict properties about information processes– Predicting running time, Θ, Ο, Ω• How to elegantly and efficiently implement information processes– Chapter 3 (rules of evaluation)313Lecture 18: MutationCS150 upcoming• How to describe information processes by defining procedures– Programming with state, objects, networks• How to predict properties about information processes– What is the fastest process that can solve a given problem?– Are there problems which can’t be solved by algorithms?• How to elegantly and efficiently implement information processes– How to implement a Scheme interpreter14Lecture 18: MutationThe Liberal ArtsTrivium (3 roads)languageQuadrivium (4 roads)numbersGrammar Rhetoric Logic ArithmeticGeometryMusicAstronomyFrom Lecture 1:15Lecture 18: MutationLiberal Arts Checkup• Grammar: study of meaning in written expression• Rhetoric: comprehension of verbal and written discourse• Logic: argumentative discourse for discovering truth• Arithmetic: understanding numbers• Geometry: quantification of space• Music: number in time• AstronomyBNF replacement rules for describing languages, rules of evaluation for meaningNot much yet…interfaces between components (PS6-9), program and user (PS8-9)Rules of evaluation, if, recursive definitionsNot much yet…wait until AprilCurves as procedures, fractals (PS3)Yes, listen to “Hey Jude!”Friday: read Neil deGrasse Tyson’s


View Full Document

UVA CS 150 - Lecture 18- The Story So Far

Documents in this Course
Objects

Objects

6 pages

Load more
Download Lecture 18- The Story So Far
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Lecture 18- The Story So Far and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Lecture 18- The Story So Far 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?