Search and sort algorithms info Flashcards
how does a linear search work?
runs through each term in the array and compares it to the search item.
loops until the item is found or until the end of the list is reached.
what is the time complexity of a linear search algorithm?
- worst case - item is not in list O(n)
- average case - O(n)
- best case - item is the first item in the list O(1)
what is the space complexity of a linear search algorithm?
O(1)
what does time complexity mean?
how well does the algorithm scale?
how does the number of steps grow as the input size (n) grows
how does a binary search work?
the array is spit in half and the middle term is compared to the search item.
if the search item is less than the mid item, the right side of the array is discarded.
if the search item is greater than the mid term, the left side is discarded.
this is repeated until the item is found or there is only 1 item left.
what is the condition for a binary search?
the array must be sorted
what is the time complexity for a binary search algorithm?
- worst case - the item is not in the array O(logn)
- average case - O(logn)
- best case - item is at the midpoint O(1)
what is the space complexity of a binary search algorithm?
O(1)
how does a bubble sort work?
the first two terms are compared in size. if the second term is less than the first term, they are swapped.
this is done until the end of the array is reached.
this is one parse.
there are multiple parses to sort the array and a final parse to check if the array is sorted.
what is the time complexity of a bubble sort algorithm?
- worst case - if the array is in reverse order O(n²)
- average case - O(n²)
- best case - the array is already sorted O(1)
what is the space complexity of a bubble sort algorithm?
O(1)
how does an insertion sort work?
the second term is compared to the one to the left of it. if it is less, the item on the left is shifted towards the right. this is continued until the item is inserted into the correct position
what is the time complexity of an insertion sort algorithm?
worst case -
what is the space complexity of an insertion sort algorithm?
O(1)
how does a merge sort algorithm work?
- the array is split into two left and right sublists
- a merge sort is performed on the left and right sublists
- the sublists are split in half until they are one item
- the items are then merged together into sublists that are sorted
- this is repeated until the list is sorted