Presentation is loading. Please wait.

Presentation is loading. Please wait.

Higher Grade Computing Studies 4. Standard Algorithms Higher Computing Software Development S. McCrossan 1 Linear Search This algorithm allows the programmer.

Similar presentations


Presentation on theme: "Higher Grade Computing Studies 4. Standard Algorithms Higher Computing Software Development S. McCrossan 1 Linear Search This algorithm allows the programmer."— Presentation transcript:

1 Higher Grade Computing Studies 4. Standard Algorithms Higher Computing Software Development S. McCrossan 1 Linear Search This algorithm allows the programmer to search a list (an array) for a specific item of data. Each item in the list is compared with the data being searched for. When a ‘match’ is found then the position (in the array) is displayed.

2 Higher Grade Computing Studies 4. Standard Algorithms Higher Computing Software Development S. McCrossan 2 Linear Search Let's say we have a list of 10 numbers in an array. (The top number only shows the position of each box in the array). We want to look for the number 64. We can see that it appears twice on the list, at positions 2 and 5. 3641826498210012 1 2 3 4 5 6 7 8 9 10

3 Higher Grade Computing Studies 4. Standard Algorithms Higher Computing Software Development S. McCrossan 3 Linear Search 1.1ask for item being searched for 1.2get item being searched for 1.3FOR each item in the list 1.4IF item_in_list = item being searched for THEN 1.5report position in list 1.6END IF 1.7END FOR loop

4 Higher Grade Computing Studies 4. Standard Algorithms Higher Computing Software Development S. McCrossan 4 Counting Occurrences This algorithm allows the programmer to count how many times an item of data appears in a list (an array). Each item in the list is compared with the item of data being counted. When a ‘match’ is found, one is added to the total number of occurrences.

5 Higher Grade Computing Studies 4. Standard Algorithms Higher Computing Software Development S. McCrossan 5 Counting Occurrences Let's say we have a list of 10 numbers in an array. (The top number only shows the position of each box in the array). We want to count how many times the number 2 appears in the list. We can see that it appears three times in the list. 3641826498210012 1 2 3 4 5 6 7 8 9 10

6 Higher Grade Computing Studies 4. Standard Algorithms Higher Computing Software Development S. McCrossan 6 Counting Occurrences 1.1set counter to zero 1.2ask for item being counted 1.3get item being counted 1.4FOR each item in the list 1.5IF item_in_list = item being counted THEN 1.6add 1 to the counter 1.7END IF 1.8END FOR loop 1.9display message showing number of occurrences (counter)

7 Higher Grade Computing Studies 4. Standard Algorithms Higher Computing Software Development S. McCrossan 7 Finding the Maximum This algorithm allows the programmer to find the highest number in a list (an array).

8 Higher Grade Computing Studies 4. Standard Algorithms Higher Computing Software Development S. McCrossan 8 Counting Occurrences Let's say we have a list of 10 numbers in an array. (The top number only shows the position of each box in the array). We want to find the highest number in the list. As you can see, it is the number 100. 3641826498210012 1 2 3 4 5 6 7 8 9 10

9 Higher Grade Computing Studies 4. Standard Algorithms Higher Computing Software Development S. McCrossan 9 Counting Occurrences 1.1set ‘largest so far’ to the value of the first item in the list 1.2FOR each of the remaining items 1.3IF current item > largest so far THEN 1.4set ‘largest so far’ = current item 1.5END IF 1.6NEXT item in list 1.7display message showing ‘largest so far’

10 Higher Grade Computing Studies 4. Standard Algorithms Higher Computing Software Development S. McCrossan 10 Finding the Minimum This algorithm allows the programmer to find the lowest number in a list (an array).

11 Higher Grade Computing Studies 4. Standard Algorithms Higher Computing Software Development S. McCrossan 11 Counting Occurrences Let's say we have a list of 10 numbers in an array. (The top number only shows the position of each box in the array). We want to find the lowest number in the list. As you can see, it is the number 1. 3641826498210012 1 2 3 4 5 6 7 8 9 10

12 Higher Grade Computing Studies 4. Standard Algorithms Higher Computing Software Development S. McCrossan 12 Counting Occurrences 1.1set ‘smallest so far’ to the value of the first item in the list 1.2FOR each of the remaining items 1.3IF current item < smallest so far THEN 1.4set ‘smallest so far’ = current item 1.5END IF 1.6NEXT item in list 1.7display message showing ‘smallest so far’


Download ppt "Higher Grade Computing Studies 4. Standard Algorithms Higher Computing Software Development S. McCrossan 1 Linear Search This algorithm allows the programmer."

Similar presentations


Ads by Google