Algorithms Flashcards
(5 cards)
1
Q
Explain the steps of binary search
A
- Binary search is more efficient than linear search
- In binary search, the list must be ordered
- First, the middle term of the list is compared with the target term. If the term matches the target then the index is returned
- If not, the top half of the list is ignored if the target term is less than the middle term
- If the target term is greater than the middle term, the bottom half of the list is ignored
- This is repeated until the target term is found
2
Q
Explain the steps of linear search
A
- Each item in the list is compared with the search item. If it does not match, the next term is compared
3
Q
Explain the stages of bubble sort
A
- Starting at the beginning, two adjacent terms are compared with eachother and then sorted in order
- The last term of the first comparison is then compared with the next adjacent term, ordering them aswell
- Once the end of the list has been reached, the last term will be in the correct place. The algorithm then resets for another pass
4
Q
Explain the steps of insertion sort
A
- Starts with one of the items from the list
- The next item is compared with the first item. If it is greater than the first item is shifted to the left and the next item takes its place. if the next item is less than the first item than the first item is shifted to the right and the next item takes its place
- This is repeated until the list is ordered
5
Q
Explain the steps of merge sort
A
- The list is broken up into individual terms
- Adjacent terms (in twos) are put back together in order, creating many mini lists
- Adjacent mini lists are then compared and put back together in order
- This is repeated until the whole list is back together in order