Presentation is loading. Please wait.

Presentation is loading. Please wait.

Advanced Higher Computing Science Standard Algorithms

Similar presentations


Presentation on theme: "Advanced Higher Computing Science Standard Algorithms"— Presentation transcript:

1 Advanced Higher Computing Science Standard Algorithms

2 This session will cover the standard programming algorithms for Advanced Higher, and look at a number of relevant past paper questions. This session will cover the standard programming algorithms for Advanced Higher, and look at a number of relevant past paper questions.

3 Searching: linear versus binary search Standard sorting algorithms:
Standard Algorithms Learning content Searching: linear versus binary search Standard sorting algorithms: Selection sort using two lists Bubble sort Insertion sort Evaluating sorting algorithms Other sorting algorithms: quicksort Past paper questions Project: Must include a minimum of two from: standard algorithms (binary search, sorts, recursion) data structures (linked list, queues, stacks) data processing (SQL) web development (HTML forms, scripting)

4 A linear search is a "Brute Force" search:
Searching A linear search is a "Brute Force" search: REPEAT <Check every item in an array in turn> UNTIL <the target is found> OR <the end of the array is reached> You will remember this algorithm from your Higher course Brute force meaning that everything is checked until a solution is found. It is systematic, but no strategy is used other than rejecting failures

5 Linear Search PROCEDURE linearSearch (ARRAY OF INTEGER numbers, INTEGER itemToFind ) DECLARE found INITIALLY false DECLARE counter INITIALLY 0 REPEAT SET found TO numbers[counter] = itemToFind SET counter TO counter + 1 UNTIL counter > length( numbers) OR found = true IF found THEN SEND itemToFind & " found at position" & (counter - 1) TO DISPLAY ELSE SEND "Item not found" TO DISPLAY END IF END PROCEDURE Uses a Boolean variable to terminate a conditional loop and a counter to keep track of the index position of the item searched for. Why do we need to use counter-1 in the IF statement?

6 The binary search is a "divide and conquer" algorithm
Only works on a sorted list REPEAT <Find the middle item in the list and compare it with the item to be searched for.> <Change the top and bottom pointers of your list to define a smaller list containing the target> UNTIL <Target is found> OR <new list is empty> Most real-life lists to be searched are sorted in some way. This is a divide and conquer algorithm Real life examples like finding a book on library shelves use additional heuristics such as a knowledge of the frequency of letters at the beginning of authors names and the range of possibiiltiies. You would probably start looking for Roger Zelazny at the end of the shelf

7 Binary Search Declare and initialise the variables
PROCEDURE binarySearch (ARRAY OF INTEGER myArray) DECLARE found AS BOOLEAN INITIALLY false DECLARE startPos AS INTEGER INITIALLY 0 DECLARE endPos AS INTEGER INITIALLY length (myArray) DECLARE middle AS INTEGER INITIALLY 0 DECLARE searchKey AS INTEGER INITIALLY 0 Declare and initialise the variables Many programming languages require variables to be initialised before use. Always a good idea for any programming task because it makes you think about your data structures.

8 Binary Search RECEIVE searchKey FROM KEYBOARD REPEAT SET middle TO (startPos + endPos) / 2 # integer division IF array[middle] < searchKey THEN SET startPos TO middle + 1 ELSE SET endPos TO middle -1 END IF IF array[middle] = searchKey THEN SET found TO true SEND "Found at position "& middle TO DISPLAY UNTIL found = true OR ( startPos > endPos ) Why do we want integer division here? Find the middle item in the list and compare it with the item to be searched for. Divide the list into 2 equal parts and identify the part containing target Repeat with new half list

9 Binary Search IF found = false THEN SEND "Not found" TO DISPLAY END IF END PROCEDURE If the sub list is empty

