Searching and Sorting Algorithms Flashcards
How does a binary search work?
1) Find the middle item in the ordered list
2) If this is the one your looking for then it stops.
3) If not, compare the item you’re looking for to the middle item. If it comes before the middle item, get rid of the second half or vice versa.
4) You’ll be left with a list that is half of the original list. Keep repeating the first 3 steps until you find the item your looking for.
What does a binary search do?
Look for items in an ordered list.
What does a linear search do?
A linear search checks each item of the list in turn to see if it’s the correct one. It stops when it’s found the item or has checked every item.
How does a linear search work?
1) Look at the first item in the unordered list.
2) If this is the item you’re looking for, then it stops as you found it.
3) If not, look at the next item in the list.
4) Repeat steps 2-3 until you find the item that you’re looking for or you’ve checked every item.
What are the differences between a binary and linear search?
Linear is much simpler, but not as efficient. It can be used on an unordered list as well.
Due to linear searches being insufficient what are they often used on?
Only small lists
Why are binary searches more efficient than a linear one?
Takes fewer steps (once it has been ordered) to find the item which makes it more suitable for larger lists.
What is a bubble sort?
Is used to sort an unordered list of items by comparing pairs of items. It is very simple to follow but cam often take a while to sort.
How does a bubble sort work?
1) Look at the first two items in the list.
2) If they’re in the right order leave them otherwise swap them.
3) Move on to the next pair (3rd and 4rd entries) and repeat step 2.
4) Repeat step 3 until you get to the end of the list - this is called 1 pass. The last item will be in the correct place, so don’t include it in the next pass.
5) Repeat steps 1-4 until there are no swaps in a pass.
What are the advantages of a bubble sort?
- it’s a simple algorithm that can be easily implemented on a computer.
- it’s an efficient way to check if a list is already in order (would only have to do 1 pass to know if the items are ordered)
- Doesn’t use very much memory as all the sorting is done using the original list.
What are the disadvantages of a bubble sort?
- inefficient way to sort a list
- due to being insufficient it doesn’t cope with very large lists.
What is a merge sort?
It splits a list apart and then puts it back together in the correct order.
The merge sort is a divide and conquer algorithm and takes advantage of what two facts?
- small lists are easier to sort than large lists
- easier to merge two ordered lists than two unordered ones.
How does the merge sort work?
1) split the list in half
2) Keep splitting the list until all the lists only contain 1 item.
3) Merge pairs of the lists so each list has twice has many items. Each time you merge them sort the items into the right order.
4) Repeat step 3 until all the lists are merged together into the original list in the correct order.
What are the smaller lists called when you split them using a merge sort?
sub-lists