advanced higher computing science standard algorithms

52
Advanced Higher Computing Science Standard Algorithms

Upload: shawn-waters

Post on 17-Jan-2018

224 views

Category:

Documents


0 download

DESCRIPTION

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

TRANSCRIPT

Page 1: Advanced Higher Computing Science Standard Algorithms

Advanced Higher Computing Science

Standard Algorithms

Page 2: Advanced Higher Computing Science Standard Algorithms

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>

Page 3: Advanced Higher Computing Science Standard Algorithms

Linear SearchPROCEDURE linearSearch (ARRAY OF INTEGER numbers, INTEGER itemToFind )

DECLARE found INITIALLY falseDECLARE 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

Page 4: Advanced Higher Computing Science Standard Algorithms

Binary Search

• Only works on a sorted listREPEAT

<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 <list is empty>

Page 5: Advanced Higher Computing Science Standard Algorithms

Binary SearchPROCEDURE binarySearch (ARRAY OF INTEGER myArray) DECLARE found AS BOOLEAN INITIALLY falseDECLARE startPos INITIALLY 0DECLARE endPos INITIALLY length (myArray)DECLARE middle INITIALLY 0DECLARE searchKey INITIALLY 0 

Page 6: Advanced Higher Computing Science Standard Algorithms

Binary SearchRECEIVE searchKey FROM KEYBOARDREPEAT 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 END IFUNTIL found = true OR ( startPos > endPos )

Page 7: Advanced Higher Computing Science Standard Algorithms

Binary SearchIF found = false THEN SEND "Not found" TO DISPLAYEND IF END PROCEDURE

Page 8: Advanced Higher Computing Science Standard Algorithms

Recursive Binary SearchPROCEDURE 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 IFEND 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 the array to contain the target creating newArray>RETURN newArrrayEND FUNCTION

Page 9: Advanced Higher Computing Science Standard Algorithms

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 required2 14 28 3

16 432 564 6128 7256 8512 9

1024 10

Page 10: Advanced Higher Computing Science Standard Algorithms

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

Page 11: Advanced Higher Computing Science Standard Algorithms

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

Page 12: Advanced Higher Computing Science Standard Algorithms

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 have different results depending on whether it is dealing with a partially sorted list or a random one?

• Does the sort require any additional memory?

Page 13: Advanced Higher Computing Science Standard Algorithms

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

Page 14: Advanced Higher Computing Science Standard Algorithms

Selection SortPROCEDURE SelectionSort(listA, listB) DECLARE dummy AS CHARACTER INITIALLY "x" # if the contraints of listA are known then use an integer as dummy valueDECLARE minimumPosition INITIALLY 0DECLARE 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 dummyEND FOR END PROCEDURE 

Page 15: Advanced Higher Computing Science Standard Algorithms

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

Page 16: Advanced Higher Computing Science Standard Algorithms

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.

Page 17: Advanced Higher Computing Science Standard Algorithms

Bubble SortPROCEDURE bubblesort(ARRAY OF INTEGER list) DECLARE listLength INITIALLY length(list)  FOR outerLoop FROM listLength -2 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 FOR END PROCEDURE

Page 18: Advanced Higher Computing Science Standard Algorithms

Bubble Sort (swap)

PROCEDURE swap(a, b)  DECLARE temp AS INTEGER INITIALLY a SET a TO b SET b TO temp END PROCEDURE

Page 19: Advanced Higher Computing Science Standard Algorithms

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.

Page 20: Advanced Higher Computing Science Standard Algorithms

Bubble Sort with Boolean variablePROCEDURE 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 END IF SET outerLoop TO outerLoop -1END WHILEEND PROCEDURE

Page 21: Advanced Higher Computing Science Standard Algorithms

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

Page 22: Advanced Higher Computing Science Standard Algorithms

Insertion SortPROCEDURE 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 FOREND PROCEDURE

Page 23: Advanced Higher Computing Science Standard Algorithms

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.

Page 24: Advanced Higher Computing Science Standard Algorithms

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

Page 25: Advanced Higher Computing Science Standard Algorithms

Specimen Paper Q3A game includes a high score table with the names and scores of the top five players stored in two 1-D arrays.

Meena 58Joe 50Patrick 27Marta 25Andrew 23

Index 0 1 2 3 4Scores 23 25 27 50 58

After a number of games, the 1-D array that holds the scores is shown below:

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 0 1 2 3 4Scores 48 25 27 50 58

Page 26: Advanced Higher Computing Science Standard Algorithms

Specimen Paper Q3aSenga 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 0 1 2 3 4Scores 48 25 27 50 58

A bubble sort is used to sort the scores. After the first pass, the list will be sorted:Index 0 1 2 3 4Scores 25 27 48 50 58

(i) 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.

Page 27: Advanced Higher Computing Science Standard Algorithms

Specimen Paper Q3aindex 0 1 2 3 4

48 25 27 50 5825 48 27 50 5825 27 48 50 58

(i) [0]48 and [1]25 [1]48 and [2]27

(ii) swaps = false will not be true until a 2nd pass has occurred

Page 28: Advanced Higher Computing Science Standard Algorithms

2015 Q3Visitors 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 N1200 Jeanie 1001 Y12034 Lewis 999 N15314 Morven 534 N72912 Richard 1287 Y110912 Richard 986 N113452 Will 952 N

Page 29: Advanced Higher Computing Science Standard Algorithms

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-Darray.

