Teach A level Compu/ng: Algorithms and Data Structures

Similar documents
15110 Principles of Computing, Carnegie Mellon University - CORTINA. Binary Search. Required: List L of n unique elements.

Sorting Pearson Education, Inc. All rights reserved.

CSE 373 NOVEMBER 8 TH COMPARISON SORTS

We can use a max-heap to sort data.

Searching in General

Sorting and Searching

Problem. Input: An array A = (A[1],..., A[n]) with length n. Output: a permutation A of A, that is sorted: A [i] A [j] for all. 1 i j n.

UNIT 5C Merge Sort. Course Announcements

Algorithm Efficiency & Sorting. Algorithm efficiency Big-O notation Searching algorithms Sorting algorithms

CSE 373: Data Structure & Algorithms Comparison Sor:ng

Java How to Program, 9/e. Copyright by Pearson Education, Inc. All Rights Reserved.

CSE 373: Data Structures & Algorithms More Sor9ng and Beyond Comparison Sor9ng

9/10/12. Outline. Part 5. Computational Complexity (2) Examples. (revisit) Properties of Growth-rate functions(1/3)

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

Overview of Sorting Algorithms

For searching and sorting algorithms, this is particularly dependent on the number of data elements.

Lecture 6 Sorting and Searching

STA141C: Big Data & High Performance Statistical Computing

Lecture Notes on Quicksort

Sorting. Hsuan-Tien Lin. June 9, Dept. of CSIE, NTU. H.-T. Lin (NTU CSIE) Sorting 06/09, / 13

Programming II (CS300)

CSc 110, Spring 2017 Lecture 39: searching

algorithm evaluation Performance

Priority queue ADT Heaps. Lecture 21

Computer Science 4U Unit 1. Programming Concepts and Skills Algorithms

Lecture Notes on Quicksort

Component 02. Algorithms and programming. Sorting Algorithms and Searching Algorithms. Matthew Robinson

Divide and Conquer Algorithms: Advanced Sorting

CS240 Fall Mike Lam, Professor. Quick Sort

Programming II (CS300)

STA141C: Big Data & High Performance Statistical Computing

Question 7.11 Show how heapsort processes the input:

Quick Sort. CSE Data Structures May 15, 2002

Teach A level Compu/ng: Algorithms and Data Structures

CompSci 105. Sorting Algorithms Part 2

Lecture 7 Quicksort : Principles of Imperative Computation (Spring 2018) Frank Pfenning

Sorting and Selection

Teach A level Compu/ng: Algorithms and Data Structures

CS261 Data Structures. Ordered Bag Dynamic Array Implementa;on

Sorting. Quicksort analysis Bubble sort. November 20, 2017 Hassan Khosravi / Geoffrey Tien 1

Selection, Bubble, Insertion, Merge, Heap, Quick Bucket, Radix

CSE 373: Data Structures and Algorithms

Searching and Sorting (Savitch, Chapter 7.4)

Principles of Algorithm Analysis. Biostatistics 615/815

CSE 373: Data Structures and Algorithms

26 How Many Times Must a White Dove Sail...

What is an algorithm?

2/14/13. Outline. Part 5. Computational Complexity (2) Examples. (revisit) Properties of Growth-rate functions(1/3)

Key question: how do we pick a good pivot (and what makes a good pivot in the first place)?

Data Structures and Algorithms for Engineers

Lecture Notes 14 More sorting CSS Data Structures and Object-Oriented Programming Professor Clark F. Olson

CS 137 Part 8. Merge Sort, Quick Sort, Binary Search. November 20th, 2017

Unit 6 Chapter 15 EXAMPLES OF COMPLEXITY CALCULATION

UNIT 5C Merge Sort Principles of Computing, Carnegie Mellon University

CSE 332: Data Structures & Parallelism Lecture 12: Comparison Sorting. Ruth Anderson Winter 2019

7. Sorting I. 7.1 Simple Sorting. Problem. Algorithm: IsSorted(A) 1 i j n. Simple Sorting

CSE 373: Data Structures and Algorithms More Asympto<c Analysis; More Heaps

Divide and Conquer Sorting Algorithms and Noncomparison-based

About this exam review

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

Measuring algorithm efficiency

! 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.

DIVIDE AND CONQUER ALGORITHMS ANALYSIS WITH RECURRENCE EQUATIONS

Lecture 7: Searching and Sorting Algorithms

CSE 143. Two important problems. Searching and Sorting. Review: Linear Search. Review: Binary Search. Example. How Efficient Is Linear Search?

CS61BL. Lecture 5: Graphs Sorting

CSE373: Data Structure & Algorithms Lecture 18: Comparison Sorting. Dan Grossman Fall 2013

Sorting Algorithms. + Analysis of the Sorting Algorithms

CS 323 Lecture 1. Design and Analysis of Algorithms. Hoeteck Wee

IS 709/809: Computational Methods in IS Research. Algorithm Analysis (Sorting)

Introduction to Computer Science and Programming for Astronomers

Searching and Sorting

