2.3.1 Algorithms Flashcards
Define a linear search
Finds an item in a list by checking each item in turn
What are the key points of a linear search?
- Starts at the first item
- Searches in sequence
- Can be represented in an array or binary tree
What are the advantages of a linear search?
Easy to implement
Works on ordered or unordered lists
Easy to add items
What are the disadvantages of a linear search?
Usually most inefficient on longer lists
Define a binary search
An algorithm for finding an item in a sorted list.
Starts at the middle and repeatedly divides the list containing the item in half
What are the key points of a binary search?
- Starts at the middle item
- Halves the items needed to search after each check
- Can be represented by an array or linked list
What are the advantages of a binary search?
Efficient
Suitable for large data sets
What are the disadvantages of a binary search?
Only works on ordered lists
Slow to add items, as they must be placed in the right place
Define a bubble sort
Orders a list of items by comparing and swapping pairs of items.
It ‘bubbles’ the smallest/largest number to the ends
How does a bubble sort work?
- 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
What are the advantages and disadvantages of a bubble sort?
Advantage: works well on small lists
Disadvantage: slow to work
Define an insertion sort
Slots each item into the correct position in a data set
How does an insertion sort work?
- 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
What are the advantages and disadvantages of an insertion sort?
Advantage: works well on small lists
Disadvantage: slow to work
Define a merge sort
Uses ‘divide and conquer’ to quickly sort data.
Creates multiple sub-problems, which are each solved individually before being re-joined.