2.3.1 Algorithms Flashcards

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

Define a linear search

A

Finds an item in a list by checking each item in turn

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

What are the key points of a linear search?

A
  • Starts at the first item
  • Searches in sequence
  • Can be represented in an array or binary tree
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What are the advantages of a linear search?

A

Easy to implement

Works on ordered or unordered lists

Easy to add items

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

What are the disadvantages of a linear search?

A

Usually most inefficient on longer lists

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

Define a binary search

A

An algorithm for finding an item in a sorted list.

Starts at the middle and repeatedly divides the list containing the item in half

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

What are the key points of a binary search?

A
  • Starts at the middle item
  • Halves the items needed to search after each check
  • Can be represented by an array or linked list
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What are the advantages of a binary search?

A

Efficient

Suitable for large data sets

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

What are the disadvantages of a binary search?

A

Only works on ordered lists

Slow to add items, as they must be placed in the right place

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

Define a bubble sort

A

Orders a list of items by comparing and swapping pairs of items.

It ‘bubbles’ the smallest/largest number to the ends

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

How does a bubble sort work?

A
  • Starts at the first item
  • Compares with the adjacent item, swapping if necessary
  • Moves on until all pairs have been compared
  • If any were swapped then a new pass will start
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What are the advantages and disadvantages of a bubble sort?

A

Advantage: works well on small lists

Disadvantage: slow to work

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

Define an insertion sort

A

Slots each item into the correct position in a data set

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

How does an insertion sort work?

A
  • Compares the second item to the one before
  • Will continue along until the current item is less than the one before
  • Compares the current item with all before to find the correct location
  • Moves the items to their new positon
  • Repeats until everything is in position
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What are the advantages and disadvantages of an insertion sort?

A

Advantage: works well on small lists

Disadvantage: slow to work

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

Define a merge sort

A

Uses ‘divide and conquer’ to quickly sort data.

Creates multiple sub-problems, which are each solved individually before being re-joined.

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

How does a merge sort work?

A
  • Repeatedly divides the list in half until each item has its own list
  • Compares adjacent lists to order them into a new list
  • Removes old lists
  • Repeats with adjacent lists until one list formed
17
Q

Define a quick sort

A

Sorts a set of data at a high speed using ‘divide and conquer’.

It creates sub-programs which are solved individually then combined.

A pivot value will be chosen to compare the items to when dividing the list.

18
Q

How does a quick sort work?

A
  • Sets a pivot value
  • Compares each item to the pivot value, adding those less than to one list and greater than to another
  • A pivot is chosen for the new lists
  • Repeats until each item has its own list
  • Conjugates the lists from left to right
19
Q

Define the ‘Dijkstra’s Shortest Path algorithm’

A

Finds the shortest distance between the starting node and target node using a weighted tree

20
Q

Define the ‘A* algorithm’

A

Finds the shortest path between two vertices on a weighted tree using heuristics. Won’t consider every vertex

21
Q

What are the advantages and disadvantages of Dijkstra’s Shortest Path?

A

Advantage:

Finds the shortest time to a location

Disadvantage:

Every vertex must be investigated

22
Q

What are the advantages and disadvantages of the A* algorithm?

A

Advantages:

Optimal in regards to efficiency

Works well for complex problems

Disadvantages:

The speed of execution is dependant on the heuristic accuracy

Can have complexity problems

23
Q

How is the Dijkstra’s Shortest Path algorithm carried out?

A
  • Two lists are formed: visited and unvisited nodes
  • Node distances are infinity, other than the starting node, all fill the unvisited list
  1. For the current node: neighbouring unvisited nodes are examined by calculating the total distance to them from the start
  2. If the new distance is less than the known distance, the distance and previous node information is updated
  3. Add the current node to the visited list and move to the next shortest-distance-away node
  4. Repeat until all nodes have been visited
24
Q

How is the A* algorithm carried out?

A
  • Fundamentally the same as Dijkstra’s
  • Each node has a heuristic value, e.g. the distance or time to the final node
    • The nodes are ordered by heuristic + weight
    • Once the final node has been reached and expanded, unvisited nodes are ignored as the shortest path is found
25
Q

What are the 5 main Big O notations?

A

Constant: O(1)

Logarithmic: O(log n)

Linear: O(n)

Polynomial: O(n2)

Exponential: O(nn)

26
Q

Explain constant Big O notation and state its time and space efficiency

A
  • Takes the same time to run regardless of the size of inputted data
  • Time: good
  • Space: good
27
Q

Explain logarithmic Big O notation and state its time and space efficiency

A
  • As the data set size increases, the time execution’s rate of decrease will increase
  • Time: good
  • Space: good
28
Q

Explain linear Big O notation and state its time and space efficiency

A
  • Time will increase directly proportionally to the size of inputted data
  • Time: average
  • Space: average
29
Q

Explain polynomial Big O notation and state its time and space efficiency

A
  • Will match every item in the list with every other item: finds all possible combinations. Time execution’s rate of increase will increase with data set size
  • Time: average
  • Space: average
30
Q

Explain exponential Big O notation and state its time and space efficiency

A
  • The time to complete doubles with each new data item. Works quickly with small data sets, and much longer with larger ones
  • Time: bad
  • Space: average