Lesson Objectives Aims Understand the following “standard algorithms”:

Slides:



Advertisements
Similar presentations
Visual C++ Programming: Concepts and Projects
Advertisements

HST 952 Computing for Biomedical Scientists Lecture 9.
SEARCHING, SORTING, TOPOLOGICAL SORTS Most real world computer applications deal with vast amounts of data. Searching for a particular data item can take.
Sorting Chapter Sorting Consider list x 1, x 2, x 3, … x n We seek to arrange the elements of the list in order –Ascending or descending Some O(n.
Binary Search Introduction to Trees. Binary searching & introduction to trees 2 CMPS 12B, UC Santa Cruz Last time: recursion In the last lecture, we learned.
CS 206 Introduction to Computer Science II 11 / 05 / 2008 Instructor: Michael Eckmann.
Searching and Sorting Arrays
Algorithms in Computer Science Topics in CS. Terms & Concepts in this Unit  algorithms  Big O Notation  flow-charts  pseudo-code  Search Algorithms.
© Peter Andreae CS4HS Algorithms Searching for an item in a list Sorting a list Searching for a word in text Analysing Networks.
Week 11 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham.
Searching and Sorting Gary Wong.
Computer Science Searching & Sorting.
© M. Gross, ETH Zürich, 2014 Informatik I für D-MAVT (FS 2014) Exercise 12 – Data Structures – Trees Sorting Algorithms.
Sorting Chapter Sorting Consider list x 1, x 2, x 3, … x n We seek to arrange the elements of the list in order –Ascending or descending Some O(n.
Fundamentals of Algorithms MCS - 2 Lecture # 15. Bubble Sort.
Sorting & Searching Review. Selection Sort 1. Find the smallest element 2. Move to the front of the array (swap with front) 3. Repeat Steps 1&2, but ignoring.
ALGORITHMS.
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.
Data Structures - CSCI 102 Selection Sort Keep the list separated into sorted and unsorted sections Start by finding the minimum & put it at the front.
ICS201 Lecture 21 : Sorting King Fahd University of Petroleum & Minerals College of Computer Science & Engineering Information & Computer Science Department.
Computer Science 1620 Sorting. cases exist where we would like our data to be in ascending (descending order) binary searching printing purposes selection.
Sorting Algorithms Written by J.J. Shepherd. Sorting Review For each one of these sorting problems we are assuming ascending order so smallest to largest.
OCR A Level F453: Data structures and data manipulation Data structures and data manipulation a. explain how static data structures may be.
Copyright Prentice Hall Modified by Sana odeh, NYU
Searching and Sorting Searching algorithms with simple arrays
Sort Algorithm.
UNIT - IV SORTING By B.Venkateswarlu Dept of CSE.
Searching and Sorting Algorithms
Lesson Objectives Aims Key Words
Tips and tools for creating and presenting wide format slides
May 17th – Comparison Sorts
CMSC201 Computer Science I for Majors Lecture 23 – Sorting
Lecture 14 Searching and Sorting Richard Gesick.
IGCSE 6 Cambridge Effectiveness of algorithms Computer Science
Sorting Algorithms.
Sorting Chapter 13 presents several common algorithms for sorting an array of integers. Two slow but simple algorithms are Selectionsort and Insertionsort.
Design and Analysis of Algorithms
Teach A level Computing: Algorithms and Data Structures
Data Structures 2018 Quiz Answers
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Print slides for students reference
Sorting Algorithms Written by J.J. Shepherd.
Binary Search Back in the days when phone numbers weren’t stored in cell phones, you might have actually had to look them up in a phonebook. How did you.
Bubble, Selection & Insertion sort
i206: Lecture 14: Heaps, Graphs intro.
Lesson Objectives Aims
Unit-2 Divide and Conquer
Welcome to CIS 068 ! Lesson 9: Sorting CIS 068.
Quadratic Sorting Chapter 12 presents several common algorithms for sorting an array of integers. Two slow but simple algorithms are Selectionsort and.
A* Path Finding Ref: A-star tutorial.
Lecture 11 Searching and Sorting Richard Gesick.
MSIS 655 Advanced Business Applications Programming
Sorting … and Insertion Sort.
IT 4043 Data Structures and Algorithms
Search,Sort,Recursion.
VCE IT Theory Slideshows
Bubble Sort Key Revision Points.
Simple Sorting Methods: Bubble, Selection, Insertion, Shell
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.
Topic 24 sorting and searching arrays
Searching.
Searching and Sorting Arrays
Search,Sort,Recursion.
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Heaps By JJ Shepherd.
Unit 2: Computational Thinking, Algorithms & Programming
Sorting.
Module 8 – Searching & Sorting Algorithms
Presentation transcript:

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

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

Sorting Algorithms

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

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

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.

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

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

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

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

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

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.

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

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

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

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

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

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

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

Searching Algorithms

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

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.

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!

Confused? You should be! Read this: https://www.raywenderlich.com/4946/introduction-to-a-pathfinding And this: http://www.policyalmanac.org/games/aStarTutorial.htm Still stuck? Next slide…

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.

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

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.

Linear Search – Flow chart

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

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.

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.

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 1 2 3 4 5 6 7 8 9 10

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.

Review/Success Criteria You should know: The