Sorting and Searching

Size: px
Start display at page:

Download "Sorting and Searching"

Transcription

1 CS 1110: Introduction to Computing Using Pyton Lecture 23 Sorting and Searcing [Andersen, Gries, Lee, Marscner, Van Loan, Wite]

2 Announcements Final Exam conflicts due tonigt at 11:59pm Final Exam review sessions on te 14 t Labs on 5/9 and 5/10 will be office ours Assignment 5 Due 11:59pm on ***Wednesday*** May 10 t Lab 13 is out 5/2/17 Sorting and Searcing 2

3 Recall: Accessing te Original Metod Wat if you want to use te original version metod? New metod = original+more Do not want to repeat code from te original version Call old metod explicitly Use metod as a function Pass object as first argument Example: Employee. str (self) class Employee(object): """An Employee wit a salary""" def str (self): return (self._name + ', year ' + str(self._start) + ', salary ' + str(self._salary)) class Executive(Employee): """An Employee wit a bonus.""" def str (self): return (Employee. str (self) + ', bonus ' + str(self._bonus)) 5/2/17 Sorting and Searcing 3

4 super Can also use super super(<class>, <instance>) returns te parent class of <class> and <instance> Example: super(executive, self). str () class Employee(object): """An Employee wit a salary""" def str (self): return (self._name + ', year ' + str(self._start) + ', salary ' + str(self._salary)) class Executive(Employee): """An Employee wit a bonus.""" def str (self): return (super(executive, self). str () + ', bonus ' + str(self._bonus)) 5/2/17 Sorting and Searcing 4

5 Dutc National Flag Variant Sequence of integer values red = negatives, wite = 0, blues = positive Only rearrange part of te list, not all pre: b? post: b < 0 = 0 > 0 t i j inv: b < 0? = 0 > 0 5/2/17 Sorting and Searcing 5

6 Dutc National Flag Variant Sequence of integer values red = negatives, wite = 0, blues = positive Only rearrange part of te list, not all pre: b? post: b < 0 = 0 > 0 t i j inv: b < 0? = 0 > 0 pre: t =, i = +1, j = post: t = i 5/2/17 Sorting and Searcing 6

7 Dutc National Flag Algoritm def dnf(b,, ): """Returns: partition points as a tuple (i,j)""" t = ; i = +1, j = ; # inv: b[..t-1] < 0, b[t..i-1]?, b[i..j] = 0, b[j+1..] > 0 wile t < i: if b[i-1] < 0: swap(b,i-1,t) t = t+1 elif b[i-1] == 0: i = i-1 else: swap(b,i-1,j) i = i-1 j = j-1 # post: b[..i-1] < 0, b[i..j] = 0, b[j+1..] > 0 return (i, j) 5/2/17 Sorting and Searcing < 0? = 0 > 0 t i j t i j t i j i t j

8 Partition Algoritm Given a list segment b[..] wit some pivot value x in b[]: pre: b Swap elements of b[..] and store in i to trutify post: post: b cange: b x? i i+1 <= x x >= x i into b /2/17 Sorting and Searcing 8

9 Sorting wit Partitions Given a list segment b[..] wit some value x in b[]: pre: b x? Swap elements of b[..] and store in j to trutify post: i i+1 post: b y <= y <= y? x >= y x >= x Partition Recursively Recursive partitions = sorting 5/2/17 Sorting and Searcing 11

10 QuicSort def quic_sort(b,, ): """Sort te array fragment b[..]""" if b[..] as fewer tan 2 elements: return i = partition(b,, ) # b[..i 1] <= b[i] <= b[i+1..] # Sort b[..i 1] and b[i+1..] quic_sort (b,, i 1) quic_sort (b, i+1, ) pre: b post: b x? i i+1 <= x x >= x 5/2/17 Sorting and Searcing 12

11 Linear Searc Vague: Find first occurrence of v in b[..-1]. Better: Store an integer in i to trutify result condition post: post: 1. v is not in b[..i-1] 2. i = OR v = b[i] 5/2/17 Sorting and Searcing 19

12 Linear Searc Vague: Find first occurrence of v in b[..-1]. Better: Store an integer in i to trutify result condition post: post: 1. v is not in b[..i-1] 2. i = OR v = b[i] pre: b? post: b i v not ere v? 5/2/17 Sorting and Searcing 20

13 Linear Searc Vague: Find first occurrence of v in b[..-1]. Better: Store an integer in i to trutify result condition post: post: 1. v is not in b[..i-1] 2. i = OR v = b[i] pre: b? post: b i v not ere v? OR b v not ere i 5/2/17 Sorting and Searcing 21

14 pre: b Linear Searc? post: b i v not ere v? OR b v not ere i inv: b i v not ere? 5/2/17 Sorting and Searcing 22

15 Linear Searc def linear_searc(b,v,,): """Returns: first occurrence of v in b[..-1]""" # Store in i index of te first v in b[..-1] i = # invariant: v is not in b[0..i-1] wile i < and b[i]!= v: i = i + 1 # post: v is not in b[..i-1] # i >= or b[i] == v return i if i < else -1 Analyzing te Loop 1. Does te initialization mae inv true? 2. Is post true wen inv is true and condition is false? 3. Does te repetend mae progress? 4. Does te repetend eep te invariant inv true? 5/2/17 Sorting and Searcing 23

16 Binary Searc Loo for v in sorted sequence segment b[..]. 5/2/17 Sorting and Searcing 24

17 Binary Searc Loo for v in sorted sequence segment b[..]. Precondition: b[..-1] is sorted (in ascending order). Postcondition: b[..i-1] < v and v <= b[i..] pre: b? post: b i < v >= v 5/2/17 Sorting and Searcing 25

