Beruflich Dokumente
Kultur Dokumente
2 Comparing Algorithms
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
Computational Complexity
This depends on the space complexity and the time complexity Time complexity indicates how fast it runs Space complexity indicates how much memory space is needed 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 space complexity See the presentation on examples of problems with different time complexities
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 worstcase complexity of the most efficient algorithm which solves the problem.
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)
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)
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
Bubble sort
Video graphical demonstration and analysis of its time complexity. It works by going through the list swapping items with the items next to them if they are the wrong way round. It will need to go through the list multiple times to sort it. If there are n items in the list then the algorithm will make n-1 passes through the list at most; on each pass it will make n-1 comparisons giving a maximum of (n-1)(n-1) comparisons which gives a time complexity of O(n2). This means that as the list size grows bigger the Bubble Sort becomes increasingly inefficient. The space complexity of the Bubble Sort algorithm is O(1) it only ever needs one additional memory location (to store one of the two values being swapped) no matter how many items are in the list. Find out more
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(n log n). In practice, Quick Sort is faster than other O(n log 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
Merge sort
YouTube - explains and demonstrates graphically Merge Sort is a fast sorting algorithm. Time complexity is O(n log n). Space complexity is O(n) which is not bad but does make it one of the least space efficient of the sorting algorithms. Merge Sort works by dividing a list in half, sorting the two sublists and then merging them together. This process is applied to each of the sublists, the sublists of the sublists, and so on. Find out more
Class work
1. Study the slides in this presentation. 2. Study the presentation on examples of problems with different time complexities. 3. Start the sorting algorithm animations and watch 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.
Homework
Answer the past paper questions: 1. Specimen paper COMP 3 Q1 2. June 2010 COMP 3 Q5 Hand-in Friday 16th March 2012
(1 mark)
(4 marks)
(c) Describe a bubble sort and explain why it is O(n). (4 marks)