Algorithms 2.1 Searches and sorts Flashcards

1
Q

Describe the steps a binary search will follow to look for a number in a sorted list

A

Find the middle item in the ordered list.

check if selected number is equal to / matches target number

if this is the item you are looking for, then stop the search - since the number has been found

if this item is not equal to the target item then check:

if searched number is larger, then discard left half

if searched number is smaller, then discard right half

Repeat this process until the number or item is found or
remaining list is of size 1 / 0 (which shows number was not found/ or number was not in list)

there are no more items to be found (remaining list is of size 1 or 0)

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

Examples of Standard searching algorithms:

A

Binary search
Linear search

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

What are binary search and linear search examples of

A

Examples of Standard searching algorithms:

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

Use the binary search algorithm to find he number 8 in the following list
3 6 8 11 13 15 18

A

Middle item = (7 + 1) /2 = 4th item
4th item = 11

compare 11 to 8
11 > 8 so split left

Items in list: 3,6,8

Middle item = (3+1)/2 = 2nd item = 6

compare 6 to 8
6 < 8 so split right

Items in the list: 8

8 is the last item in the list
Middle item = (1+1)/2 = 1st item = 8

Stop searching as 8 has been found

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

Show the stages of a binary search to find the word ‘zebra’ when applied to the data shown

amber, house, kick, moose, orange, range, tent, wind, zebra

A

middle item = (9 + 1) / 2 = 5th item
5th item = orange

compare zebra to orange
zebra > orange, so split right

middle item = (4+1)/2 = 2.5 rounds up to 3rd item
3rd item = Wind

compare zebra to wind
zebra>wind, so split right

Zebra is the last item in the list OR Middle item = (1+1)/2 = 1st item = Zebra
Compare zebra to zebra
Zebra = Zebra

Item has been found

Middle item = (1+1)/2 = 1st item = Zebra

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

Describe the steps a linear search would follow when searching for a number that is not in the given list.

A

Starting with the first value
Checking all values in order

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

Describe the steps a linear search

A

1) look at the first item in the unordered list

2) is this is the item you are looking for, then stop the search - the item has been found

3) if not, then look at the next item in the list

repeat steps 2) - 3) until you find the item that you are looking for or you have checked every item (this shows that the item was not in the list)

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

Use linear search to find “11” in the list
3 6 8 11 13 15 18

A

Starting with the first value
Check each item in order:

Check 1st item: 3 ≠ 11 move to the next item in the list
Check 2nd item: 6 ≠ 11 move to the next item in the list
Check 3rd item: 8 ≠ 11 move to the next item in the list
Check 4th item: 11 = 11

Stop searching as 11 has been found

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

Nicoa has a list of numbers
2 3 7 5 13 11

She says, “I can’t use a binary search to find 13” Why is this the case

A

A binary search only works on ordered data

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

file:///C:/Users/44748/Documents/The%20code%20for%20all%20the%20searching%20and%20sorting%20algorithms.pdf

A

file:///C:/Users/44748/Documents/The%20code%20for%20all%20the%20searching%20and%20sorting%20algorithms.pdf

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

Standard sorting algorithms:

A

o Bubble sort
o Merge sort
o Insertion sort

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

o Bubble sort
o Merge sort
o Insertion sort

are examples of

A

Standard sorting algorithms

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

Merge sort is an example of ______

A

divide and conquer algorithm

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

describe the steps to bubble sort

A

1)look at the first two items in the list

2) if they are in the right order, you dont have to do anything
If they are in the wrong order, swap them

3) move on to the next pair of items (the 2nd and 3rd items) and repeat step 2).

4) repeat step 3) until you get to the end of the list - this is called one pass
The last item will now be in the correct place, so dont include it in the next pass

5) Repeat steps 1) - 4) until there are no swaps in a pass

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

describe the steps to merge sort

A

1) split the list in half (the smaller lists are called sub lists) - the second sub-list should start at the middle item

2) keep repeating step 1) on each sub-list until all the lists only contain one item

3) merge pairs of sub-lists so that each sub-list has twice as many items
Each time you merge sub-lists, sort the items into the right order

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

describe the steps to insertion sort

A

1) Look at the second item in a list
2) Compare it to all items before it (in this case just the first item) and insert the number into the right place
3) Repeat step 2) for the third, fourth, fifth etc. items until the last number/item in the list has been inserted into the correct place

17
Q

advantages of linear search

A

much simpler than binary search
can be used on any type of list
daya doesn’t have to be order
used on small lists

18
Q

disadvantages of linear search

A

not as efficient as binary search

19
Q

adv. of binary search

A

much more efficient than a linear search (once the list has been ordered)

in general binary search takes fewer steps to find the item you are looking for - which makes it more suitable for large lists of items

20
Q

disadv. of binary search

A

data has to be order before a specific item can be searched

21
Q

adv. of bubble sort

A

simple algorithm - can be easily implemented on a computer
an efficient way to check if a list is already in order
doesn’t use very much memory as all the sorting is done in the original list

22
Q

disadv. of bubble sort

A

it is an inefficient way to sort a list
due to being inefficient, the bubble sort algorithm does not cope well with a very large list of items

23
Q

adv. of merge sort

A

in general, more efficient and quicker than bubble sort and insertion sort algorithms for large lists

it has a very consistent running time regardless of how ordered the items in the list are

24
Q

disav. of merge sort

A

it is slower than other algorithms for small lists
even if the list is already sorted, it still goes through the whole splitting and merging process

it uses more memory than the other sorting algorithms (bubble and insertion sort) in order to create the separate lists

25
Q

adv. of insertion sort

A

it is an intuitive way of sorting things and can be coded

it copes very well with small lists - for this reason, an insertion/merge hybrid sort is often used to take advantage of the strengths of each algorithm

all the sorting is done on the original list so, like bubble sort, it doesn’t require very much additional memory

it is very quick to add items to an already ordered list

it is also very quick in checking that a list is already sorted

26
Q

disadv. of insertion sort

A

it doesn’t cope well with very large lists