CSc 120. Introduction to Computer Programming II. 14: Stacks and Queues. Adapted from slides by Dr. Saumya Debray

Sorting. CPSC 259: Data Structures and Algorithms for Electrical Engineers. Hassan Khosravi

Algorithms Lecture 11. UC Davis, ECS20, Winter Discrete Mathematics for Computer Science

having any value between and. For array element, the plot will have a dot at the intersection of and, subject to scaling constraints.

COSC242 Lecture 7 Mergesort and Quicksort

Chapter 3. The Efficiency of Algorithms INVITATION TO. Computer Science

(Sequen)al) Sor)ng. Bubble Sort, Inser)on Sort. Merge Sort, Heap Sort, QuickSort. Op)mal Parallel Time complexity. O ( n 2 )

Algorithm Analysis CISC4080 CIS, Fordham Univ. Instructor: X. Zhang

COMP Data Structures

Sorting: Given a list A with n elements possessing a total order, return a list with the same elements in non-decreasing order.

"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

Algorithms for Arrays Vectors Pointers CS 16: Solving Problems with Computers I Lecture #14

SAMPLE OF THE STUDY MATERIAL PART OF CHAPTER 6. Sorting Algorithms

Divide and Conquer 4-0

Cpt S 122 Data Structures. Sorting

Algorithms in Systems Engineering ISE 172. Lecture 12. Dr. Ted Ralphs

Building Java Programs Chapter 13

INSTITUTE OF AERONAUTICAL ENGINEERING

Search,Sort,Recursion

SORTING. Comparison of Quadratic Sorts

Lab 6 Sorting. Sup Biotech 3 Python. Pierre Parutto

Search and Sorting Algorithms

Unit-2 Divide and conquer 2016

Chapter 10. Sorting and Searching Algorithms. Fall 2017 CISC2200 Yanjun Li 1. Sorting. Given a set (container) of n elements

CSCI 121: Searching & Sorting Data

O(n): printing a list of n items to the screen, looking at each item once.

Announcements. Lab 11 is due tomorrow. Quiz 6 is on Monday. Ninja session tonight, 7-9pm. The final is in two weeks!

10/11/2013. Chapter 3. Objectives. Objectives (continued) Introduction. Attributes of Algorithms. Introduction. The Efficiency of Algorithms

Transcription:

Teach A level Compu/ng: Algorithms and Data Structures Eliot Williams @MrEliotWilliams Course Outline Representa+ons of data structures: Arrays, tuples, Stacks, Queues,Lists 2 Recursive Algorithms 3 Searching and Sor*ng - EW will be late! 4 Hashing and Dic*onaries, Graphs and Trees 5 Depth and breadth first searching ; tree traversals

Searching, sor/ng and algorithm analysis Algorithm Analysis An algorithm is a self-contained sequence of ac*ons to be performed We can analysis an algorithms efficiency using the following measures The *me it takes to finish How much memory it needs 2

Measuring /me FirsTme.py Run the program how long do the simple expressions take Change the numbers and opera*ons in the first three statements Uncomment the further statements (each block at a *me) Measuring /me FirsTme.py Different computers will produce different *mes Time taken to execute a command is not equal on the same computer Time taken to execute a sequence of commands grows with the length of the sequence 3

We don t need absolute /me measures We want to understand fundamental proper*es of our algorithms, not things specific to a par*cular input or machine. Problem (size N) Time: T Problem (size 2*N) Any Computer Time: T2 We therefore only care about how the *me increases Maybe the *me stays the same Maybe doubling the size, doubles the *me Maybe doubling the size, more than doubles the *me Big O Nota/on Big O Nota*on is a formal way of sta*ng how the *me taken by an algorithm relates to an input size Try second*me.py and second*men.py O() input size does not effect the *me O(n) input grows linearly with input size 4

Big O Nota/on Big O Nota*on is a formal way of sta*ng how the *me taken by an algorithm relates to an input size we only need to consider the largest term O() input size does not effect the *me O(log n) logarithmic growth O(n) *me grows linearly with input size O(n 2 ) *me is directly propor*onal to the square of the input size O(n y ) Exponen*al growth O(2 n ) polynomial growth intractable problems. O(n 2 ) O(n) O(log n) O() 5

Search: The Problem Find a target value in the list Is it there? If so, at what index? 2 3 4 5 6 7 8 9 7 3 52 9 4 34 76 28 92 44 6 Target = 4, found at index 4 Target = 27, not found Linear Search Target 34 Index 2 3 4 5 6 7 8 9 7 3 52 9 4 34 76 28 92 Idea: look at each entry in turn Steps Start at index = Is Array[Index] equal to the target? If yes, stop; otherwise increment the index Stop when index is one less than the length 6

Exercise. and.2 Linear Search Algorithm Algorithm in pseudo code Array is A index =! while index < length of array! if A[index] equals target! for i is to length of array-! if A[i] equals target! return i! return index! index = index +! return - to show not found! return - to show not found! 7

Binary Search Searching a sorted list Searching a Sorted List Ques*on: why are books in the library kept in order? 8

