Algorithms
Algorithms
Searching Algorithms
Linear search:
- No ordered list required.
- Linear search scans each item or element in a collection of items, e.g. playing cards,
in turn, starting from the beginning, until a match is found or the end of the collection
is reached.
- Linear search doesn’t care whether the list is ordered or not.
- Average time is length / 2.
Binary search:
- Ordered list required.
- Goes to the middle of the list, decides whether the item is greater than, less than the
item that's being looked for or equal to the item that’s being looked for. If it’s greater
than or less than then it will go to the corresponding half of the array to begin the
search process again. If the item matches then it’s position is returned. It then
repeats the process. Very efficient method for large data sets.
- Maximum computing time is determined by powers of 2 because it splits the array in
the middle each time hence a list of length 1 has 2^0, length 2 has 2^1, 128 has 2^7
etc.
- Faster than Linear Search.
Sorting Algorithms
Bubble Sort:
- Works by swapping the smaller items with larger items as the program goes through
the list. The program will repeat until it makes a run where it did not make a swap at
which point the program is sorted.
- Could take (length of array)^2 attempts to sort.
Merge Sort:
- The algorithm divides the list into equal parts until it can no longer be divided. It then
merges the sorted parts back together but this time sorting each subsection. The sort
here uses the same bubble sort technique.
- The time complexity is nlogn.
- Faster than bubble sort for large arrays.