10 Recursive Binary Search
PROCEDURE binarySearch(ARRAY OF INTEGER myArray, INTEGER searchKey) IF <Target is found or list is empty> THEN <display result> ELSE SET newList TO split (myArray,searchKey) BinarySearch(newList,searchKey) END IF END PROCEDURE FUNCTION split(myArraY, searchKey)RETURNS ARRAY OF INTEGER <Find the middle item in the array and compare it with the searchkey> <Change the top and bottom pointers of the array to contain the target creating newArray> RETURN newArrray END FUNCTION Since a binary search is effectively applying the same process repeatedly on an ordered list until either the item is found or the section of the list being searched is empty, it lends itself to a recursive solution. This algorithm uses a split function which returns an array given an input array and the searchkey Stopping condition: If list is empty or is a list with the target in it and nothing else then stop Otherwise split the list into two and binary search the list containing the target As written this algorithm will only detect the presence or otherwise of the searchkey, not its position

11 Binary Search The binary search is very efficient, particularly for large lists. Doubling the size of a list means only one extra split is required. Number of data items Splits required 2 1 4 8 3 16 32 5 64 6 128 7 256 512 9 1024 10 For n data items the splits required is log2n In other words the number of splits required is the power of two which would be needed to equal more than the number of data items

12 Comparing linear and binary search algorithms
Linear Search Average number of comparisons is N/2 Simple to implement Binary Search Data must be sorted Number of comparisons is Log2N Can be implemented recursively Small lists – linear search is fine Large lists – sort then use binary search bearing in mind the additional processing required to sort the data (trade-off)

13 Selection sort using two lists Bubble sort Insertion sort
Sorting Algorithms If you want to do a binary search, then you have to sort the data first. Selection sort using two lists Bubble sort Insertion sort

14 Evaluating the efficiency of a sorting algorithm
How many comparisons are required on average per number of items to be sorted? How many times items in the list have their positions swapped on average per number of items to be sorted? Does the sort require any additional memory? Does the sort have different results depending on whether it is dealing with a partially sorted list or a random one? Item 4 – how about a reverse sorted list or one with duplicates

15 Selection Sort FOR EACH item FROM listA DO
<find the minimum value in listA and place it in position in listB> <replace the item removed from listA with a dummy value> END FOR EACH The selection sort uses two lists. This algorithm described in this way is easy to understand, because it is similar to how we might sort a small set of items in real life, such as a deck of playing cards, or books from a box into shelves (but without the need for a dummy value). It is inefficient because it has to check every item in listA each time the loop executes, and because it requires a second list with the same storage space as the first. The extra storage space needed can become a serious problem if very large lists of items are being sorted.

16 Selection Sort PROCEDURE SelectionSort(listA, listB) DECLARE dummy AS CHARACTER INITIALLY "x" # if the contraints of listA are known then use an integer as dummy value DECLARE minimumPosition INITIALLY 0 DECLARE listLength INITIALLY length(listA) FOR listBcounter FROM 0 TO listLength DO SET minimumPosition TO listBcounter FOR listAcounter FROM 0 TO listLength DO IF listA[listAcounter]< listA[minimumPosition]THEN SET minimumPosition TO listAcounter END IF END FOR SET listB[listBcounter] TO listA[minimumPosition] SET listA[minimumPosition]TO dummy END PROCEDURE The dummy character could be an integer value if you know the limits on the contents of the list. Some versions of this algorithm have a separate sub procedure to find the smallest item in the list prior to insertion into listB

17 Relatively inefficient Number of comparisons is n2
Selection Sort Relatively inefficient Number of comparisons is n2 Requires two arrays (memory overhead) Uses a dummy variable which may not be the same data type as the items being sorted Because it uses fixed loops, the selection sort using two lists will always have to make n2 comparisons and will make n passes through the list and will move n items from one array to another where n is the number of items in the list to be sorted. There is a version of this algorithm which just shifts items around within the list – much more memory efficient

18 Bubble Sort The bubble-sort is so named, because items in the list appear to "bubble" to the top of the list. The bubble sort swaps items between positions within the list, so does not require additional memory to operate.