Searching a Sorted List Ques*on: why are books in the library kept in order? In an ordered array, we do not have to look at every item Before this one Aper this one quickly find the correct loca*on What is the best algorithm for looking? Binary Search Sorted Lists Which half is it in? Look in the middle. Target Index 4 4 2 3 4 5 6 7 8 9 7 9 28 3 34 4 52 76 92 7 6 9

Binary Search Algorithm Key idea: in which part of the array are we looking? lep right Binary Search 3 Cases lep X mid right Case : X equals target value Case 2: X < target value Case 3: X > target value lep right lep X right

Binary Search Algorithm left =! right = length of array! while right > left:! mid = average of left and right! if A[mid] equals target! found it at 'mid'! if A[mid] < target! search between mid+ & right! otherwise! search between left & mid! return not found! Binary Search Python def BSearch(A, target):! left =! right = len(a)! while right > left:! mid = (left + right) // 2! if A[mid] == target:! return mid! elif A[mid] < target:! left = mid+! else:! right = mid! return -!

Binary Search Complexity 7 9 28 3 34 4 52 Number of steps = number of binary digit to index O(log N) Binary Search (Recursive) Search a sorted array is E in the array? Base case: empty array or E found Recursive case: Compare E with the middle element If E smaller, search the lep half If E larger, search the right half 2

Exercise.3 Complete the itera5ve and recursive binary search procedures in binarysearch.py Sor/ng 3

Sor/ng: The Problem 2 3 4 5 6 7 8 9 7 3 52 9 4 34 76 28 92 7 9 28 3 34 4 52 76 92 Arrange array in order Same entries; in order swap entries Proper*es Speed, space, stable, Discussion Sort a pack of top trumps Describe how you do it 4

Bubble Sort Insight 2 3 4 5 6 7 8 9 7 3 52 9 4 34 76 28 92 Librarian finds two books out of order Swap them over! Repeatedly Bubble Sort Descrip/on 2 3 4 5 6 7 8 9 7 3 52 9 4 34 76 28 92 Pass through the array (star*ng on the lep) Swap any entries that are out of order Repeat un*l no swaps needed Quiz: show array aper first pass 5

Bubble Sort Algorithm Sor*ng Array A Assume indices to length- while swaps happen! index =! while index < length! if A[index-] > A[index]! swap A[index-] and A[index]! index = index +! Exercise 2. Bubble Sort Complete the table to show the successive passes of a bubble sort 6

Demo sor*ngdemo.py Bubble Sort Proper/es Stable Inefficient O(N 2 ) Double length *me increases 4-fold hvp://www.sor*ng-algorithms.com/bubble-sort 7

Inser/on Sort Insight not yet ordered ordered 7 3 52 9 4 34 76 28 92 Imagine part of the array is ordered Insert the next item into the correct place not yet ordered ordered 7 3 52 9 4 34 28 76 92 Inser/on Sort Descrip/on not yet ordered ordered 7 3 52 9 4 34 76 28 92 Start with one entry ordered Take each entry in turn Insert into ordered part by swapping with lower values Stop when all entries inserted 8

Inser/on Sort Algorithm Sor*ng Array A Assume indices to length- index =! while index < length of array! ix = index! while A[ix] < A[ix-] and ix >! swap A[ix] and A[ix-]! ix = ix! index = index +! A[:index] ordered Same values Inner loop: insert into ordered list Exercises 3.x 9

Quicksort Insight How could we share out sor*ng between two people? Choose a value V Give first person all values < V Give second person all values > V When there is only a single entry it is sorted Quicksort Example 7 3 52 9 4 34 76 28 28 7 9 3 34 76 52 4 all < 3 all >= 3 7 9 28 9 28 4 34 52 76 34 4 2

Quicksort Descrip/on Choose a pivot value Par**on the array Values less than the pivot to the lep The pivot Values greater than the pivot to the right Repeat process on each par**on un*l par**on has no more than one value Work done in par++on Tasks Quickfirststep.py going through only once quicks Separate into two new lists and put back together with pivot Checks basic understanding 2

Recursive Sor/ng Concept Split array in two halves, sort each half Combine two sorted arrays Single item sorted (base case) Two algorithms Merge sort Halve array merge two sorted lists Quicksort Par**on array combine easy Merge Sort Insight How could we share out sor*ng between two people? Half it and sort each half Merge the two sorted lists When there is only a single entry it is sorted 22

Merge Sort 7 3 52 9 4 34 76 Decompose 7 3 52 9 4 34 76 Recursion MAGIC 7 9 3 52 34 4 76 Merge 7 9 3 34 4 52 76 Merging Work done in the merge Repeatedly select smallest 34 4 76 7 9 3 52 7 9 3 34 4 52 76 23

Proper/es Inser*on sort hvp://www.sor*ng-algorithms.com/inser*on-sort O(N 2 ) same as bubble sort Stable Quicksort hvp://www.sor*ng-algorithms.com/quick-sort More efficient: O(N logn) Not stable Merge sort hvp://www.sor*ng-algorithms.com/merge-sort O(N log N ) same as quick sort but extra space Stable 24