Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Search Algorithms

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 2

Search Algorithms

Sequential (linear) search Compares an item in a list to a target and moves


on to the next item, sequentially.

Depending on the arrangement of data in a list, an algorithm can take much


longer or shorter to run.
Best Case The target value is the first item stored in a list during a
sequential search, making it O(1) complexity. (NOTE: It is often best to worry
more about average and worst-case performances rather than best.)
Worst Case Target is not in the list or is the last item, meaning n iterations
are made and it is O(n).
Average Case Add the number of iterations to find the target at each
possible position and divide the sum by n. This ends up being O(n) in a
sequential search, and the worst and average case are often the same.

Best, Worst, and Average Case Performance

Binary Search Algorithm


A binary search algorithm can be used only on a sorted list.
Upper and lower bounds are established (first index and last of the list) and
then the midpoint is checked for the target value.
Depending on whether the midpoint is lower or higher than the target, the
upper and lower bounds are redefined and a new midpoint is checked until
the target is found or determined to not be in the list.

Selection Sort Algorithm


Goes through the list searching for the smallest number, compares it to the
number in list[0], and if list[0] is bigger their places are swapped. This is
repeated again except with the position list[1] up until list[n-1].
O(n2) in all cases.

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

O(n2) worst 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)

You might also like