Presentation is loading. Please wait.

Presentation is loading. Please wait.

Lesson Objectives Aims Understand the following “standard algorithms”:

Similar presentations


Presentation on theme: "Lesson Objectives Aims Understand the following “standard algorithms”:"— Presentation transcript:

1 Lesson Objectives Aims Understand the following “standard algorithms”:
Bubble sort Insertion sort Merge sort Quick sort Dijkstra’s shortest path algorithm A* algorithm Binary search Linear search

2 Pre-notes Most of these algorithms are GCSE topics
If you’ve done the “new” 9-1 GCSE you will already know most of them The only difference here is, again, you need to know what the code for each looks like You may well be asked questions where you have to complete an algorithm or identify an algorithm simply by looking at the code

3 Sorting Algorithms

4 Bubble Sort Bubble sort – moving through a list repeatedly, comparing pairs of elements, swapping elements that are in the wrong order.

5 Bubble Sort Algorithm SwapsMade = True Repeat until SwapsMade = False
Take the first element and second element from the list IF element 1 > element 2 THEN Swap them ELSE Do nothing IF no more elements are left: IF SwapsMade = True, then return to the start – GOTO Step 3 ELSE set SwapsMade = False End Repeat

6 Merge Sort List – a set of data. Merge sort:
The list is split into individual elements These lists are then combined into a NEW list, 2 lists at a time.

7 Merge Sort Algorithm Split the list into individual elements
Get two lists Compare the first element in both lists. Put the smallest into a new list. Continue steps 3 and 4 with each remaining element in the two lists Get the next two lists and repeat 3-5 Repeat 2 – 6 until only 1 list remains

8 Merge Sort – Complete Example
22 13 5 2 8 15 Split the list into individual elements: 22 13 5 2 8 15 22 Merge individual lists 13 22 2 5 8 15 22 Merge sub-lists 2 5 13 22 8 15 22 Merge sub-lists 2 5 8 13 15 22

9 Use a merge sort to sort the list:
Task Use a merge sort to sort the list: 2 19 3 45 77 10 25 29 60 1

10 Answer 2 19 3 45 77 10 25 29 60 1 2 19 3 45 77 10 25 29 60 1 2 19 3 45 10 77 25 29 1 60 2 3 19 45 10 25 29 77 1 60 2 3 10 19 25 29 45 77 1 60 1 2 3 10 19 25 29 45 60 77

11 Insertion Sort Inserting each element in to the correct location! Uses the concept of a “sorted list” and an “unsorted list”

12 Insertion Sort - Algorithm
Take the first element 1 consider this to be in the right place - the ‘sorted’ list. The remaining elements are an ‘unsorted’ list. Get the next element from the ‘unsorted’ list. Compare this to the first element in the ‘sorted’ list. IF it is smaller, put it in front of that element (move the others along). ELSEIF it is larger, compare with the next. ELSEIF there are no more elements in the ‘sorted’ list put it in the final position. REPEAT UNTIL all element in the ‘unsorted’ list are in the ‘sorted’ list.

13 Insertion Sort Insertion sort this list 12 9 3 15 2 7 12 is the sorted list 12 9 3 15 2 7 Take element 1 of unsorted list 12 9 3 15 2 7 Compare to first element in sorted list 12 9 3 15 2 7 First element is greater: so put it on left 9 12 3 15 2 7 Take next element or unordered list 9 12 3 15 2 7 Compare to first element in sorted list 9 12 3 15 2 7 First element is greater: so put it on left 3 9 12 15 2 7

14 Insertion Sort Take element 1 of unsorted list 3 9 12 15 2 7 Compare to first element in sorted list 3 9 12 15 2 7 First element is smaller: so move to next element in ordered list 3 9 12 15 2 7 First element is smaller: so move to next element in ordered list 3 9 12 15 2 7 No more elements left, so insert at the end of the ordered list 3 9 12 15 2 7

15 Insertion Sort Can you talk through the next steps? 3 9 12 15 2 7 3 9

