2.1 Algorithms Flashcards

(16 cards)

1
Q

What are the steps of a linear search?

A

Check the first value
IF it is the value you are looking for Celebrate and stop
ELSE move to and check the next value
REPEAT UNTIL you have checked all the
elements and not found the value you are
looking for

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

What are the pros of linear searching?

A

Performs well with small and medium sized lists
Fairly simple to code
Data doesn’t need to be in any particular order
Doesn’t break if new items are added into the list

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

What are the cons of linear searching?

A

May be too slow to process large lists
If you are searching for the last item in a list, you may have to go through the whole list

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

What are the steps of a binary search?

A

List first needs to be sorted into order.
take the middle value.
Compare to the value you are looking for.
IF it is the value you are looking for, celebrate and stop
ELSEIF it is larger than the value you are looking for, take the values to the left of the middle value and find the middle again
IF it is smaller than the one you are looking for, take the values from the right of the middle value and find the middle again

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

How do you find the index of the midpoint of an array?

A

LP (Left point) + RP (Right point) DIV 2

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

What are the pros of a binary search?

A

Very good performance over large ordered lists.
Takes a maximum of 8 loops to find an item in a list of 1000 items.

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

What are the cons of a binary search?

A

Only works on ordered lists.
More complicated than a linear search to code.
If new items are added to the list it will need reordering every time they are added.

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

What are the steps to complete a bubble sort?

A

Take the first element and the second element from the list.
Compare them.
IF element 1 > element 2 THEN, swap item
ELSE, do nothing
REPEAT: move along the list to the next pair
IF no more elements: Go to 1
ELSE: Go to 2
UNTIL: you have moved through the entire list and not made any changes

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

What are the pros of using a bubble sort?

A

Simple to write
Simple to understand
Data is sorted in the same Memory location that it is held.

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

What are the cons of using a bubble sort?

A

One of the slowest ways to sort a list.

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

What are the steps of completing a merge sort?

A

Split all elements up into individual lists
Compare the first adjacent element in both lists
Put the smallest into a new list
Compare the next element of 1 list with the second element of the second list
Put the smallest into a new list
Repeat until merged

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

What are the pros of a merge sort?

A

It is the fastest of the three types of sort (bubble, insert, merge)
It is the best option to use for long lists of data (more than 1000 long)

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

What are the cons of a merge sort?

A

More complicated to code compared to bubble and insert
It may use twice the memory size of the list - depending on the way it is coded

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

What are the steps of an insertion sort?

A

the elements are an unsorted list
Compare the 1st element in the unsorted list to the next element in the sorted list
IF it is smaller, swap it by moving the element to the left
ELSE IF there are no elements in the unsorted list put it in the final position
REPEAT UNTIL all element in the unsorted list are in the sorted list

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

What are the pros of an insertion sort?

A

Easy to implement / code
Memory efficient
good for small or nearly sorted lists

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

What are the cons of an insertion sort?

A

Inefficient for large datasets
Not suitable for high-volume data
Can be slower than other algorithms