Search Algorithms
Search Algorithms
Search Algorithms
Bubble Sort
Goes through list comparing first and second values, then second and
third, and so on until it compare n and n-1. Each comparison it will see
if the larger number occupies the larger index, and if it doesnt the
values will be swapped. This process is then repeated except it only
swaps up to the n-2 and n-1 comparison, and so on.
O(n2).
Insertion Sort
Starts from the second item and compares it to the first, swapping if the first
is large. Then it compares the third item to the two before it, and so on.
Meaning after i iterations the first I numbers are sorted.
O(n2)
Quicksort
Begin by selecting the midpoint in the list, called the pivot point. Everything
less of the pivot is moved to the left of the pivot, and anything else is moved
to the right. This guarantees the pivot is in the correct place for the fully
sorted list. Repeat this process with the partitions left and right of the pivot,
and then repeat it with their partitions, and so on. The process terminates
each time it encounters a sublist with fewer than two items.
O(n log n) best case/average case
Mergesort
The middle position of list divides the list in two sublists, which are then each
divided into two more sublists, and so on in that manner until there are
individual elements left. Two individual elements will be merged into one
sublist of two elements which will then merge with another sublist of two
elements to form an ordered list of four elements and so on. Eventually the
two main partitions will be sorted and merge together to create a sorted list.
O(n log n)