19 Bubble Sort PROCEDURE bubblesort(ARRAY OF INTEGER list)
DECLARE listLength INITIALLY length(list) FOR outerLoop FROM listLength -1 TO 0 STEP -1 DO FOR counter FROM 0 TO outerLoop -1 DO IF list[counter] > list[counter+ 1] THEN swap (list[counter], list[counter+1]) END IF END FOR END PROCEDURE Both of the loops in this algorithm are unconditional loops, so the bubble-sort will always complete n passes through the list, and make n2 comparisons. In the worst case scenario, where the list to be sorted is in reverse order, there will be n2 swaps as well.

20 DECLARE temp AS INTEGER INITIALLY a SET a TO b SET b TO temp
Bubble Sort (swap) PROCEDURE swap(a, b) DECLARE temp AS INTEGER INITIALLY a SET a TO b SET b TO temp END PROCEDURE Since a swap only occurs after any comparison if the first item is larger than the second item, the number of swaps depends on how partially sorted the original list is. Note that parameters a and b are reference parameters (they change their value)

21 Bubble Sort The bubble sort can be made more efficient by introducing a counter which keeps track of how many swaps have occurred in a pass and which terminates the search if none have occurred.

22 Bubble Sort with Boolean variable
PROCEDURE bubblesort(ARRAY OF INTEGER list) DECLARE outerLoop INITIALLY length(list) WHILE sorted = false DO SET swaps TO 0 FOR counter FROM 0 TO outerLoop -1 DO IF list[counter] > list[counter+ 1] THEN swap (list[counter], list[counter+1]) SET swaps TO swaps + 1 END IF END FOR IF swaps = 0 THEN SET sorted TO true SET outerLoop TO outerLoop -1 END WHILE END PROCEDURE The outer loop is now a conditional loop, but the inner loop still has to be an unconditional one. Swaps is set to 0 each time before the inner loop runs

23 Relatively inefficient (large number of swaps)
Bubble Sort Relatively inefficient (large number of swaps) In worst case, number of comparisons is n2 Can be adapted to stop when the list is sorted Negligible memory overheads

24 Insertion Sort PROCEDURE insertionSort ( ARRAY OF INTEGER list) DECLARE listLength INITIALLY length (list) DECLARE current AS INTEGER INITIALLY 0 DECLARE position AS INTEGER INITIALLY 0 FOR index FROM 1 TO listLength -1 DO SET current TO list[index] SET position TO index WHILE (position >0) AND (list[position -1] > current) DO SET list [position] TO list[position -1] SET position TO position -1 END WHILE SET list[position] TO current END FOR END PROCEDURE The insertion sort effectively divides the list into two sections – sorted and unsorted. The first item is the sorted part with one member. It then progressively takes each element in turn and inserts it into the sorted list.

25 Other sorting algorithms
As well as evaluating sorting algorithms according to their memory requirements and processor load, they may have a different performance depending upon: the size of the list being sorted; if the list is partially sorted or not; if the list to be sorted is already sorted in reverse order; if the list contains large numbers of duplicate items. Many sort algorithm evaluations will give best and worst case scenarios depending on these conditions.

26 Other sorting algorithms: Quicksort
PROCEDURE quickSort(ARRAY OF INTEGER list , INTEGER i, INTEGER listLength) DECLARE pivotPosition AS INTEGER INITIALLY 0 IF i < listLength THEN SET pivotPosition TO partition(list, i, listLength) quickSort(list, i, pivotPositon - 1) quickSort(list, pivotPositon + 1, listLength) END IF END PROCEDURE FUNCTION partition(ARRAY list, INTEGER i, INTEGER listLength)RETURNS INTEGER <swaps the items in the list using i as the pivot and returns a new pivot position> END FUNCTION Another divide an conquer recursive algorithm. The algorithm was originally designed to sort words into alphabetical order so that they could be compared to a foreign language dictionary for translation. For this reason it is not very efficient when sorting lists which contain lots of identical items, as that would be an unlikely scenario in these circumstances. Because it is a recursive algorithm, it is particularly suited to parallel processing, but is very memory intensive and becomes inefficient if the list to be sorted does not fit into available RAM.

