Sort and Search Algorithms Flashcards
A search algorithm is
a set of instructions for finding a specific item of data within a data set.
An effective search is
one which will always either find the solution or determine that the target data is not present
An efficient search will
find the solution quickly regardless of its location within the data set
two common search algorithms are:
Linear search and Binary search
Linear search
checks each term in a dataset one by one until output is found
pros and cons of linear search
pro: Very easy to implement
con: Slow on a long list
Binary Search
Looks at the middle value of a dataset, if that value is greater than the target, repeat on the first half of the data set, if it is less than the target, repeat on the second half of the data set. Stop repeating when target is found or the size of our dataset is zero
pros and cons of binary search
pro: faster than linear search on a large dataset
cons: Dataset must be sorted before starting
which search algorithm only works on sorted lists and which works on unsorted lists
sorted: linear search
unsorted: binary search
examples of sort algorithms:
bubble sort, insertion sort and merge sort
A sort algorithm is
a set of instructions to arrange a dataset into a particular order.
An efficient sort algorithm is
one which can sort a dataset in a short time.
bubble sort
compares the first two items in a dataset and swaps them if they are in the wrong order. This process continues for the rest of the dataset. Repeat the whole process until a pass with no swaps happens.
What is a pass?
A pass is when every item in a dataset has been tested and the end of the dataset has been reached
pros and cons of bubble sort
pros: easy to implement, does not use much memory
cons: poor for efficiency
insertion sort
in a dataset, compare second item with first, swap if necessary. take the third item and compare with second and first item. compare items with the previous items: 2 with 1; 3 with 2 and 1; 4 with 3,2 and 1; 5 with 4,3,2 and 1 etc. repeat until sorted list
pros and cons of insertion sort
pros: easy to implement, little memory used
cons: not very efficient
Merge sort is an example of
a divide and conquer algorithm
merge sort
split lists into lists of size one
merge each pair of sublists
continue merging until there is only one list
is merge sort or insertion sort more efficient?
merge sort is more efficient than insertion sort