Teach A level Computing: Algorithms and Data Structures

Slides:



Advertisements
Similar presentations
MATH 224 – Discrete Mathematics
Advertisements

Algorithms Analysis Lecture 6 Quicksort. Quick Sort Divide and Conquer.
CMPS1371 Introduction to Computing for Engineers SORTING.
Chapter 19: Searching and Sorting Algorithms
Ver. 1.0 Session 5 Data Structures and Algorithms Objectives In this session, you will learn to: Sort data by using quick sort Sort data by using merge.
CHAPTER 11 Sorting.
 2006 Pearson Education, Inc. All rights reserved Searching and Sorting.
COMP s1 Computing 2 Complexity
1 Data Structures and Algorithms Sorting. 2  Sorting is the process of arranging a list of items into a particular order  There must be some value on.
(C) 2010 Pearson Education, Inc. All rights reserved. Java How to Program, 8/e.
Chapter 12 Recursion, Complexity, and Searching and Sorting
Computer Science 101 Fast Searching and Sorting. Improving Efficiency We got a better best case by tweaking the selection sort and the bubble sort We.
Computer Science Searching & Sorting.
Merge Sort. What Is Sorting? To arrange a collection of items in some specified order. Numerical order Lexicographical order Input: sequence of numbers.
 2005 Pearson Education, Inc. All rights reserved Searching and Sorting.
 Pearson Education, Inc. All rights reserved Searching and Sorting.
C++ Programming: From Problem Analysis to Program Design, Second Edition Chapter 19: Searching and Sorting.
Data Structure Introduction.
Searching & Sorting Programming 2. Searching Searching is the process of determining if a target item is present in a list of items, and locating it A.
UNIT 5.  The related activities of sorting, searching and merging are central to many computer applications.  Sorting and merging provide us with a.
Chapter 9 Sorting. The efficiency of data handling can often be increased if the data are sorted according to some criteria of order. The first step is.
Intro To Algorithms Searching and Sorting. Searching A common task for a computer is to find a block of data A common task for a computer is to find a.
ICS201 Lecture 21 : Sorting King Fahd University of Petroleum & Minerals College of Computer Science & Engineering Information & Computer Science Department.
PREVIOUS SORTING ALGORITHMS  BUBBLE SORT –Time Complexity: O(n 2 ) For each item, make (n –1) comparisons Gives: Comparisons = (n –1) + (n – 2)
Chapter 9: Sorting1 Sorting & Searching Ch. # 9. Chapter 9: Sorting2 Chapter Outline  What is sorting and complexity of sorting  Different types of.
Quick Sort Modifications By Mr. Dave Clausen Updated for Python.
329 3/30/98 CSE 143 Searching and Sorting [Sections 12.4, ]
WHICH SEARCH OR SORT IS BETTER?. COMPARING ALGORITHMS Time efficiency refers to how long it takes an algorithm to run Space efficiency refers to the amount.
INTRO2CS Tirgul 8 1. Searching and Sorting  Tips for debugging  Binary search  Sorting algorithms:  Bogo sort  Bubble sort  Quick sort and maybe.
1 Algorithms Searching and Sorting Algorithm Efficiency.
Sort Algorithm.
Chapter 11 Sorting Acknowledgement: These slides are adapted from slides provided with Data Structures and Algorithms in C++, Goodrich, Tamassia and Mount.
Sorts, CompareTo Method and Strings
Fundamentals of Algorithms MCS - 2 Lecture # 11
16 Searching and Sorting.
19 Searching and Sorting.
Searching and Sorting Algorithms
COP 3503 FALL 2012 Shayan Javed Lecture 15
Lecture 14 Searching and Sorting Richard Gesick.
COMP108 Algorithmic Foundations Algorithm efficiency
Warmup What is an abstract class?
COMP 53 – Week Seven Big O Sorting.
Introduction to Algorithms
Description Given a linear collection of items x1, x2, x3,….,xn
Algorithm design and Analysis
Advanced Sorting Methods: Shellsort
CSc 110, Spring 2017 Lecture 39: searching.
Complexity Present sorting methods. Binary search. Other measures.
Quicksort analysis Bubble sort
Algorithm An algorithm is a finite set of steps required to solve a problem. An algorithm must have following properties: Input: An algorithm must have.
CSC215 Lecture Algorithms.
Unit-2 Divide and Conquer
Teach A level Computing: Algorithms and Data Structures
Welcome to CIS 068 ! Lesson 9: Sorting CIS 068.
Lecture 11 Searching and Sorting Richard Gesick.
MSIS 655 Advanced Business Applications Programming
C++ Plus Data Structures
Searching CLRS, Sections 9.1 – 9.3.
24 Searching and Sorting.
Sub-Quadratic Sorting Algorithms
Sorting "There's nothing in your head the sorting hat can't see. So try me on and I will tell you where you ought to be." -The Sorting Hat, Harry Potter.
Chapter 4.
CSE 326: Data Structures Sorting
Search,Sort,Recursion.
Searching/Sorting/Searching
Core Assessments Core #1: This Friday (5/4) Core #2: Tuesday, 5/8.
Searching and Sorting Hint at Asymptotic Complexity
Advanced Sorting Methods: Shellsort
CMPT 225 Lecture 10 – Merge Sort.
Presentation transcript:

