algorithms Flashcards

what are they

1
Q

what is abstraction

A

picking out unnecessary details leaving only the important information.

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

what is decomposition

A

breaking down a task into smaller pieces.

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

what is an algorithm

A

a series of instructions to complete a task

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

which is easier to code

a) binary search
b) linear search

A

b)linear search

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

which is more efficient

a) binary search
b) linear search

A

a)binary search

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

What is the precondition that must be true before we can use a binary search

A

the data must be ordered

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

how does a linear search work

A

Each item in turn from the data set is compared with the target until either the target item is found or the end of the list is reached

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

how does a binary search work

A

The data set MUST be ordered. The middle item (round down if an even number of items) is compared to the target. If the item is not the target then a check is run to see whether the target would appear ‘above’ (further forward in the list) or ‘below’ (further back in the list) - if it is present at all, which it may not be.
If the item would appear above the original mid point then a new mid point at the centre of the upper-half (all items above the original mid-point) is chosen.
Else if the item would appear below the original mid point a new mid point at the centre of the lower-half (all items below the original mid point) is chosen.
The process is then repeated with the new mid point until either the target is found OR the last item has been checked and the item has been proven not to be in the data set

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

Which search algorithm is represented by the following pseudocode?

found = False 
number = 0 
target = value 
WHILE found == False AND number < LEN(book) 
IF data[number] == target 
found = True 
number = number + 1 
END WHILE 
IF found == True 
PRINT “target found” 
ELSE 
PRINT “target not data” 
END IF
A

linear search

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

Which search algorithm is represented by the following pseudocode?

found = False 
left = 0 
right = LEN(data) - 1 
target = value 

WHILE found == False and left <= right
mid = (left + right) DIV 2

IF data[mid] == target 
found = True 
ELSE 
IF target > data[mid]: 
left = mid + 1 
ELSE 
right = mid - 1 
END IF 
END IF 
END WHILE 
IF found == True 
print("target found") 
ELSE 
print("target not in data") 
END IF
A

binary search

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

Which search algorithm is most efficient if the target item is near the start of the data set

A

linear search

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

Which sort algorithm makes repeated passes of the data set and guarantees to move one item at a time to the end of the data set

A

bubble sort

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

What is the effect of this snippet of pseudocode?

items = ["y", "x"] 
i = 1 
if items [ i - 1 ] > items [ i ]: 
temp = items [ i - 1 ] 
items [ i - 1 ] = items [ i ] 
items [ i ] = temp
A

it swaps and items become x,y

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

Why is temp used in this snippet of pseudocode?

items = ["y", "x"] 
i = 1 
if items [ i - 1 ] > items [ i ]: 
temp = items [ i - 1 ] 
items [ i - 1 ] = items [ i ] 
items [ i ] = temp
A

otherwise i - 1 would be lost

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

how does a merge sort work

A
  1. The set of unsorted data is split into two
  2. Step 1 is repeated with each new unsorted data set until we have single items (so 16 unsorted items is split into 2 sets of 8 unsorted items, then 4 sets of 4 unsorted items then 8 sets of 2 unsorted items and finally 16 sets of 1 item each - 1 item is considered sorted)
  3. Pairs of sorted data sets (started with single items) are merged into a new sorted data set
  4. Step 3 is repeated until 1 fully sorted data set exists (so 16 sets of 1 sorted item becomes 8 sets of 2 sorted items then 4 sets of 4 sorted items etc.)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

what is pseudocode

A

a programming language that is in English to make it easier understood