27 Other sorting algorithms: Merge sort
The merge sort is another recursive sort algorithm, It divides the list into sub lists which are sorted and then merged together to make a larger sorted list. It is particularly useful if you already have two sorted lists which have to be combined together.

28 2016 Q4c Specimen Paper Q3c Old Advanced Higher 2015 Q3
Past paper questions 2016 Q4c Specimen Paper Q3c Old Advanced Higher 2015 Q3 Old Advanced Higher 2014 Q3 Old Advanced Higher 2012 Q5 Old Advanced Higher 2011 Q4

29 2016 Q4c Dawid decides to add a new module to his program. This module sorts the data in the array of records into ascending order of registration class. Part of Dawid’s code is shown. Line 1 and Line 5 of the code are incomplete. Provide the missing details by rewriting both lines of code. Bubble Sort IF pupilData[counter-1] > pupilData[counter] THEN

30 2016 Q4c Dawid’s school has 2000 pupils. Explain why it may be more appropriate to use a quick sort rather than the sort algorithm used in part (c) above. Quicksort takes less time than bubble sort to sort large data sets since fewer comparisons are needed. The quicksort uses fewer comparisons because it divides the list into two sub-lists then recursively quicksorts each sub-list A bubble sort will need n2 comparisons A quicksort will need nlog2(n)

31 Specimen Paper Q3c A game includes a high score table with the names and scores of the top five players stored in two 1-D arrays. Meena 58 Joe 50 Patrick 27 Marta 25 Andrew 23 After a number of games, the 1-D array that holds the scores is shown below: Index 1 2 3 4 Scores 23 25 27 50 58 Senga plays the game and scores 48. Her score of 48 and her name now replaces the lowest score and name that were in position 0. The 1-D array for the scores is now: Index 1 2 3 4 Scores 48 25 27 50 58

32 Specimen Paper Q3c Senga plays the game and scores 48. Her score of 48 and her name now replaces the lowest score and name that were in position 0. The 1-D array for the scores is now: Index 1 2 3 4 Scores 48 25 27 50 58 A bubble sort is used to sort the scores. After the first pass, the list will be sorted: Index 1 2 3 4 Scores 25 27 48 50 58 State the two exchanges that took place in this pass. (ii) Explain why the bubble sort will make another pass through the list, even though it is sorted.

33 Specimen Paper Q3c A bubble sort is used to sort the scores. After the first pass, the list will be sorted. State the two exchanges that took place in this pass. index 1 2 3 4 48 25 27 50 58

34 Specimen Paper Q3c (i) [0]48 and [1]25
A bubble sort is used to sort the scores. After the first pass, the list will be sorted. State the two exchanges that took place in this pass. index 1 2 3 4 48 25 27 50 58 (i) [0]48 and [1]25

35 (ii) swaps = false will not be true until a 2nd pass has occurred
Specimen Paper Q3c A bubble sort is used to sort the scores. After the first pass, the list will be sorted. State the two exchanges that took place in this pass. index 1 2 3 4 48 25 27 50 58 (i) [0]48 and [1] [1]48 and [2]27 (ii) swaps = false will not be true until a 2nd pass has occurred

36 Old Higher 2015 Q3 Visitors to the 2015 Edinburgh comic convention have taken part in a gaming competition in the hope of qualifying for the event in New York. In order to qualify, competitors have to score at least 1000 points during a 20-second game play. The event has been very popular with 120,000 people taking part during the convention. The table below shows a sample of some of the competitors’ results Competitor Number Competitor Name Score Qualify(Y/N) 257 Dino 876 N 1200 Jeanie 1001 Y 12034 Lewis 999 15314 Morven 534 72912 Richard 1287 110912 986 113452 Will 952

37 Old Advanced Higher 2015 Q3a (a) The data could be stored as a single 2-D array or a set of four 1-D arrays. State two advantages of using a set of four 1-D arrays rather than a single 2-D array. You can choose a suitable data type for each 1-D array It is more efficient to store the data in a suitable data type rather than storing all of them as strings Competitor Number Competitor Name Score Qualify(Y/N) 257 Dino 876 N 1200 Jeanie 1001 Y 12034 Lewis 999 15314 Morven 534 72912 Richard 1287 110912 986 113452 Will 952 If you used a single 2D array then you would need to store every field as a string. How else could you store this data? You could of course store the data as an a 1D array of records.