Teach A level Computing: Algorithms and Data Structures Eliot Williams @MrEliotWilliams

Course Outline 1 Representations of data structures: Arrays, tuples, Stacks, Queues,Lists 2 Recursive Algorithms 3 Searching and Sorting - EW will be late! 4 Hashing and Dictionaries, Graphs and Trees   5 Depth and breadth first searching ; tree traversals

Searching, sorting and algorithm analysis

Algorithm Analysis An algorithm is a self-contained sequence of actions to be performed We can analysis an algorithms efficiency using the following measures The time it takes to finish How much memory it needs Cloud computing allows you to rent processing by the hour, time to execute is money!

Measuring time Firsttime.py Run the program how long do the simple expressions take Change the numbers and operations in the first three statements Uncomment the further statements (each block at a time)

Measuring time Firsttime.py Different computers will produce different times 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

We don’t need absolute time measures We want to understand fundamental properties of our algorithms, not things specific to a particular input or machine. We therefore only care about how the time increases Maybe the time stays the same Maybe doubling the size, doubles the time Maybe doubling the size, more than doubles the time Problem (size N) Time: T1 Any Computer Problem (size 2*N) Time: T2

Big O Notation Big O Notation is a formal way of stating how the time taken by an algorithm relates to an input size Try secondtime.py and secondtimen.py O(1) input size does not effect the time O(n) input grows linearly with input size

Big O Notation Big O Notation is a formal way of stating how the time taken by an algorithm relates to an input size –we only need to consider the largest term O(1) input size does not effect the time O(log n) logarithmic growth O(n) time grows linearly with input size O(n2) time is directly proportional to the square of the input size O(ny) Exponential growth O(2n) polynomial growth – intractable problems….

O(n) O(n2) O(log n) O(1)

Search: The Problem Find a target value in the list Is it there? If so, at what index? Target = 41, found at index 4 Target = 27, not found 0 1 2 3 4 5 6 7 8 9 10 11 17 31 52 19 41 34 76 11 28 92 44 61

Linear Search 0 1 2 3 4 5 6 7 8 9 34 Target Index 17 31 52 19 41 34 76 11 28 92 Idea: look at each entry in turn Steps Start at index = 0 Is Array[Index] equal to the target? If yes, stop; otherwise increment the index Stop when index is one less than the length

Exercise 1.1 and 1.2

Linear Search – Algorithm Algorithm in pseudo code Array is A index = 0 while index < length of array if A[index] equals target return index index = index + 1 return -1 to show not found for i is 0 to length of array-1 if A[i] equals target return i return -1 to show not found

Searching a sorted list Binary Search Searching a sorted list

Searching a Sorted List Question: why are books in the library kept in order?

Searching a Sorted List Question: 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” “After this one” … quickly find the correct location What is the best algorithm for looking?

Binary Search – Sorted Lists Which half is it in? Look in the middle. 0 1 2 3 4 5 6 7 8 9 41 Target Index 11 17 19 28 31 34 41 52 76 92 4 6 7

