1.2 Comparing Algorithms2

  • Upload
    iducduy

  • View
    215

  • Download
    0

Embed Size (px)

Citation preview

  • 8/12/2019 1.2 Comparing Algorithms2

    1/15

    1.2 Comparing Algorithms

  • 8/12/2019 1.2 Comparing Algorithms2

    2/15

    What is an algorithm?

    A set of unambiguous instructions for solving a

    problem

    Algorithms are used to transform inputs into

    outputs

    Computer programs would not exist without them

    There are several ways to write an algorithm to

    solve a problem e.g. bubble sort & insertion sort

    Must produce correct output for all valid inputs

    Amount of memory used and speed of computation

    are important

  • 8/12/2019 1.2 Comparing Algorithms2

    3/15

    Computational Complexity

    This depends on the space complexity and the timecomplexity

    Time complexity indicates how fast it runs

    Space complexity indicates how much memory space isneeded

    The number of items to be searched or sorted impacts on

    how long it takes to carry out the operation

    Time complexity can be improved at the expense of spacecomplexity

    See the presentationon examples of problems with

    different time complexities

    http://www.school-portal.co.uk/GroupDownloadFile.asp?GroupId=1010236&ResourceID=4106083http://www.school-portal.co.uk/GroupDownloadFile.asp?GroupId=1010236&ResourceID=4106083
  • 8/12/2019 1.2 Comparing Algorithms2

    4/15

    Complexity of a problem

    Worst-case complexity

    e.g. search for a value where every number in a list is

    examined

    Best-case complexity e.g. search for a value where only one comparison is

    required

    Average-case complexity

    The complexity of a problem is taken to be the worst-

    case complexity of the most efficient algorithm

    which solves the problem.

  • 8/12/2019 1.2 Comparing Algorithms2

    5/15

    Units for measuring time

    Time in seconds is not used because there are

    too many factors that can vary e.g. computer

    speed

    Time efficiency is based on counting the

    number of times the algorithms basic

    operation is executed on inputs of size n

  • 8/12/2019 1.2 Comparing Algorithms2

    6/15

    Order of growth

    Assesses by what factor execution time increases

    when the size of the input is increased.

    E.g. if the input size (n) doubles and the algorithm

    takes four times longer to execute, the order of

    growth is quadratic. Therefore, the algorithm is

    O(n)

  • 8/12/2019 1.2 Comparing Algorithms2

    7/15

    Big O notation

    Allows us to talk about the complexity of the

    algorithm irrespective of the machine that the

    algorithm is implemented on

    YouTube - explains and demonstrates the main

    asymptotic bounds associated with measuring

    algorithm performance

    O(g) is called big O of g it represents the class

    of functions that grow no faster than g

    e.g. O(n)

    http://www.youtube.com/watch?v=6Ol2JbwoJp0http://www.youtube.com/watch?v=6Ol2JbwoJp0
  • 8/12/2019 1.2 Comparing Algorithms2

    8/15

    Travelling salesman problem

    Given a list of cities and their pair wise

    distances, the task is to find the least possibledistance that visits each city exactly once

    No general method of solution is known, and

    the problem is non-deterministic polynomial-

    time hard.

    Travelling salesman problem wiki

    TSP website including game

    http://en.wikipedia.org/wiki/Travelling_salesman_problemhttp://en.wikipedia.org/wiki/Travelling_salesman_problemhttp://www.tsp.gatech.edu/index.htmlhttp://www.tsp.gatech.edu/index.htmlhttp://en.wikipedia.org/wiki/Travelling_salesman_problemhttp://en.wikipedia.org/wiki/Travelling_salesman_problem
  • 8/12/2019 1.2 Comparing Algorithms2

    9/15

    Sorting algorithms

    Many different methods that can be used to sort an

    unordered list

    As the size of the list increases the efficiency of an

    algorithm becomes critical

    When choosing an algorithm consider both the

    time efficiency and the space efficiency.

    Animations about the different sorting algorithms

    Detailed look at some sorting algorithms

    http://www.sorting-algorithms.com/http://www.sorting-algorithms.com/http://localhost/var/www/apps/conversion/tmp/scratch_4/Case_study_Comparing_algorithms.dochttp://www.iti.fh-flensburg.de/lang/algorithmen/sortieren/algoen.htmhttp://localhost/var/www/apps/conversion/tmp/scratch_4/Case_study_Comparing_algorithms.dochttp://www.iti.fh-flensburg.de/lang/algorithmen/sortieren/algoen.htmhttp://localhost/var/www/apps/conversion/tmp/scratch_4/Case_study_Comparing_algorithms.dochttp://www.sorting-algorithms.com/http://www.sorting-algorithms.com/
  • 8/12/2019 1.2 Comparing Algorithms2

    10/15

    Bubble sort

    Videographical demonstration and analysis of its timecomplexity.

    It works by going through the list swapping items with the itemsnext to them if they are the wrong way round. It will need to gothrough the list multiple times to sort it.

    If there aren

    items in the list then the algorithm will maken

    -1passes through the list at most; on each pass it will make n-1comparisonsgiving a maximum of (n-1)(n-1) comparisonswhich gives a time complexity of O(n2). This means that as thelist size grows bigger the Bubble Sort becomes increasinglyinefficient.

    The space complexity of the Bubble Sort algorithm is O(1)itonly ever needs one additional memory location (to store one ofthe two values being swapped) no matter how many items are inthe list.

    Find out more

    http://www.school-portal.co.uk/GroupDownloadFile.asp?GroupId=1010236&ResourceID=4488273http://www.theteacher99.btinternet.co.uk/theteacher/newalevel/cp1_4_1.htmhttp://www.theteacher99.btinternet.co.uk/theteacher/newalevel/cp1_4_1.htmhttp://www.school-portal.co.uk/GroupDownloadFile.asp?GroupId=1010236&ResourceID=4488273
  • 8/12/2019 1.2 Comparing Algorithms2

    11/15

    Quick sort YouTube - explains and demonstrates graphically

    There are many versions of the Quick Sort algorithm.

    In fact, it is one of the most frequently used algorithms for

    sorting.

    The time complexity of Quick Sort is O(n2), though under most

    circumstances it is O(nlog n).

    In practice, Quick Sort is faster than other O(nlog n) sorting

    algorithms.

    The space complexity is O(log n), which is considered to be

    good (though it is less space efficient than Bubble Sort).

    Quick Sort works by picking an item in the list as a pivot and

    placing all items in the list on the correct side of the pivot. For

    each of the two sublists a pivot is chosen and the process

    continues in this manner until the list is sorted.

    Find out more

    http://www.youtube.com/watch?v=y_G9BkAm6B8http://www.theteacher99.btinternet.co.uk/theteacher/newalevel/cp4_4_3.htmhttp://www.theteacher99.btinternet.co.uk/theteacher/newalevel/cp4_4_3.htmhttp://www.youtube.com/watch?v=y_G9BkAm6B8
  • 8/12/2019 1.2 Comparing Algorithms2

    12/15

    Merge sort

    YouTube - explains and demonstrates graphically Merge Sort is a fast sorting algorithm. Time complexity is O(nlog n). Space complexity is O(n)which is not bad but

    does make it one of the least space efficient ofthe sorting algorithms.

    Merge Sort works by dividing a list in half, sortingthe two sublists and then merging them together.

    This process is applied to each of the sublists, thesublists of the sublists, and so on. Find out more

    http://www.youtube.com/watch?v=GCae1WNvnZMhttp://www.cse.iitk.ac.in/users/dsrkg/cs210/applets/sortingII/mergeSort/mergeSort.htmlhttp://www.cse.iitk.ac.in/users/dsrkg/cs210/applets/sortingII/mergeSort/mergeSort.htmlhttp://www.youtube.com/watch?v=GCae1WNvnZM
  • 8/12/2019 1.2 Comparing Algorithms2

    13/15

    Class work

    1. Study the slides in this presentation.

    2. Study the presentationon examples ofproblems with different time complexities.

    3. Start the sorting algorithm animationsandwatch how each one operates.

    4. Watch the bubble sort demo video including

    analysis of its time complexity.5. Answer questions 1-11 in the A2 Computing

    book pages 21-24.

    http://www.school-portal.co.uk/GroupDownloadFile.asp?GroupId=1010236&ResourceID=4106083http://www.sorting-algorithms.com/http://www.school-portal.co.uk/GroupDownloadFile.asp?GroupId=1010236&ResourceID=4488273http://www.school-portal.co.uk/GroupDownloadFile.asp?GroupId=1010236&ResourceID=4488273http://www.sorting-algorithms.com/http://www.school-portal.co.uk/GroupDownloadFile.asp?GroupId=1010236&ResourceID=4106083
  • 8/12/2019 1.2 Comparing Algorithms2

    14/15

    Homework

    Answer the past paper questions:

    1. Specimen paper COMP 3 Q12. June 2010 COMP 3 Q5

    Hand-in Friday 16thMarch 2012

  • 8/12/2019 1.2 Comparing Algorithms2

    15/15

    AQA, Specimen paper question

    1 The big O notation is often used to describe the efficiency of an algorithm.

    (a) Place the following algorithms in order of efficiency, the most efficient first.

    Algorithm A that is O(n)

    Algorithm B that is O(a)

    Algorithm C that is O(n) (1 mark)

    (b) Describe a linear search and explain why it is O(n).

    (4 marks)

    (c) Describe a bubble sort and explain why it is O(n).

    (4 marks)