16 Take element 1 of unsorted list
2 3 7 9 12 15 Compare to element 1 in sorted list. It is larger. 2 3 7 9 12 15 Compare to element 2 in sorted list. It is larger. 2 3 7 9 12 15 Compare to element 3 in sorted list. It is larger. 2 3 7 9 12 15 Compare to element 4 in sorted list. It is larger. 2 3 7 9 12 15 Compare to element 5 in sorted list. It is larger. 2 3 7 9 12 15 Compare to element 6 in sorted list. 2 3 7 9 12 15 It is the same, so place it to the right. 2 3 7 9 12 15

17 Task Use an Insertion Sort to sort the list: 3 19 2 44 56 7 12

18 Quicksort There is an excellent explanation and visual demo of quick sort here: Should the link not work, it is in the shared area Make notes and be ready to explain it.

19 For each of the four sorting algorithms:
Task For each of the four sorting algorithms: Find a pseudocode algorithm for each Add it to your notes Comment the code

20 Searching Algorithms

21 Dijkstra’s Shortest Path
This algorithm finds the shortest path to a point in a Graph data structure A graph is simply a collection of connected nodes, with a value determining the distance between nodes Much like towns and cities on a map

22 Youtube is rammed with tutorials and demos of this algorithm
Your task is to find the best one and: Send the link to everyone Prepare a short presentation to explain it. One or two will be chosen at random to explain to the class.

23 A* Algorithm – don’t write this down
Create two lists : open and closed. Closed list contains squares we do not need to evaluate again. Open list is the opposite. Each square will be given a score which determines how good that square is in terms of getting us to our goal. Get the square on the open list which has the lowest score. Let’s call this square S. Remove S from the open list and add S to the closed list. For each square T in S’s walkable adjacent tiles: If T is in the closed list: Ignore it. If T is not in the open list: Add it and compute its score. If T is already in the open list: Check if the F score is lower when we use the current generated path to get there. If it is, update its score and update its parent as well. Confused? Next slide!

24 Confused? You should be! Read this:
And this: Still stuck? Next slide…

25 Again…Youtube is rammed with tutorials and demos of this algorithm
Guess what…! Your task is to find the best one and: Send the link to everyone Prepare a short presentation to explain it. One or two will be chosen at random to explain to the class.

26 Linear Search Get target number Start at the beginning of the list
Get an item If the item matches the target then it is found If not, move on to the next item in the list If the end of the list is reached, the target does not exist in the list

27 Draw the flow chart for linear search
Task Draw the flow chart for linear search What advantages do you think linear search has? Write down at least two. Answers: Linear search is very simple to program Linear search does NOT need the list to be sorted first.

28 Linear Search – Flow chart

29 What may be a limitation of the linear search?
Disadvantages What may be a limitation of the linear search? As the size of list grows, so does the time required to find the target (potentially) Worst case scenario – the target is at the end of the list Difficult to predict how long a search will take – it depends on the position of the target in the list

30 Sometimes referred to as Binary Chop
Binary search Sometimes referred to as Binary Chop It has one important prerequisite: The list MUST be sorted The binary search will fail or report items as not found if the list is not sorted.

31 Binary Method: Before starting: Then… Find the median
Sort the list Get target number Then… Find the median If the median = target then found If target > median then go to step 1 with the RIGHT sub list If target < median then go to step 1 with the LEFT sub list Repeat until found or list = 1 and not found.

32 Just to make things awkward:
Caveat Just to make things awkward: If the list is an odd number, finding the median is easy. If it is EVEN, then you must use the number to the RIGHT of the median line. E.g. in the list 1..10, 6 will be the median

33 Finally… The big one… Using the book Or the internet
Or Craig N Dave’s youtube channel… Find pseudo code for each algorithm in this lesson Add it, with comments to your notes.

34 Review/Success Criteria
You should know: The


Download ppt "Lesson Objectives Aims Understand the following “standard algorithms”:"

Similar presentations


Ads by Google