You are on page 1of 4

D1/1 ALGORITHMS

1.3. Sorting algorithms 1.4. Searching algorithms

Specification Be able to develop and adapt simple algorithms (Sorting: Bubble, Shuttle, insertion, quick sort. Candidates not expected to memorise these) (A2) Learning objectives Be thoroughly familiar with the sorting algorithms Be thoroughly familiar with the searching algorithms (although not explicitly in specification) Ch.1 p.15-24 Ex.1E Q10,11,12,13,15,16 Multiple Choice Test 3

1.3. Sorting algorithms The aim here is to teach a computer how to sort a list of data, e.g. numbers. The efficiency of the algorithm is very important. (a) Interchange sort algorithm 1. Swap the smallest number with the first number. 2. Find the smallest number excluding the first. Swap this with the second number. 3. Repeat this until the list is sorted. A measure of the efficiency of a sorting algorithm is to count the number of comparisons and swaps needed to sort the data. Use Allsorts to count comparisons and swaps for: • a random list of size 6; • a random list of size 8; • a list in reverse order. What do we notice? • The number of comparisons is independent of the list chosen; •
• 1 n ( n − 1) ; 2 The number of swaps is dependent on the original list: if it is in reverse order, n – 1 swaps are required (one per pass) which is the worst case. So the maximum number of 1 comparisons and swaps is n ( n − 1) + ( n − 1) , and the algorithm therefore has 2 quadratic complexity.

The no. of comparisons for a list of size n is the (n – 1)th triangle number:

(b) Bubble sort algorithm This depends on successive comparisons of pairs of numbers. 1. Compare the 1st & 2nd numbers and swap if necessary (putting the smaller on top). 2. Compare the 2nd & 3rd numbers and swap if necessary. 3. Complete the first pass (the largest number will now be at the bottom). 4. Repeat the steps above, but exclude the last number. 5. Repeat until there are no swaps in a given pass. Use Allsorts (Bubble Down) to count comparisons and swaps for: • a random list of size 6; a list in the correct order; • • a list in reverse order.

What do we notice? • This time the number of comparisons depends on the list; • The worst case is a list in reverse order; 1 • The no. of comparisons in this case is again n ( n − 1) ; 2 1 The no. of swaps in this case is n ( n − 1) • 2 This algorithm therefore also has quadratic complexity. Also we could use this MathsNet demonstration. A disadvantage of bubble sort is that once the data has been sorted, another complete pass through the data is required to ensure that the sorting has been finished. This is partly overcome by: (c) Shuttle sort algorithm 1. Compare the first two numbers and swap if necessary to place in ascending order. 2. Compare the 2nd & 3rd numbers and swap if necessary, then compare 1st & 2nd and swap if necessary (OCR specification: IF 2nd & 3rd swapped) 3. Repeat including the 3rd & 4th numbers, etc. The Allsorts spreadsheet uses the OCR definition. Use it to count comparisons and swaps for: • a random list of size 6; • a list in the correct order; • a list in reverse order. What do we notice? • This time the number of comparisons depends on the list; • The worst case is a list in reverse order; 1 • The no. of comparisons in this case is again n ( n − 1) ; 2 1 • The no. of swaps in this case is n ( n − 1) 2 This algorithm therefore also has quadratic complexity. (d) Insertion sort algorithm This is what you would do to sort a hand of cards. 1. Write down the first number on the list. 2. Write the next number before the first if it is lower, or after it if it is greater. 3. Take the next number and compare it with the first in your list; write before it if it is less. If not compare to the next number, etc. 4. Repeat until all the numbers in the original list have been placed. Use the Allsorts spreadsheet to sort: • a random list of size 6; • a list in the correct order; • a list in reverse order. What do we notice?

The best case here is when the original list is in reverse order, because at each stage we only need to compare the new number with the first on the new list; • The worst case is when the original list is in the correct order! 1 • The no. of comparisons in this case is again n ( n − 1) ; 2 There are no swaps. • Again, the algorithm has quadratic complexity. (e) Quick sort algorithm 1. Split the list into two sublists, one containing numbers less than or equal to the first in the list, the other containing those greater. 2. Repeat the above on sublists containing two or more numbers until no more sublists remain. Use the Allsorts spreadsheet to sort: • a random list of size 6; • a random list of size 8; • a list in the correct order; a list in reverse order. • What do we notice? • Lists in reverse or correct orders are the worst cases, because they generate the most sublists; 1 • The no. of comparisons in this case is again n ( n − 1) ; 2 • There are no swaps. Again, the algorithm has quadratic complexity. Also we could use this MathsNet demonstration. 1.4. Searching algorithms This is not explicitly in the specification: just read through p.22-24, noting the three methods (linear search, indexed sequential search, binary search). There is a demonstration of the binary search algorithm on MathsNet. Resources • The Allsorts spreadsheet is excellent • “Bubble Sort Example” PowerPoint illustrates this algorithm • “QuickSort” PowerPoint illustrates this algorithm • “Binary Search” PowerPoint illustrates this algorithm

ALGORITHMS 1.3. Sorting algorithms
Download the Allsorts spreadsheet from the MEI Distance Learning website. (a) 1. 2. 3. Interchange sort algorithm Swap the smallest number with the first number. Find the smallest number excluding the first. Swap this with the second number. Repeat this until the list is sorted.

(b) Bubble sort algorithm This depends on successive comparisons of pairs of numbers. 1. Compare the 1st & 2nd numbers and swap if necessary (putting the smaller on top). 2. Compare the 2nd & 3rd numbers and swap if necessary. 3. Complete the first pass (the largest number will now be at the bottom). 4. Repeat the steps above, but exclude the last number. 5. Repeat until there are no swaps in a given pass. http://www.mathsnet.net/asa2/2004/d11bubble.html (c) Shuttle sort algorithm 1. Compare the first two numbers and swap if necessary to place in ascending order. 2. Compare the 2nd & 3rd numbers and swap if necessary, then compare 1st & 2nd and swap if necessary (OCR specification: IF 2nd & 3rd swapped) 3. Repeat including the 3rd & 4th numbers, etc. (The Allsorts spreadsheet here uses the OCR definition.) (d) Insertion sort algorithm This is what you would do to sort a hand of cards. 1. Write down the first number on the list. 2. Write the next number before the first if it is lower, or after it if it is greater. 3. Take the next number and compare it with the first in your list; write before it if it is less. If not compare to the next number, etc. 4. Repeat until all the numbers in the original list have been placed. (e) Quick sort algorithm 1. Split the list into two sublists, one containing numbers less than or equal to the first in the list, the other containing those greater. 2. Repeat the above on sublists containing two or more numbers until no more sublists remain. http://www.mathsnet.net/asa2/2004/d11quicksort.html

Use the five sorting algorithms and the Allsorts spreadsheet to sort: • a random list of size 6; • a random list of size 8; • a list in the correct order; • a list in reverse order. For each algorithm, try to find which lists produce the best and worst cases.