18 Binary Searc Loo for value v in sorted segment b[..] pre: b post: b inv: b < v? i < v >= v i j? >= v Called binary searc because eac iteration of te loop cuts te array segment still to be processed in alf Example b if v is 3, set i to 0 if v is 4, set i to 5 if v is 5, set i to 7 if v is 8, set i to 10 5/2/17 Sorting and Searcing 26

19 Binary Searc pre: b? i post: b < v >= v i j inv: b < v? >= v i = ; j = +1; wile i!= j: Looing at b[i] gives linear searc from left. Looing at b[j-1] gives linear searc from rigt. Looing at middle: b[(i+j)/2] gives binary searc. 5/2/17 Sorting and Searcing 27

20 Binary Searc def bsearc(b, v): i = 0 j = len(b) # invariant; b[0..i-1] < v, b[i..j-1] unnown, b[j..] >= v wile i < j: mid = (i+j)/2 if b[mid] < v: i = mid+1 else: #b[mid] >= v j = mid if i< len(b) and b[i] == v: else: return i return -1 Analyzing te Loop 1. Does te initialization mae inv true? 2. Is post true wen inv is true and condition is false? 3. Does te repetend mae progress? 4. Does te repetend eep te invariant inv true? 5/2/17 Sorting and Searcing 28

21 Binary Searc Recursive def rbsearc(b, v): """ len(b) > 0 """ return rbsearc_elper(b, v, 0, len(b)) def rbsearc_elper(b, v, i, j): if i >= j: if i < len(b) and b[i] == v: return i else: return -1 mid = (i + j) / 2 if b[mid] < v: return rbsearc_elper(b, v, mid + 1, j) else: # b[mid] >= v return rbsearc_elper(b, v, i, mid) 5/2/17 Sorting and Searcing 29

Lecture 25. Sequence Algorithms (Continued)

Lecture 25. Sequence Algorithms (Continued) Lecture 25 Sequence Algoritms (Continued) Announcements for Tis Lecture Assignment & Lab A6 is not graded yet Done by end of classes A7 due Wed, Dec. 10 Wednesday after classes Keep on top of milestones

More information

Lecture 26: Sorting CS 1110 Introduction to Computing Using Python

Lecture 26: Sorting CS 1110 Introduction to Computing Using Python http://www.cs.cornell.edu/courses/cs1110/2018sp Lecture 26: Sorting CS 1110 Introduction to Computing Using Python [E. Andersen, A. Bracy, D. Gries, L. Lee, S. Marschner, C. Van Loan, W. White] Academic

More information

CS211 Spring 2004 Lecture 06 Loops and their invariants. Software engineering reason for using loop invariants

CS211 Spring 2004 Lecture 06 Loops and their invariants. Software engineering reason for using loop invariants CS211 Spring 2004 Lecture 06 Loops and teir invariants Reading material: Tese notes. Weiss: Noting on invariants. ProgramLive: Capter 7 and 8 O! Tou ast damnale iteration and art, indeed, ale to corrupt

More information

CS 1110, LAB 13: SEQUENCE ALGORITHMS

CS 1110, LAB 13: SEQUENCE ALGORITHMS CS 1110, LAB 13: SEQUENCE ALGORITHMS http://www.cs.cornell.edu/courses/cs1110/2017fa/labs/lab13/ First Name: Last Name: NetID: This final lab of the course helps you practice with more complicated invariants

More information

CS 1110: Introduction to Computing Using Python Loop Invariants

CS 1110: Introduction to Computing Using Python Loop Invariants CS 1110: Introduction to Computing Using Python Lecture 21 Loop Invariants [Andersen, Gries, Lee, Marschner, Van Loan, White] Announcements Prelim 2 conflicts due by midnight tonight Lab 11 is out Due

More information

SORTING 9/26/18. Prelim 1. Prelim 1. Why Sorting? InsertionSort. Some Sorting Algorithms. Tonight!!!! Two Sessions:

SORTING 9/26/18. Prelim 1. Prelim 1. Why Sorting? InsertionSort. Some Sorting Algorithms. Tonight!!!! Two Sessions: Prelim 1 2 "Organizing is wat you do efore you do someting, so tat wen you do it, it is not all mixed up." ~ A. A. Milne SORTING Tonigt!!!! Two Sessions: You sould now y now wat room to tae te final. Jenna

More information

Announcements SORTING. Prelim 1. Announcements. A3 Comments 9/26/17. This semester s event is on Saturday, November 4 Apply to be a teacher!

Announcements SORTING. Prelim 1. Announcements. A3 Comments 9/26/17. This semester s event is on Saturday, November 4 Apply to be a teacher! Announcements 2 "Organizing is wat you do efore you do someting, so tat wen you do it, it is not all mixed up." ~ A. A. Milne SORTING Lecture 11 CS2110 Fall 2017 is a program wit a teac anyting, learn

More information

isinstance and While Loops

isinstance and While Loops CS 1110: Introduction to Computing Using Python Lecture 20 isinstance and While Loops [Andersen, Gries, Lee, Marschner, Van Loan, White] Announcements A4: Due 4/20 at 11:59pm Should only use our str method

More information

CS 234. Module 6. October 16, CS 234 Module 6 ADT Dictionary 1 / 33

CS 234. Module 6. October 16, CS 234 Module 6 ADT Dictionary 1 / 33 CS 234 Module 6 October 16, 2018 CS 234 Module 6 ADT Dictionary 1 / 33 Idea for an ADT Te ADT Dictionary stores pairs (key, element), were keys are distinct and elements can be any data. Notes: Tis is

