A Level OCR Computer Science 2.3 - Algorithms

0.0(0)
studied byStudied by 1 person
0.0(0)
full-widthCall Kai
learnLearn
examPractice Test
spaced repetitionSpaced Repetition
heart puzzleMatch
flashcardsFlashcards
GameKnowt Play
Card Sorting

1/90

encourage image

There's no tags or description

Looks like no tags are added yet.

Study Analytics
Name
Mastery
Learn
Test
Matching
Spaced

No study sessions yet.

91 Terms

1
New cards

What are the characteristics of a good algorithm

Clear and precisely stated steps, allows for invalid inputs, terminates at some point, efficient, understandable

2
New cards

What is complexity

The order with which time and space grows

3
New cards

What does time complexity scale within an algorithm

The number of individual steps taken

4
New cards

How do we write big O notation

O(n^x) where x is the highest power of n the algorithm reaches

5
New cards

What is constant complexity

No matter how large n gets the complexity stays the same

6
New cards

What is the big O for constant complexity

O(1)

7
New cards

What is a quick way of recognising constant complexity

Has no loops

8
New cards

What is linear complexity

Complexity increases by a set amount for each increase in n

9
New cards

What is the big O for linear complexity

O(n)

10
New cards

What is a quick way of recognizing linear complexity

One loop that executes n times

11
New cards

What is quadratic complexity

Complexity scales with the square of n as n increases

12
New cards

What is the big O for quadratic complexity

O(n^2)

13
New cards

What is a quick way of recognising quadratic complexity

A loop with a loop nested in it that both execute n times

14
New cards

What is logarithmic complexity

Complexity increases at an exponentially decreasing rate as n increases

15
New cards

What is the big O of logarithmic complexity

O(log n)

16
New cards

What is a quick way of recognising logarithmic complexity

Look for the halving of the data set

17
New cards

What is exponential complexity

Complexity increases exponentially as n increases

18
New cards

What is the big O of exponential complexity

O(2^n)

19
New cards

What is the quick way of recognising exponential complexity

Recursive function that calls itself twice

20
New cards

What is big O notation

A measure of the space or time complexity of an algorithm

21
New cards

What is a permutation

A possible configuration of something, e.g. the order items are in

22
New cards

What is the time complexity of an algorithm

The order by which the time taken for an algorithm to be completed grows

23
New cards

What is the space complexity of an algorithm

The order by which the space taken up by an algorithm grows

24
New cards

Why it the big O a measure of interest to computer scientists

Big O is a way of comparing algorithms so that the most efficient one can be chosen. A bad time complexity can make a problem involving vast amounts of data infeasible to solve

25
New cards

How does linear search work

Can work on both ordered and un-ordered data sets

Gets first elements

If equal then report is found

If not equal then move to next element

Repeat for all elements, until found/ end of list reached

26
New cards

When is linear search used

When items are not ordered

When list is very small

27
New cards

What is the worst case big O time of linear search

O(n)

28
New cards

What is the average case big O time of linear search

O(n)

29
New cards

What is the best case big O time of linear search

O(1)

30
New cards

What is the big O space of linear search

O(1)

31
New cards

How does binary search work on an array

Works on an ordered set of data

Find mid-point

If equal to midpoint item is found

If less than mid-point make sub- list from left

If greater than mid-point make sub-list from right

Repeat with sub-list until found/sub-list is empty

32
New cards

What is the algorithm for binary search

function binary_search(array, target, left, right)

>while left <= right

>>midPoint = (left + right) DIV 2

>>if array[midPoint] == target

>>>return True

>>elif array[midPoint] < target

>>>left = midPoint + 1

>>else

>>>right = midPoint -1

>endwhile

>return False

endfunction

33
New cards

What is the worst case big O time of a binary search on an array

O(log n)

34
New cards

What is the average case big O time of a binary search on an array

O(log n)

35
New cards

What is the best case big O time of a binary search on an array

O(1)

36
New cards

What is the big O space of a binary search on an array

O(1)

37
New cards

What is the worst case big O time of a binary search on a binary tree

O(n)

38
New cards

What is the average case big O time of a binary search on a binary tree

O(log n)

39
New cards

What is the best case big O time of a binary search on a binary tree

O(1)

40
New cards

What is the big O space of a binary search on a binary tree

O(1)

41
New cards

Under what circumstance can we not use a binary search

On an unsorted list

42
New cards

Where might we use a linear search instead of a binary search

On an unsorted list, on a small list, on a linked list

43
New cards

How does a bubble sort work

>Starting at the beginning of the list

Items are swapped with their

neighbour if they are out of order.

>Each pair of neighbours is checked in

order.

>When a swap is made a flag is set.

>If at the end of the list the flag has

been set the flag is unset and the

algorithm starts from the beginning of

the list again.

>When the algorithm gets to the end

of the list and the flag is unset the list

is sorted and the algorithm finishes.

44
New cards

How can bubble sort be improved

Checking one less item per iteration

Alternating sorting directions

45
New cards

What is the worst case big O time of bubble sort

O(n^2)