• 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 N1200 Jeanie 1001 Y12034 Lewis 999 N15314 Morven 534 N72912 Richard 1287 Y110912 Richard 986 N113452 Will 952 N

Page 30: Advanced Higher Computing Science Standard Algorithms

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

Page 31: Advanced Higher Computing Science Standard Algorithms

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

Page 32: Advanced Higher Computing Science Standard Algorithms

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.

Page 33: Advanced Higher Computing Science Standard Algorithms

2015 Q3cRECEIVE searchKey FROM KEYBOARDSET startPos TO 0 SET endPos TO 120000REPEATSET middle TO (startPos + endPos) / 2 IF array[middle] > searchKey THEN SET startPos TO middle + 1 ELSE SET endPos TO middle -1END IF IF array[middle] = searchKey THEN SET found TO true SEND "Found at position "& middle TO DISPLAY END IFUNTIL found = true OR ( startPos > endPos )

Page 34: Advanced Higher Computing Science Standard Algorithms

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

(seconds)

Total

Anna Millie 17 73·2 4·0 77·2Foster Giant 18 84·1 7·5 91·6

Grainger O’Malley 19 74·0 0 74·0

Page 35: Advanced Higher Computing Science Standard Algorithms

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

Page 36: Advanced Higher Computing Science Standard Algorithms

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.

Page 37: Advanced Higher Computing Science Standard Algorithms

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

Page 38: Advanced Higher Computing Science Standard Algorithms

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.

15 12 18 16 5 19 15 14A 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.

Page 39: Advanced Higher Computing Science Standard Algorithms

2012 Q5b

Initial scores: 15 12 18 16 5 19 15 14A 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:

15 18 16 12 19 15 14 5Write down the list after the next pass.

Page 40: Advanced Higher Computing Science Standard Algorithms

2012 Q5

15 18 16 12 19 15 14 5

18 15 16 12 19 15 14 518 16 15 12 19 15 14 518 16 15 12 19 15 14 518 16 15 19 12 15 14 518 16 15 19 15 12 14 518 16 15 19 15 14 12 5

(b) (i) Original 15 12 18 16 5 19 15 14After 1st pass 15 18 16 12 19 15 14 5

After 2nd pass 18 16 15 19 15 14 12 5

Page 41: Advanced Higher Computing Science Standard Algorithms

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

Page 42: Advanced Higher Computing Science Standard Algorithms

2012 Q5c

(c) A candidate scores these marks:19 19 17 16 14 14 13 11(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.

Page 43: Advanced Higher Computing Science Standard Algorithms

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

Page 44: Advanced Higher Computing Science Standard Algorithms

2011 Q4Senga 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 0 1 2 3 4 5 6Char C D E I L M T

Page 45: Advanced Higher Computing Science Standard Algorithms

2011 Q4SET lower TO lowest indexSET upper TO highest indexREPEAT 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 IFUNTIL list[middle] = search_value OR lower > upperIF search_value = list[middle] THEN SEND "Search item was found at" & middle TO DISPLAYELSE SEND "Search item is not in list" TO DISPLAYEND IF

Page 46: Advanced Higher Computing Science Standard Algorithms

2011 Q4

Copy and complete the diagram for all of the characters.

Page 47: Advanced Higher Computing Science Standard Algorithms

2011 Q4SET lower TO lowest indexSET upper TO highest indexREPEAT 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 IFUNTIL list[middle] = search_value OR lower > upperIF search_value = list[middle] THEN SEND "Search item was found at" & middle TO DISPLAYELSE SEND "Search item is not in list" TO DISPLAYEND IF

Page 48: Advanced Higher Computing Science Standard Algorithms

2011 Q4SET lower TO lowest indexSET upper TO highest indexREPEAT 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 IFUNTIL list[middle] = search_value OR lower > upperIF search_value = list[middle] THEN SEND "Search item was found at" & middle TO DISPLAYELSE SEND "Search item is not in list" TO DISPLAYEND IF

Page 49: Advanced Higher Computing Science Standard Algorithms

2011 Q4SET lower TO lowest indexSET upper TO highest indexREPEAT 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 IFUNTIL list[middle] = search_value OR lower > upperIF search_value = list[middle] THEN SEND "Search item was found at" & middle TO DISPLAYELSE SEND "Search item is not in list" TO DISPLAYEND IF

Page 50: Advanced Higher Computing Science Standard Algorithms

2011 Q4SET lower TO lowest indexSET upper TO highest indexREPEAT 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 IFUNTIL list[middle] = search_value OR lower > upperIF search_value = list[middle] THEN SEND "Search item was found at" & middle TO DISPLAYELSE SEND "Search item is not in list" TO DISPLAYEND IF

Page 51: Advanced Higher Computing Science Standard Algorithms

2011 Q4SET lower TO lowest indexSET upper TO highest indexREPEAT 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 IFUNTIL list[middle] = search_value OR lower > upperIF search_value = list[middle] THEN SEND "Search item was found at" & middle TO DISPLAYELSE SEND "Search item is not in list" TO DISPLAYEND IF

Page 52: Advanced Higher Computing Science Standard Algorithms

2011 Q4SET lower TO lowest indexSET upper TO highest indexREPEAT 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 IFUNTIL list[middle] = search_value OR lower > upperIF search_value = list[middle] THEN SEND "Search item was found at" & middle TO DISPLAYELSE SEND "Search item is not in list" TO DISPLAYEND IF