More information

Lecture 24: Loop Invariants

Lecture 24: Loop Invariants http://www.cs.cornell.edu/courses/cs1110/2018sp Lecture 24: Loop Invariants [Online Reading] CS 1110 Introduction to Computing Using Python [E. Andersen, A. Bracy, D. Gries, L. Lee, S. Marschner, C. Van

More information

CS 1110, LAB 12: SEQUENCE ALGORITHMS First Name: Last Name: NetID:

CS 1110, LAB 12: SEQUENCE ALGORITHMS   First Name: Last Name: NetID: CS 1110, LAB 12: SEQUENCE ALGORITHMS http://www.cs.cornell.edu/courses/cs1110/2014fa/labs/lab12.pdf First Name: Last Name: NetID: This last lab is extremely important. It helps you understand how to construct

More information

CS 1110 Final, December 8th, Question Points Score Total: 100

CS 1110 Final, December 8th, Question Points Score Total: 100 CS 1110 Final, December 8th, 2016 This 150-minute exam has 8 questions worth a total of 100 points. Scan the whole test before starting. Budget your time wisely. Use the back of the pages if you need more

More information

Lecture 20. Subclasses & Inheritance

Lecture 20. Subclasses & Inheritance Lecture 20 Subclasses & Inheritance Announcements for Today Reading Today: Chapter 18 Online reading for Thursday Prelim, Nov 9 th 7:30-9:00 Material up to Thursday Review posted on Thursday Recursion

More information

CS 1110: WORKED EXAMPLES REGARDING LOOPS AND INVARIANTS

CS 1110: WORKED EXAMPLES REGARDING LOOPS AND INVARIANTS CS 1110: WORKED EXAMPLES REGARDING LOOPS AND INVARIANTS http://www.cs.cornell.edu/courses/cs1110/2013sp/materials/loop_invariants.pdf D. GRIES, L. LEE, S. MARSCHNER, AND W. WHITE An invariant is, in the

More information

CS 1110 Final, December 8th, Question Points Score Total: 100

CS 1110 Final, December 8th, Question Points Score Total: 100 CS 1110 Final, December 8th, 2016 This 150-minute exam has 8 questions worth a total of 100 points. Scan the whole test before starting. Budget your time wisely. Use the back of the pages if you need more

More information

CS 1110 Final, December 17th, Question Points Score Total: 100

CS 1110 Final, December 17th, Question Points Score Total: 100 CS 1110 Final, December 17th, 2014 This 150-minute exam has 8 questions worth a total of 100 points. Scan the whole test before starting. Budget your time wisely. Use the back of the pages if you need

More information

Lecture 18. Classes and Types

Lecture 18. Classes and Types Lecture 18 Classes and Types Announcements for Today Reading Today: See reading online Tuesday: See reading online Prelim, Nov 6 th 7:30-9:30 Material up to next class Review posted next week Recursion

More information

Last Name: First: Netid: Section. CS 1110 Final, December 17th, 2014

Last Name: First: Netid: Section. CS 1110 Final, December 17th, 2014 CS 0 Final, December 7th, 204 SOLUTION This 50-minute exam has 8 questions worth a total of 00 points. Scan the whole test before starting. Budget your time wisely. Use the back of the pages if you need

More information

CS1110. Lecture 22: Prelim 2 Review Session. Announcements. Processed prelim regrade requests: on the front table.

CS1110. Lecture 22: Prelim 2 Review Session. Announcements. Processed prelim regrade requests: on the front table. CS1110 Lecture 22: Prelim 2 Review Session Announcements Processed prelim regrade requests: on the front table. Reminders: Exam: 7:30 9:00PM, Tuesday Apr 16 th Kennedy 116 (Call Auditorium, same as before).

More information

CS Lecture 19: Loop invariants

CS Lecture 19: Loop invariants CS 1110 Lecture 19: Loop invariants Announcements Prelim 2 conflicts Today (April 2) is two weeks before the prelim, and the deadline for submitting prelim conflicts. Instructor travel This week and the

More information

SORTING. Insertion sort Selection sort Quicksort Mergesort And their asymptotic time complexity

SORTING. Insertion sort Selection sort Quicksort Mergesort And their asymptotic time complexity 1 SORTING Insertion sort Selection sort Quicksort Mergesort And their asymptotic time complexity See lecture notes page, row in table for this lecture, for file searchsortalgorithms.zip Lecture 11 CS2110

More information

PREPARING FOR THE FINAL EXAM

PREPARING FOR THE FINAL EXAM PREPARING FOR THE FINAL EXAM CS 1110: FALL 2012 This handout explains what you have to know for the final exam. Most of the exam will include topics from the previous two prelims. We have uploaded the

More information

CS 1110 Final, December 16th, 2013

CS 1110 Final, December 16th, 2013 CS 1110 Final, December 16th, 2013 This 150-minute exam has 8 questions worth a total of 100 points. Scan the whole test before starting. Budget your time wisely. Use the back of the pages if you need

More information

CS 1110 Prelim 2 April 22, 2014

CS 1110 Prelim 2 April 22, 2014 CS 1110 Prelim 2 April 22, 2014 This 90-minute exam has 6 questions worth a total of 46 points. When permitted to begin, scan the whole test before starting. Budget your time wisely. Use the back of the

More information

Lecture 19: Subclasses & Inheritance (Chapter 18)

Lecture 19: Subclasses & Inheritance (Chapter 18) http://www.cs.cornell.edu/courses/cs1110/2018sp Lecture 19: Subclasses & Inheritance (Chapter 18) CS 1110 Introduction to Computing Using Python [E. Andersen, A. Bracy, D. Gries, L. Lee, S. Marschner,