Binary Search – Algorithm Key idea: in which part of the array are we looking? left right

Binary Search – 3 Cases X X left mid right Case 1: X equals target value Case 2: X < target value Case 3: X > target value left right X left right

Binary Search – Algorithm left = 0 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+1 & right otherwise search between left & mid return not found

Binary Search – Python def BSearch(A, target): left = 0 right = len(A) while right > left: mid = (left + right) // 2 if A[mid] == target: return mid elif A[mid] < target: left = mid+1 else: right = mid return -1

Binary Search – Complexity 1 1 1 1 1 1 1 11 17 19 28 31 34 41 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 left half If E larger, search the right half

Exercise 1.3 Complete the iterative and recursive binary search procedures in binarysearch.py

Sorting

Sorting: The Problem 0 1 2 3 4 5 6 7 8 9 17 31 52 19 41 34 76 11 28 92 11 17 19 28 31 34 41 52 76 92 Arrange array in order Same entries; in order – swap entries Properties Speed, space, stable,

Discussion Sort a pack of top trumps Describe how you do it

Bubble Sort – Insight 0 1 2 3 4 5 6 7 8 9 17 31 52 19 41 34 76 11 28 92 Librarian finds two books out of order Swap them over! Repeatedly

Bubble Sort – Description 0 1 2 3 4 5 6 7 8 9 17 31 52 19 41 34 76 11 28 92 Pass through the array (starting on the left) Swap any entries that are out of order Repeat until no swaps needed Quiz: show array after first pass

Bubble Sort – Algorithm Sorting Array A Assume indices 0 to length-1 while swaps happen index = 1 while index < length if A[index-1] > A[index] swap A[index-1] and A[index] index = index + 1

Exercise 2.1 Bubble Sort Complete the table to show the successive passes of a bubble sort

Demo sortingDemo.py

Bubble Sort – Properties Stable Inefficient O(N2) Double length – time increases 4-fold http://www.sorting-algorithms.com/bubble-sort

Insertion Sort – Insight not yet ordered ordered 17 31 52 19 41 34 76 11 28 92 Imagine part of the array is ordered Insert the next item into the correct place not yet ordered ordered 17 31 52 19 41 34 11 28 76 92

Insertion Sort – Description not yet ordered ordered 17 31 52 19 41 34 76 11 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

Insertion Sort – Algorithm Sorting Array A Assume indices 0 to length-1 A[0:index] ordered Same values index = 1 while index < length of array ix = index while A[ix] < A[ix-1] and ix > 0 swap A[ix] and A[ix-1] ix = ix – 1 index = index + 1 Inner loop: insert into ordered list

Exercises 3.x

Quicksort – Insight How could we share out sorting 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 17 31 52 19 41 34 76 11 28 28 17 19 11 31 34 76 52 41 all < 31 all >= 31 11 17 19 28 41 34 52 76 19 28 34 41

Quicksort Description Choose a pivot value Partition the array Values less than the pivot to the left The pivot Values greater than the pivot to the right Repeat process on each partition … until partition has no more than one value Work done in partition

Tasks Quickfirststep.py – going through only once quickS1 Separate into two new lists and put back together with pivot Checks basic understanding

Recursive Sorting Concept Two algorithms Merge sort Quicksort 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 Partition array – combine easy

Merge Sort – Insight How could we share out sorting between two people? Half it and sort each half Merge the two sorted lists When there is only a single entry – it is sorted

Merge Sort 17 31 52 19 41 34 76 11 Decompose 17 31 52 19 41 34 76 11 Recursion MAGIC 17 19 31 52 11 34 41 76 11 17 19 31 34 41 52 76 Merge

Merging Work done in the merge Repeatedly select smallest 11 34 41 76 17 19 31 52 11 17 19 31 34 41 52 76

Properties http://www.sorting-algorithms.com/insertion-sort Insertion sort O(N2) – same as bubble sort Stable Quicksort More efficient: O(N logN) Not stable Merge sort O(N log N ) – same as quick sort but extra space http://www.sorting-algorithms.com/quick-sort http://www.sorting-algorithms.com/merge-sort