Search algorithms Flashcards

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

linear search algorithm

A

traverse the list using a for loop

compare each item to the search term

if there is no match it is not in the list

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

big - O complexity of the linear search

A

O(n)

num operations directly proportional to num items in the list

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

returned value of linear search function

A

boolean - True or False

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

linear search benefits and limitations

A

linear search can be used on any list, but takes more operations than binary search

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

binary search

A

find midpoint

compare to search term

discard half of the list where the search term is not found

repeat until found or list is down to one item

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

Big- O complexity of binary search

A

O(log n)

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

two ways of implementing binary search

A
  • slicing list into smaller list
  • using “start” and “stop” markers to mark the ara of the list where the search term might occur
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

benefits and limitations of binary search

A
  • faster than linear search
  • can only be used with an ordered list
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

five levels of big O complexity in order

A

constant - O(1)
logarithmic - O(log n)
linear - O(n)
polynomial- O(n^x)
exponential - O(x^n)

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

how to define the complexity of an algorithm

A

constant - no loop
logarithmic- dividing a list in half repeatedly
linear - a for loop
polynomial - a loop inside a loop
exponential - an even more complex program

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

how to determine the complexity of an expression

A

Ignore the actual numbers

Look for any places where n occurs in the expression

Compare this to the big-O levels to find a match to the shape

If there is more than one example, take the most complex

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

two ways to swap values

A

tuple substitution

three- line swap using a “temp” variable

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

bubble sort

A

traverse list
compare each value to the next, swap if wrong way around
traverse multiple times until there are no swaps

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

how to recognise bubble sort

A

a for loop inside a while loop

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

big- O complexity of bubble sort

A

polynomial
O(n^x)

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

evaluate bubble sort

A

very slow
avoid using unless list is short

17
Q

insertion sort

A

traverse list once
compare value at marker to one before it
if smaller, pass all the way back and insert it in its correct position

18
Q

how to recognise insertion sort

A

for loop with a backwards for loop inside it

19
Q

big O complexity of insertion sort

A

polynomial
O(n^x)

20
Q

evaluate insertion sort

A

usually faster than bubble sort

works well to tidy up list which is almost sorted

21
Q

merge sort

A

split list into many small lists of one

marge to make sorted lists of two

continue to merge until one sorted list

22
Q

how to recognise merge sort

A

two functions -
one to split the list
plus a merge function

23
Q

merge sort complexity

A

O ( n x logn )

24
Q

evaluate merge sort

A

fast and reliable

takes up more space in memory than other sorts

25
Q

quick sort

A

find a pivot value

sort list into two partitions of values larger and smaller than the pivot

repeat recursively on each of the partitions

26
Q

recognise quick sort

A

partition function
called by a sort function

27
Q

quick sort complexity

A

polynomial
O(n^x)

usually much faster

28
Q

evaluate quick sort

A

fast, almost as fast as merge sort

not as reliable as merge sort

does not take up extra space in memory