More information

Data Structures and Programming Spring 2014, Midterm Exam.

Data Structures and Programming Spring 2014, Midterm Exam. Data Structures and Programming Spring 2014, Midterm Exam. 1. (10 pts) Order te following functions 2.2 n, log(n 10 ), 2 2012, 25n log(n), 1.1 n, 2n 5.5, 4 log(n), 2 10, n 1.02, 5n 5, 76n, 8n 5 + 5n 2

More information

"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

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 "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 2017 Prelim 1 2 It's on Tuesday Evening (3/13) Two Sessions:

More information

Fault Localization Using Tarantula

Fault Localization Using Tarantula Class 20 Fault localization (cont d) Test-data generation Exam review: Nov 3, after class to :30 Responsible for all material up troug Nov 3 (troug test-data generation) Send questions beforeand so all

More information

"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

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 "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 Prelim 1: Tuesday, 12 March 2 Visit exams page of

More information

each node in the tree, the difference in height of its two subtrees is at the most p. AVL tree is a BST that is height-balanced-1-tree.

each node in the tree, the difference in height of its two subtrees is at the most p. AVL tree is a BST that is height-balanced-1-tree. Data Structures CSC212 1 AVL Trees A binary tree is a eigt-balanced-p-tree if for eac node in te tree, te difference in eigt of its two subtrees is at te most p. AVL tree is a BST tat is eigt-balanced-tree.

More information

SEARCHING AND SORTING HINT AT ASYMPTOTIC COMPLEXITY

SEARCHING AND SORTING HINT AT ASYMPTOTIC COMPLEXITY SEARCHING AND SORTING HINT AT ASYMPTOTIC COMPLEXITY Lecture 10 CS2110 Fall 2016 Miscellaneous 2 A3 due Monday night. Group early! Only 325 views of the piazza A3 FAQ yesterday morning. Everyone should

More information

CSc 110, Spring 2017 Lecture 39: searching

CSc 110, Spring 2017 Lecture 39: searching CSc 110, Spring 2017 Lecture 39: searching 1 Sequential search sequential search: Locates a target value in a list (may not be sorted) by examining each element from start to finish. Also known as linear

More information

Iteration and For Loops

Iteration and For Loops CS 1110: Introduction to Computing Using Python Lecture 11 Iteration and For Loops [Andersen, Gries, Lee, Marschner, Van Loan, White] Rooms: Announcements: Prelim 1 aa200 jjm200 Baker Laboratory 200 jjm201

More information

PREPARING FOR PRELIM 1

PREPARING FOR PRELIM 1 PREPARING FOR PRELIM 1 CS 1110: FALL 2012 This handout explains what you have to know for the first prelim. There will be a review session with detailed examples to help you study. To prepare for the prelim,

More information

"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

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 "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 2017 Announcements 2 is a program with a teach anything,

More information

ASYMPTOTIC COMPLEXITY

ASYMPTOTIC COMPLEXITY Simplicity is a great virtue but it requires hard work to achieve it and education to appreciate it. And to make matters worse: complexity sells better. - Edsger Dijkstra ASYMPTOTIC COMPLEXITY Lecture

More information

AVL Trees Outline and Required Reading: AVL Trees ( 11.2) CSE 2011, Winter 2017 Instructor: N. Vlajic

AVL Trees Outline and Required Reading: AVL Trees ( 11.2) CSE 2011, Winter 2017 Instructor: N. Vlajic 1 AVL Trees Outline and Required Reading: AVL Trees ( 11.2) CSE 2011, Winter 2017 Instructor: N. Vlajic AVL Trees 2 Binary Searc Trees better tan linear dictionaries; owever, te worst case performance

More information

Conditionals & Control Flow

Conditionals & Control Flow CS 1110: Introduction to Computing Using Python Lecture 8 Conditionals & Control Flow [Andersen, Gries, Lee, Marschner, Van Loan, White] Announcements: Assignment 1 Due tonight at 11:59pm. Suggested early

More information

CS 1110 Final Exam Solutions May 15th, 2014

CS 1110 Final Exam Solutions May 15th, 2014 Last Name: First Name: Cornell NetID, all caps: The Important First Question: CS 1110 Final Exam Solutions May 15th, 2014 1. [2 points] When allowed to begin, write your last name, first name, and Cornell

More information

Binary Search Tree - Best Time. AVL Trees. Binary Search Tree - Worst Time. Balanced and unbalanced BST

Binary Search Tree - Best Time. AVL Trees. Binary Search Tree - Worst Time. Balanced and unbalanced BST AL Trees CSE Data Structures Unit Reading: Section 4.4 Binary Searc Tree - Best Time All BST operations are O(d), were d is tree dept minimum d is d = log for a binary tree N wit N nodes at is te best

More information

CS Prelim 2 Review Fall 2018

CS Prelim 2 Review Fall 2018 CS 1110 Prelim 2 Review Fall 2018 Exam Info Prelim 1: Thursday, November 8th Last name L P at 5:15 6:45 in Uris G01 Last name Q Z at 5:15 6:45 in Statler Aud. Last name A D at 7:30 9:00 in Uris G01 Last

More information

CORRECTNESS ISSUES AND LOOP INVARIANTS

CORRECTNESS ISSUES AND LOOP INVARIANTS Aout A2 and feedack. Recursion 2 CORRECTNESS ISSUES AND LOOP INVARIANTS S2 has een graded. If you got 30/30, you will proaly have no feedack. If you got less than full credit, there should e feedack showing

More information

CSC 273 Data Structures