38 Old Advanced Higher 2015 Q3b(i)
A binary search could be used to retrieve information about competitors. Explain why Competitor Name is unsuitable for a binary search of the data as currently stored. Competitor Name is not a unique field. If the array contained duplicate values then it may not fine the correct competitor

39 Old Advanced Higher 2015 Q3b(ii)
State one reason why Score is unsuitable for a binary search of the data as currently stored. The data is not sorted

40 Old Advanced Higher 2015 Q3c (i) Write a binary search algorithm, using detailed pseudocode, which asks for a competitor’s number and then finds the position of that competitor number in the 120,000 competitors. (ii) Describe the changes that will need to be made to the algorithm if the Competitor Number is changed to descending order. Competitor number field is sorted of course

41 Old Advanced Higher 2015 Q3c RECEIVE searchKey FROM KEYBOARD SET startPos TO 0 SET endPos TO REPEAT SET middle TO (startPos + endPos) / 2 IF array[middle] > searchKey THEN SET startPos TO middle + 1 ELSE SET endPos TO middle -1 END IF IF array[middle] = searchKey THEN SET found TO true SEND "Found at position "& middle TO DISPLAY UNTIL found = true OR ( startPos > endPos ) I've missed declaring variables – not needed in an exam when you are just asked for an algorithm

42 Old Advanced Higher 2014 Q3(a)
A table of results from a horse race is provided which included a total column. The selection sort using two lists is used to sort the list in ascending order of “total”. Describe how a selection sort using two lists works. Horse name Entry number Time for round (seconds) Time penalty Total Anna Millie 17 73·2 4·0 77·2 Foster Giant 18 84·1 7·5 91·6 Grainger O’Malley 19 74·0

43 Old Advanced Higher 2014 Q3(a)
FOR EACH item FROM listA DO <find the minimum value in listA and place it in position in listB> <replace the item removed from listA with a dummy value> END FOR EACH

44 Old Advanced Higher 2014 Q3(b)
(b) As each horse completes its round, its record will be added to the bottom of an already sorted list and then resorted. Explain how the fact that the list is already sorted, except for the last record, will affect the efficiency of the selection sort algorithm. The fact that the list is already partially sorted does not affect the selection sort in any way, as each pass requires the minimum value to be found.

45 Old Advanced Higher 2014 Q3(c)
A bubble sort algorithm could have been used on the list. Compare it with the selection sort in terms of memory usage and number of comparisons. The bubble sort algorithm can include a Boolean variable to terminate if no swaps occur. It does not need an additional list so uses half as much memory as the selection sort

46 Old Advanced Higher 2012 Q5b An exam has eight questions each worth 20 marks. An example of a candidate’s eight marks for the eight questions is shown below. A candidate’s overall score for the exam is calculated by totalling their best five questions. The candidate’s best five questions could be identified by sorting the list into descending order. The best five questions would then be in the first five positions in the list.

47 Old Advanced Higher 2012 Q5b Initial scores: A programmer has been asked to write a module to calculate the overall total from a set of exam marks. After the first pass using the bubble sort the list is: Write down the list after the next pass.

48 Old Advanced Higher 2012 Q5 (b) (i) Original After 1st pass 15 18 16 12 19 14 5 After 2nd pass

49 Old Advanced Higher 2012 Q5b (ii) State the maximum number of passes required by the bubble sort to place the best five question totals in the first five positions. Explain your answer. Three passes will be required because the worst three marks will be placed at the end in the sorted part of the list. We only need the best 5 marks, not a fully sorted list

50 Old Advanced Higher 2012 Q5c (c) A candidate scores these marks: (i) State the number of passes required by the bubble sort for this list. (ii) Explain the feature of the bubble sort that makes it more efficient for this list.