46
New cards

What is the average case big O time of bubble sort

O(n^2)

47
New cards

What is the best case big O time of bubble sort

O(n)

48
New cards

What is the big O space of bubble sort

O(1)

49
New cards

How does an insertion sort work

Breaks list into a sorted list (initially empty) and an unsorted list. One item taken from the unsorted list at a time and inserted into the sorted list in the correct place

50
New cards

What is the algorithm for insertion sort

def ins(list, lastindex)

>for x = 1 to last index:

>>currentdata = list[x]

>>position = x

>>while (position > 0 AND list[x-1] > currentdata)

>>>list[position] = list[position -1]

>>>position = position - 1

>>list[position] = currentdata

51
New cards

What is the worst case big O time of insertion sort

O(n^2)

52
New cards

What is the average case big O time of insertion sort

O(n^2)

53
New cards

What is the best case big O time of insertion sort

O(n)

54
New cards

What is the big O space of insertion sort

O(1)

55
New cards

Why is bubble sort inefficient

Requires significantly more swaps than other sorting algorithms, meaning it takes up more CPU cycles to solve the same problem

56
New cards

What kind of data structure is a stack

A LIFO - Last in first out + dynamic

57
New cards

What can a stack be thought of as

A stack of books where you can only remove the top one

58
New cards

What commands are used to add and remove items in a stack and what do they do

Push - Add item to the top, Pop - remove the top item

59
New cards

How are pointers used in stacks

Pointer to top of the stack, tells which item is to be removed or where an item is to be added

60
New cards

What kind of data structure is a queue

A FIFO - First in First out

Dynamic - list length and content can change

61
New cards

What can a queue be thought of as

A queue of people for a shop

62
New cards

What commands are used to add and remove items in a queue and what do they do

Enqueue - add an item to back, Dequeue remove the item from the front

63
New cards

How are pointers used in queues

The pointer at both front and back of the list, these shows where items are added and removed from and move when an item is added or removed

64
New cards

How does merge sort work

Divides the unsorted list into n sublists each containing one element, repeatedly merge sublists to produce newly sorted sublists until there is only one sublist remaining. This is the sorted list

65
New cards

What is the worst case big O time of merge sort

O(nlog n)

66
New cards

What is the average case big O time of merge sort

O(nlog n)

67
New cards

What is the best case big O time of merge sort

O(nlog n)

68
New cards

What is the big O space of merge sort

O(n)

69
New cards

How does quick sort work

Finds a split point which divides the list into two sublists, one containing items more than the split point, the other less. This is done repeatedly till the split points have nothing to compare to.

70
New cards

What is the worst case big O time of quick sort

O(n^2)

71
New cards

What is the average case big O time of quick sort

O(nlog n)

72
New cards

What is the best case big O time of quick sort

O(nlog n)

73
New cards

What is the big O space of quick sort

O(log n)

74
New cards

When might a bubble or insertion sort be better than quick or insertions sort

Easier to implement, quicker on nearly sorted lists

75
New cards

What is meant by logarithmic growth

As the size of the array

increases, the rate at which the

number of checks needed increases

more slowly

The inverse of exponential growth

The rate of increase is a logarithmic

function of the size of the array

76
New cards

When is quick and merge sort better than insertion and bubble sort

On large sets of data

77
New cards

Which is faster, insertion sort or bubble sort

Insertion sort is generally faster

78
New cards

Why do insertion sort and bubble sort have space complexity of O(1)

They are inplace (all sorting takes place within the data itself)

79
New cards

Why do insertion sort and bubble sort have time complexity of O(n^2)

They have a nested loop

80
New cards

Define a tree

A data structure that represents a hierarchical nature made up of nodes called roots(parents) and leaves(children) connected by branches

81
New cards

What is a binary tree

A tree that has at most two children for each parent node

82
New cards

How does a depth-first traversal work

Goes to the left child when it can.

When there are no left children it goes to the right child

When there are no children left it visits the node and back tracks to the parent node

Uses a stack

83
New cards

What is a depth first search used for

Job-scheduling, where some jobs have to be completed before others can begin

Finding a path between two vertices

Solving puzzles such as navigating a maze

84
New cards

How does a breadth-first traversal work

Visits all nodes connected directly to the current node

Then visits all nodes directly connected to each of those nodes(etc)

Uses a queue

85
New cards

What is a breadth first used for

Finding the shortest path between two points

A Web crawler uses a breadth-first search to analyse sites that you can reach by following links randomly

Facebook uses a breadth-first search to find all the friends of a given individual

86
New cards

How does a pre-order traversal work

Explores the root, then the left subtrees, then the right subtrees: olr

87
New cards

How does an in-order traversal work

Explores the left subtree, then the root, then the right subtree: lor

88
New cards

How does a post order traversal work

Explores the left subtree, then the right subtree, then the root: lro

89
New cards

Why are trees traversed

To get the information stored in them

90
New cards

What is an intractable problem

One that cannot be solved efficiently e.g. in polynomial time

91
New cards

What is an insoluble problem

One that has no solution at all other than heuristics