CSC 273 Data Structures CSC 273 Data Structures Lecture 6 - Faster Sorting Methods Merge Sort Divides an array into halves Sorts the two halves, Then merges them into one sorted array. The algorithm for merge sort is usually

More information

CS 234. Module 6. October 25, CS 234 Module 6 ADT Dictionary 1 / 22

CS 234. Module 6. October 25, CS 234 Module 6 ADT Dictionary 1 / 22 CS 234 Module 6 October 25, 2016 CS 234 Module 6 ADT Dictionary 1 / 22 Case study Problem: Find a way to store student records for a course, wit unique IDs for eac student, were records can be accessed,

More information

Prelim 2 Solution. CS 2110, November 19, 2015, 7:30 PM Total. Sorting Invariants Max Score Grader

Prelim 2 Solution. CS 2110, November 19, 2015, 7:30 PM Total. Sorting Invariants Max Score Grader Prelim 2 CS 2110, November 19, 2015, 7:30 PM 1 2 3 4 5 6 Total Question True Short Complexity Searching Trees Graphs False Answer Sorting Invariants Max 20 15 13 14 17 21 100 Score Grader The exam is closed

More information

Announcements. Lilian s office hours rescheduled: Fri 2-4pm HW2 out tomorrow, due Thursday, 7/7. CSE373: Data Structures & Algorithms

Announcements. Lilian s office hours rescheduled: Fri 2-4pm HW2 out tomorrow, due Thursday, 7/7. CSE373: Data Structures & Algorithms Announcements Lilian s office ours resceduled: Fri 2-4pm HW2 out tomorrow, due Tursday, 7/7 CSE373: Data Structures & Algoritms Deletion in BST 2 5 5 2 9 20 7 0 7 30 Wy migt deletion be arder tan insertion?

More information

Lecture 17: Classes (Chapter 15)

Lecture 17: Classes (Chapter 15) http://www.cs.cornell.edu/courses/cs1110/018sp Lecture 17: Classes (Chapter 15) CS 1110 Introduction to Computing Using Python [E. Andersen, A. Bracy, D. Gries, L. Lee, S. Marschner, C. Van Loan, W. White]

More information

ASYMPTOTIC COMPLEXITY

ASYMPTOTIC COMPLEXITY Simplicity is a great virtue but it requires hard work to achieve it and education to appreciate it. And to make matters worse: complexity sells better. - Edsger Dijkstra ASYMPTOTIC COMPLEXITY Lecture

More information

Sorting is a problem for which we can prove a non-trivial lower bound.

Sorting is a problem for which we can prove a non-trivial lower bound. Sorting The sorting problem is defined as follows: Sorting: Given a list a with n elements possessing a total order, return a list with the same elements in non-decreasing order. Remember that total order

More information

Sorting. Bubble sort method. Bubble sort properties. Quick sort method. Notes. Eugeniy E. Mikhailov. Lecture 27. Notes. Notes

Sorting. Bubble sort method. Bubble sort properties. Quick sort method. Notes. Eugeniy E. Mikhailov. Lecture 27. Notes. Notes Sorting Eugeniy E. Mikhailov The College of William & Mary Lecture 7 Eugeniy Mikhailov (W&M) Practical Computing Lecture 7 1 / 18 Bubble sort method Some one give us a vector of unsorted numbers. We want

More information

Lecture 21. Programming with Subclasses

Lecture 21. Programming with Subclasses Lecture 21 Programming with Subclasses Announcements for This Lecture Assignments Prelim 2 A4 is now graded Mean: 90.4 Median: 93 Std Dev: 10.6 Mean: 9 hrs Median: 8 hrs Std Dev: 4.1 hrs A5 is also graded

More information

Lecture 8: Conditionals & Control Flow (Sections ) CS 1110 Introduction to Computing Using Python

Lecture 8: Conditionals & Control Flow (Sections ) CS 1110 Introduction to Computing Using Python http://www.cs.cornell.edu/courses/cs1110/2018sp Lecture 8: Conditionals & Control Flow (Sections 5.1-5.7) CS 1110 Introduction to Computing Using Python [E. Andersen, A. Bracy, D. Gries, L. Lee, S. Marschner,

More information

Today. CISC101 Reminders & Notes. Searching in Python - Cont. Searching in Python. From last time

Today. CISC101 Reminders & Notes. Searching in Python - Cont. Searching in Python. From last time CISC101 Reminders & Notes Test 3 this week in tutorial USATs at the beginning of next lecture Please attend and fill out an evaluation School of Computing First Year Information Session Thursday, March

More information

CS Prelim 2 Review Fall 2012

CS Prelim 2 Review Fall 2012 CS 1110 Prelim 2 Review Fall 2012 Exam Info Prelim 1: 7:30 9:00PM, Tuesday, November 6th Last name A P in Kennedy 1116 Last name R T in Warren 131 Last name U Z in Warren 231 To help you study: Study guides,

More information

THE AUSTRALIAN NATIONAL UNIVERSITY Mid Semester Examination September COMP1730 / COMP6730 Programming for Scientists

THE AUSTRALIAN NATIONAL UNIVERSITY Mid Semester Examination September COMP1730 / COMP6730 Programming for Scientists THE AUSTRALIAN NATIONAL UNIVERSITY Mid Semester Examination September 2016 COMP1730 / COMP6730 Programming for Scientists Study Period: 15 minutes Time Allowed: 2 hours Permitted Materials: One A4 page

More information

Sorting and Searching

Sorting and Searching Sorting and Searching Sorting o Simple: Selection Sort and Insertion Sort o Efficient: Quick Sort and Merge Sort Searching o Linear o Binary Reading for this lecture: http://introcs.cs.princeton.edu/python/42sort/