51 Old Advanced Higher 2012 Q5c (c)(i) The bubble sort will only require 1 pass because the list is already sorted (c)(ii) The bubble sort uses a boolean variable to terminate the sort if no swaps occur

52 Old Advanced Higher 2011 Q4 Senga is a computer science student investigating the efficiency of the binary search algorithm. She decides to examine its performance on a list of seven characters held in a 1-D array. The array is shown below: The character I is found on the first pass through the loop. On the second pass the character D is one of two characters that could be found. Senga decides to represent the performance of the algorithm using a binary decision tree. Index 1 2 3 4 5 6 Char C D E I L M T

53 Old Advanced Higher 2011 Q4 SET lower TO # lowest index SET upper TO # highest index REPEAT SET middle TO (lower+upper) div 2 IF search_value > list[middle] THEN SET lower TO middle+1 ELSE SET upper TO middle–1 END IF UNTIL list[middle] = search_value OR lower > upper IF search_value = list[middle] THEN SEND "Search item was found at" & middle TO DISPLAY SEND "Search item is not in list" TO DISPLAY

54 Old Advanced Higher 2011 Q4 Copy and complete the diagram for all of the characters.

55 Old Advanced Higher 2011 Q4 SET lower TO # lowest index SET upper TO # highest index REPEAT SET middle TO (lower+upper) div 2 IF search_value > list[middle] THEN SET lower TO middle+1 ELSE SET upper TO middle–1 END IF UNTIL list[middle] = search_value OR lower > upper IF search_value = list[middle] THEN SEND "Search item was found at" & middle TO DISPLAY SEND "Search item is not in list" TO DISPLAY

56 Old Advanced Higher 2011 Q4 SET lower TO # lowest index SET upper TO # highest index REPEAT SET middle TO (lower+upper) div 2 IF search_value > list[middle] THEN SET lower TO middle+1 ELSE SET upper TO middle–1 END IF UNTIL list[middle] = search_value OR lower > upper IF search_value = list[middle] THEN SEND "Search item was found at" & middle TO DISPLAY SEND "Search item is not in list" TO DISPLAY

57 Old Advanced Higher 2011 Q4 SET lower TO # lowest index SET upper TO # highest index REPEAT SET middle TO (lower+upper) div 2 IF search_value > list[middle] THEN SET lower TO middle+1 ELSE SET upper TO middle–1 END IF UNTIL list[middle] = search_value OR lower > upper IF search_value = list[middle] THEN SEND "Search item was found at" & middle TO DISPLAY SEND "Search item is not in list" TO DISPLAY

58 Old Advanced Higher 2011 Q4 SET lower TO # lowest index SET upper TO # highest index REPEAT SET middle TO (lower+upper) div 2 IF search_value > list[middle] THEN SET lower TO middle+1 ELSE SET upper TO middle–1 END IF UNTIL list[middle] = search_value OR lower > upper IF search_value = list[middle] THEN SEND "Search item was found at" & middle TO DISPLAY SEND "Search item is not in list" TO DISPLAY

59 Old Advanced Higher 2011 Q4 SET lower TO # lowest index SET upper TO # highest index REPEAT SET middle TO (lower+upper) div 2 IF search_value > list[middle] THEN SET lower TO middle+1 ELSE SET upper TO middle–1 END IF UNTIL list[middle] = search_value OR lower > upper IF search_value = list[middle] THEN SEND "Search item was found at" & middle TO DISPLAY SEND "Search item is not in list" TO DISPLAY

60 Old Advanced Higher 2011 Q4 SET lower TO # lowest index SET upper TO # highest index REPEAT SET middle TO (lower+upper) div 2 IF search_value > list[middle] THEN SET lower TO middle+1 ELSE SET upper TO middle–1 END IF UNTIL list[middle] = search_value OR lower > upper IF search_value = list[middle] THEN SEND "Search item was found at" & middle TO DISPLAY SEND "Search item is not in list" TO DISPLAY

61 Thank you Please complete the online evaluation form


Download ppt "Advanced Higher Computing Science Standard Algorithms"

Similar presentations


Ads by Google