1.3 Algorithms and Programs Flashcards

You may prefer our related Brainscape-certified flashcards:
1
Q

Write the bubble sort pseudocode

A

Procedure BubbleSort(List)
n = len(List)
Repeat
swapped = false
for i = 1 to n-1
if List[i-1] > List[i] then
temp = List[i-1]
List[i-1] = List[i]
List[i] = temp
swapped = true
end if
end for
until not swapped
End procedure

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Write insertion sort pseudocode

A

Procedure InsertionSort(List)
n = len(List)
For i = 1 to n-1
temp = List[i]
z = i
While z>0 And List[z-1] > temp
List[z] = List[z-1]
z = z – 1
End While
List[z] = Temp
Next i
End procedure

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Write quicksort pseudocode

A

Procedure Quicksort(List, Lo by val, Hi by val)
TmpLo = Lo
TmpHi = Hi
Pivot = List((Lo+Hi)/2)
While TmpLo <= TmpHi
While List(TmpLo) < Pivot and TmpLo < Hi
TmpLo = TmpLo + 1
End While
While Pivot < List(TmpHi) and TmpHi > Lo
TmpHi = TmpHi - 1
End While
If TmpLo <= TmpHi Then
temp = List[TmpLo]
List[TmpLo] = List[TmpHi]
List[TmpHi] = temp
TmpLo=TmpLo+1
TmpHi=TmpHi-1
End If
End While
if Lo < TmpHi Then QuickSort(List, Lo, TmpHi)
if TmpLo < Hi Then QuickSort(List, TmpLo, Hi)
End Procedure

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

binary search algorithm

A

Function BinarySearch(List, SearchItem)
Lo = 0
Hi = Len(List) - 1
While Hi >= Lo
Mid = Lo + (Hi – Lo) Div 2
If SearchItem = List[Mid] Then
Return Mid
Else if SearchItem < List[Mid] Then
Hi = Mid – 1
Else
Lo = Mid + 1
End if
End While
Return -1
End Function

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Linear Search algorithm

A

Function LinearSearch(List, SearchItem)
i = 0
found = false
While i <= Len(List)-1 and Not found
if Searchitem = List[i] then
found = true
else
i = i + 1
end if
End While
If found then
return i
else
return -1
end if
End Function

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

what are trace-tables used for

A

use a trace table with a column for each variable and if applicable, a column for output. As values change you should note those changes in the table

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

what is meant by the term algorithm and give three common methods of defining algorithms

A

an algorithm is a set of rules to solve a specific problem

flow-chart,
structured english,
pseudocode

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

what is variable scope and the difference between local and global variables

A

The scope of a variable describes the parts of the program in which the variable can be accessed.

A global variable - variable can be accessed anywhere in the program - has a global scope

A local variable - variable can only be accessed within its subroutine - has a local scope

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Why is it good practise to use constants, meaningful names for identifiers and annotation in computer programs?

A

-easier to make sense of what certain parts of the program do

-easier to make sense what value the variables hold

-makes it easier for other programmers to make changes to the program if they understand

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

what is a rogue value

A

A special value in a data set that indicates the end of the data. For example a list of scores in a test could be entered with -1 indicating there are no more scores to enter

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

what are the features of a recursive algorithm and what problems might the programmer encounter

A

recursive algorithms usually require much less code
calls itself
has a terminating condition
when it calls itself it passes one or more parameters

problems:
when it runs it can use more memory as it has to keep track of the multiple calls to the procedures on the run-time stack

debugging recursive algorithms can be more difficult because when an error occurs its not easy to work out which level of the recursion the error has occurred

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Explain the purpose of DIV and MOD

A

DIV - same as divide but only returns the whole part of the number

MOD - carries out the division but only returns the remainder

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What is meant by sequence, selection and repetition in programming

A

sequence - executing lines of code one after the other in the order given

selection - where there is a choice as to whether a set of instructions are executed (e.g. if) selections can also be nested (can contain further e.g. if statements)

repetition - when the same instruction is repeated in a loop. The loops can be nested (loops can contain further loops etc.)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

explain the purpose of procedures

A

procedures are blocks of code which can be called from anywhere in the program to carry out a task.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

what is a parameter and explain the difference between value and reference, giving reasons to use each of them

A

parameters are values that can be sent to a procedure and/or received from a procedure

passing by value means that a copy of the parameter value is passed into the subroutine and doesn’t return a value back.

could be used for e.g. a procedure to draw a dot on a screen might take two parameters x and y. These would only need to be value parameters.

passing by reference means that the memory address of the parameter is passed into the procedure to be used and any changes made to the value is returned to the parameter.

could be used for E.g. a procedure which takes in a length in metres and returns two values for feet and inches

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

what is validation and its purpose? give examples of validation types

A

Validation is used to check that the data being entered as input is reasonable or sensible in the context where it is used.

its purpose is to reduce the possibility of entering invalid data into the system

some validation techniques are:
Range check
Presence check
Length check
Type check
Format check
Lookup check
Check-Digits

17
Q

What is verification

A

checking if the data being entered is correct.

e.g. having to enter a password twice during a login process

18
Q

How does a linear search (serial or sequential) algorithm operate

A

serial search:
involves looking at each item in the data set (list) until the next item is found or the end of the list is reached (signifying the item doesn’t exist)

sequential search:
same as serial search and can be used on data that is already sorted in order of the search field, but not binary search as direct access isn’t possible (e.g. searching a file on tape)

when searching through the list, if an item is read that would be after the item being searched for, the search can stop immediately instead of waiting until the end of the list.

19
Q

how does a binary search operate and what are the advantages?

A

data has to be sorted in order, and data has to be able to be directly accessed (e.g. memory or on disk) so storage e.g. file, tape storage cannot be used

how it works:
middle item is examined to see if it is the item being searched for

if not, either the bottom or top half of the list is discarded depending on how big the value being searched for is

take another middle term and continue the process

can be iterative and recursive

20
Q

how does bubble sort operate

A

repeatedly stepping through the list to be sorted, comparing each pair of adjacent items and swapping them if they are in the wrong order.

Pass through the list is completed until no swaps are needed

21
Q

how does insertion sort operate

A

starting with second item, compare to all items before it in the list, shifting them up one place until the correct place to insert is found

22
Q

how does quicksort operate

A

choose a pivot in the list (data before the pivot is ListA and data after the pivot is ListB)

Move data less than the pivot into ListA and data after the pivot into ListB

Quicksort ListA and ListB

recursive algorithm

23
Q

what is the shortest path algorithm

A

Dijkstra’s algorithm

used to find the shortest route between two nodes in a connected ‘graph’

used in e.g. satnav