More information

1 Copyright 2012 by Pearson Education, Inc. All Rights Reserved.

1 Copyright 2012 by Pearson Education, Inc. All Rights Reserved. CHAPTER 20 AVL Trees Objectives To know wat an AVL tree is ( 20.1). To understand ow to rebalance a tree using te LL rotation, LR rotation, RR rotation, and RL rotation ( 20.2). To know ow to design te

More information

Lecture 21. Programming with Subclasses

Lecture 21. Programming with Subclasses Lecture 21 Programming with Subclasses Announcements for Today Reading Today: See reading online Tuesday: Chapter 7 Prelim, Nov 10 th 7:30-9:00 Material up to Today Review has been posted Recursion + Loops

More information

Synchronization: Semaphores

Synchronization: Semaphores Illinois Institute of Technology Lecture 26 4/25 solved Synchronization: Semaphores CS 536: Science of Programming, Spring 2018 A. Why Avoiding interference, while good, isn t the same as coordinating

More information

CS1110. Lecture 1: Final review session. Review materials See website for a version of last year s final with conventions redone to match this year.

CS1110. Lecture 1: Final review session. Review materials See website for a version of last year s final with conventions redone to match this year. CS1110 Lecture 1: Final review session Announcements Review materials See website for a version of last year s final with conventions redone to match this year. Slides by D. Gries, L. Lee, S. Marschner,

More information

Lecture 21. Programming with Subclasses

Lecture 21. Programming with Subclasses Lecture 21 Programming with Subclasses Announcements for Today Reading Today: See reading online Tuesday: Chapter 7 Prelim, Nov 9 th 7:30-9:00 Material up to Today Review has been posted Recursion + Loops

More information

Binary Search APRIL 25 TH, 2014

Binary Search APRIL 25 TH, 2014 Binary Search APRIL 25 TH, 2014 The Search Problem One of the most common computational problems (along with sorting) is searching. In its simplest form, the input to the search problem is a list L and

More information

C/C++ Programming Lecture 18 Name:

C/C++ Programming Lecture 18 Name: . The following is the textbook's code for a linear search on an unsorted array. //***************************************************************** // The searchlist function performs a linear search

More information

Review 4. Lists and Sequences

Review 4. Lists and Sequences Review 4 Lists and Sequences Overview of List Syntax x = [0, 0, 0, 0] x.append(2) 3 in x x[2] = 5 x[0] = 4 k = 3 x[k] = 2 * x[0] x[k 2] = 6 Create list of length 4 with all zeroes Append 2 to end of list

More information

UNIT 5C Merge Sort. Course Announcements

UNIT 5C Merge Sort. Course Announcements UNIT 5C Merge Sort 15110 Principles of Computing, Carnegie Mellon University 1 Course Announcements Exam information 2:30 Lecture: Sections F, G, H will go to HH B131. 3:30 Lecture: Section O will go to

More information

CS Prelim 2 Review Fall 2017

CS Prelim 2 Review Fall 2017 CS 1110 Prelim 2 Review Fall 2017 Exam Info Prelim 2: 7:30 9:00PM, Thursday, Nov. 9th Last name A J in Uris G01 Last name K Z in Statler Auditorium SDS Students will get an e-mail To help you study: Study

More information

CS 1110 Final, December 9th, Question Points Score Total: 100

CS 1110 Final, December 9th, Question Points Score Total: 100 CS 1110 Final, Decemer 9th, 2015 This 150-minute exam has 8 questions worth a total of 100 points. Scan the whole test efore starting. Budget your time wisely. Use the ack of the pages if you need more

More information

When a BST becomes badly unbalanced, the search behavior can degenerate to that of a sorted linked list, O(N).

When a BST becomes badly unbalanced, the search behavior can degenerate to that of a sorted linked list, O(N). Balanced Binary Trees Binary searc trees provide O(log N) searc times provided tat te nodes are distributed in a reasonably balanced manner. Unfortunately, tat is not always te case and performing a sequence

More information

Chapter 5. Quicksort. Copyright Oliver Serang, 2018 University of Montana Department of Computer Science

Chapter 5. Quicksort. Copyright Oliver Serang, 2018 University of Montana Department of Computer Science Chapter 5 Quicsort Copyright Oliver Serang, 08 University of Montana Department of Computer Science Quicsort is famous because of its ability to sort in-place. I.e., it directly modifies the contents of

More information

Prelim 2 Solution. CS 2110, November 19, 2015, 5:30 PM Total. Sorting Invariants Max Score Grader

Prelim 2 Solution. CS 2110, November 19, 2015, 5:30 PM Total. Sorting Invariants Max Score Grader Prelim 2 CS 2110, November 19, 2015, 5:30 PM 1 2 3 4 5 6 Total Question True Short Complexity Searching Trees Graphs False Answer Sorting Invariants Max 20 15 13 14 17 21 100 Score Grader The exam is closed

More information

What is sorting? Lecture 36: How can computation sort data in order for you? Why is sorting important? What is sorting? 11/30/10

What is sorting? Lecture 36: How can computation sort data in order for you? Why is sorting important? What is sorting? 11/30/10 // CS Introduction to Computation " UNIVERSITY of WISCONSIN-MADISON Computer Sciences Department Professor Andrea Arpaci-Dusseau Fall Lecture : How can computation sort data in order for you? What is sorting?

More information

Lecture 22. While Loops

Lecture 22. While Loops Lecture 22 While Loops Announcements for This Lecture Assignments Prelim 2 A5 is now graded Will be returned in lab Mean: 52 Median: 53 Std Dev: 5.5 Passing Grade: 30 A6 due next Tuesday Dataset should

