Upload
vivian-wells
View
212
Download
0
Embed Size (px)
Citation preview
1
Higher Grade Computing Studies4. Standard Algorithms
Higher Computing Software Development
S. McCrossan
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 Studies4. Standard Algorithms
Higher Computing Software Development
S. McCrossan
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.
3 64 18 2 64 98 2 100 1 2
1 2 3 4 5 6 7 8 9 10
3
Higher Grade Computing Studies4. Standard Algorithms
Higher Computing Software Development
S. McCrossan
Linear Search
1.1 ask for item being searched for1.2 get item being searched for
1.3 FOR each item in the list1.4 IF item_in_list = item being searched for THEN1.5 report position in list1.6 END IF1.7 END FOR loop
4
Higher Grade Computing Studies4. Standard Algorithms
Higher Computing Software Development
S. McCrossan
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 Studies4. Standard Algorithms
Higher Computing Software Development
S. McCrossan
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.
3 64 18 2 64 98 2 100 1 2
1 2 3 4 5 6 7 8 9 10
6
Higher Grade Computing Studies4. Standard Algorithms
Higher Computing Software Development
S. McCrossan
Counting Occurrences
1.1 set counter to zero1.2 ask for item being counted1.3 get item being counted1.4 FOR each item in the list1.5 IF item_in_list = item being counted THEN1.6 add 1 to the counter1.7 END IF1.8 END FOR loop1.9 display message showing number of occurrences (counter)
7
Higher Grade Computing Studies4. Standard Algorithms
Higher Computing Software Development
S. McCrossan
Finding the Maximum
• This algorithm allows the programmer to find the highest number in a list (an array).
8
Higher Grade Computing Studies4. Standard Algorithms
Higher Computing Software Development
S. McCrossan
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.
3 64 18 2 64 98 2 100 1 2
1 2 3 4 5 6 7 8 9 10
9
Higher Grade Computing Studies4. Standard Algorithms
Higher Computing Software Development
S. McCrossan
Counting Occurrences
1.1 set ‘largest so far’ to the value of the first item in the list1.2 FOR each of the remaining items1.3 IF current item > largest so far THEN1.4 set ‘largest so far’ = current item1.5 END IF1.6 NEXT item in list1.7 display message showing ‘largest so far’
10
Higher Grade Computing Studies4. Standard Algorithms
Higher Computing Software Development
S. McCrossan
Finding the Minimum
• This algorithm allows the programmer to find the lowest number in a list (an array).
11
Higher Grade Computing Studies4. Standard Algorithms
Higher Computing Software Development
S. McCrossan
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.
3 64 18 2 64 98 2 100 1 2
1 2 3 4 5 6 7 8 9 10
12
Higher Grade Computing Studies4. Standard Algorithms
Higher Computing Software Development
S. McCrossan
Counting Occurrences
1.1 set ‘smallest so far’ to the value of the first item in the list1.2 FOR each of the remaining items1.3 IF current item < smallest so far THEN1.4 set ‘smallest so far’ = current item1.5 END IF1.6 NEXT item in list1.7 display message showing ‘smallest so far’