More information

Overview of List Syntax

Overview of List Syntax Lists and Sequences Overview of List Syntax x = [0, 0, 0, 0] Create list of length 4 with all zeroes x 4300112 x.append(2) 3 in x x[2] = 5 x[0] = 4 k = 3 Append 2 to end of list x (now length 5) Evaluates

More information

CSCE Java. Lab 13 - Searching & Sorting. Dr. Chris Bourke

CSCE Java. Lab 13 - Searching & Sorting. Dr. Chris Bourke CSCE 155 - Java Lab 13 - Searching & Sorting Dr. Chris Bourke Prior to Lab Before attending this lab: 1. Read and familiarize yourself with this handout. 2. Review notes on Search & Sorting Some additional

More information

CS 1110: Introduction to Computing Using Python Lists and Sequences

CS 1110: Introduction to Computing Using Python Lists and Sequences CS : Introduction to Computing Using Python Lecture Lists and Sequences [Andersen, Gries, Lee, Marschner, Van Loan, White] Prelim Lecture Announcements Date: Tuesday, March 4th, 7:3 pm to 9: pm Submit

More information

Homework 3 Solutions

Homework 3 Solutions CS3510 Design & Analysis of Algorithms Section A Homework 3 Solutions Released: 7pm, Wednesday Nov 8, 2017 This homework has a total of 4 problems on 4 pages. Solutions should be submitted to GradeScope

More information

Data Structures and Algorithms 2018

Data Structures and Algorithms 2018 Question 1 (12 marks) Data Structures and Algorithms 2018 Assignment 4 25% of Continuous Assessment Mark Deadline : 5pm Monday 12 th March, via Canvas Sort the array [5, 3, 4, 6, 8, 4, 1, 9, 7, 1, 2] using

More information

Lecture 10: Lists and Sequences

Lecture 10: Lists and Sequences http://www.cs.cornell.edu/courses/cs/8sp Lecture : Lists and Sequences (Sections.-.,.4-.6,.8-.) CS Introduction to Computing Using Python [E. Andersen, A. Bracy, D. Gries, L. Lee, S. Marschner, C. Van

More information

Numerical Derivatives

Numerical Derivatives Lab 15 Numerical Derivatives Lab Objective: Understand and implement finite difference approximations of te derivative in single and multiple dimensions. Evaluate te accuracy of tese approximations. Ten

More information

Algorithm for siftdown(int currentposition) while true (infinite loop) do if the currentposition has NO children then return

Algorithm for siftdown(int currentposition) while true (infinite loop) do if the currentposition has NO children then return 0. How would we write the BinaryHeap siftdown function recursively? [0] 6 [1] [] 15 10 Name: template class BinaryHeap { private: int maxsize; int numitems; T * heap;... [3] [4] [5] [6] 114 0

More information

CS61A Lecture 15. Amir Kamil UC Berkeley February 25, 2013

CS61A Lecture 15. Amir Kamil UC Berkeley February 25, 2013 CS61A Lecture 15 Amir Kamil UC Berkeley February 25, 2013 Announcements HW5 due on Wednesday Trends project out Partners are required; find one in lab or on Piazza Will not work in IDLE New bug submission

More information

! Search: find a given item in a list, return the. ! Sort: rearrange the items in a list into some. ! list could be: array, linked list, string, etc.

! Search: find a given item in a list, return the. ! Sort: rearrange the items in a list into some. ! list could be: array, linked list, string, etc. Searching & Sorting Week 11 Gaddis: 8, 19.6,19.8 CS 5301 Spring 2015 Jill Seaman 1 Definitions of Search and Sort! Search: find a given item in a list, return the position of the item, or -1 if not found.!

More information

Lecture 17: Classes (Chapter 15)

Lecture 17: Classes (Chapter 15) http://www.cs.cornell.edu/courses/cs1110/2018sp Lecture 17: Classes (Chapter 15) CS 1110 Introduction to Computing Using Python [E. Andersen, A. Bracy, D. Gries, L. Lee, S. Marschner, C. Van Loan, W. White]

More information

Lecture 18: Using Classes Effectively (Chapter 16)

Lecture 18: Using Classes Effectively (Chapter 16) http://www.cs.cornell.edu/courses/cs1110/2018sp Lecture 18: Using Classes Effectively (Chapter 16) CS 1110 Introduction to Computing Using Python [E. Andersen, A. Bracy, D. Gries, L. Lee, S. Marschner,

More information

Prelim 2. CS 2110, November 19, 2015, 7:30 PM Total. Sorting Invariants Max Score Grader

Prelim 2. CS 2110, November 19, 2015, 7:30 PM Total. Sorting Invariants Max Score Grader Prelim 2 CS 2110, November 19, 2015, 7:30 PM 1 2 3 4 5 6 Total Question True Short Complexity Searching Trees Graphs False Answer Sorting Invariants Max 20 15 13 14 17 21 100 Score Grader The exam is closed

More information

Sorting. Sorting in Arrays. SelectionSort. SelectionSort. Binary search works great, but how do we create a sorted array in the first place?

Sorting. Sorting in Arrays. SelectionSort. SelectionSort. Binary search works great, but how do we create a sorted array in the first place? Sorting Binary search works great, but how do we create a sorted array in the first place? Sorting in Arrays Sorting algorithms: Selection sort: O(n 2 ) time Merge sort: O(nlog 2 (n)) time Quicksort: O(n

More information

University of Waterloo CS240, Winter 2010 Assignment 2

University of Waterloo CS240, Winter 2010 Assignment 2 University of Waterloo CS240, Winter 2010 Assignment 2 Due Date: Wednesday, February 10, at 5:00pm Please read http://www.student.cs.uwaterloo.ca/~cs240/w10/guidelines.pdf for guidelines on submission.

More information

SEARCHING, SORTING, AND ASYMPTOTIC COMPLEXITY. Lecture 11 CS2110 Spring 2016

SEARCHING, SORTING, AND ASYMPTOTIC COMPLEXITY. Lecture 11 CS2110 Spring 2016 1 SEARCHING, SORTING, AND ASYMPTOTIC COMPLEXITY Lecture 11 CS2110 Spring 2016 Time spent on A2 2 Histogram: [inclusive:exclusive) [0:1): 0 [1:2): 24 ***** [2:3): 84 ***************** [3:4): 123 *************************

More information

How invariants help writing loops Author: Sander Kooijmans Document version: 1.0

How invariants help writing loops Author: Sander Kooijmans Document version: 1.0 How invariants help writing loops Author: Sander Kooijmans Document version: 1.0 Why this document? Did you ever feel frustrated because of a nasty bug in your code? Did you spend hours looking at the

More information

Prelim 1 Solutions. CS 2110, March 10, 2015, 5:30 PM Total Question True False. Loop Invariants Max Score Grader

Prelim 1 Solutions. CS 2110, March 10, 2015, 5:30 PM Total Question True False. Loop Invariants Max Score Grader Prelim 1 Solutions CS 2110, March 10, 2015, 5:30 PM 1 2 3 4 5 Total Question True False Short Answer Recursion Object Oriented Loop Invariants Max 20 15 20 25 20 100 Score Grader The exam is closed book

More information

CS134 Spring 2005 Final Exam Mon. June. 20, 2005 Signature: Question # Out Of Marks Marker Total

CS134 Spring 2005 Final Exam Mon. June. 20, 2005 Signature: Question # Out Of Marks Marker Total CS134 Spring 2005 Final Exam Mon. June. 20, 2005 Please check your tutorial (TUT) section from the list below: TUT 101: F 11:30, MC 4042 TUT 102: M 10:30, MC 4042 TUT 103: M 11:30, MC 4058 TUT 104: F 10:30,

More information

UNIT-2. Problem of size n. Sub-problem 1 size n/2. Sub-problem 2 size n/2. Solution to the original problem

UNIT-2. Problem of size n. Sub-problem 1 size n/2. Sub-problem 2 size n/2. Solution to the original problem Divide-and-conquer method: Divide-and-conquer is probably the best known general algorithm design technique. The principle behind the Divide-and-conquer algorithm design technique is that it is easier

More information

APT Session 2: Python

APT Session 2: Python APT Session 2: Python Laurence Tratt Software Development Team 2017-10-20 1 / 17 http://soft-dev.org/ What to expect from this session: Python 1 What is Python? 2 Basic Python functionality. 2 / 17 http://soft-dev.org/

More information

Lecture 7. Binary Search Trees / AVL Trees

Lecture 7. Binary Search Trees / AVL Trees Lecture 7. Binary Searc Trees / AVL Trees T. H. Cormen, C. E. Leiserson and R. L. Rivest Introduction to Algoritms, 3rd Edition, MIT Press, 2009 Sungkyunkwan University Hyunseung Coo coo@skku.edu Copyrigt

More information

CS 1110 Prelim 2 November 14th, 2013

CS 1110 Prelim 2 November 14th, 2013 CS 1110 Prelim 2 November 14th, 2013 This 90-minute exam has 6 questions worth a total of 100 points. Scan the whole test before starting. Budget your time wisely. Use the back of the pages if you need

More information

Advanced Tree. Structures. AVL Tree. Outline. AVL Tree Recall, Binary Search Tree (BST) is a special case of. Splay Tree (Ch 13.2.

Advanced Tree. Structures. AVL Tree. Outline. AVL Tree Recall, Binary Search Tree (BST) is a special case of. Splay Tree (Ch 13.2. ttp://1...0/csd/ Data tructure Capter 1 Advanced Tree tructures Dr. atrick Can cool of Computer cience and Engineering out Cina Universit of Tecnolog AVL Tree Recall, Binar earc Tree (BT) is a special

More information

Exam I Principles of Imperative Computation, Summer 2011 William Lovas. May 27, 2011

Exam I Principles of Imperative Computation, Summer 2011 William Lovas. May 27, 2011 Exam I 15-122 Principles of Imperative Computation, Summer 2011 William Lovas May 27, 2011 Name: Sample Solution Andrew ID: wlovas Instructions This exam is closed-book with one sheet of notes permitted.

More information

61A Lecture 6. Monday, February 2

61A Lecture 6. Monday, February 2 61A Lecture 6 Monday, February 2 Announcements Homework 2 due Monday 2/2 @ 11:59pm Project 1 due Thursday 2/5 @ 11:59pm Project party on Tuesday 2/3 5pm-6:30pm in 2050 VLSB Partner party on Wednesday 2/4

More information

CS240 Fall Mike Lam, Professor. Quick Sort

CS240 Fall Mike Lam, Professor. Quick Sort ??!!!!! CS240 Fall 2015 Mike Lam, Professor Quick Sort Merge Sort Merge sort Sort sublists (divide & conquer) Merge sorted sublists (combine) All the "hard work" is done after recursing Hard to do "in-place"

More information

61A Lecture 7. Monday, September 15

61A Lecture 7. Monday, September 15 61A Lecture 7 Monday, September 15 Announcements Homework 2 due Monday 9/15 at 11:59pm Project 1 deadline extended, due Thursday 9/18 at 11:59pm! Extra credit point if you submit by Wednesday 9/17 at 11:59pm

More information