Searching and sorting algorithms Flashcards
Methods for sorting algorithms
- Bubble sort
- Merge sort
- Insertion sort
Methods for searching algorithms
- Binary search
- linear search
How does binary search work
Splits the data set in half and compares if the number is greater or smaller than the number they are finding, gets rid of half the list and splits it again comparing if it is greater than or smaller than until it has found the correct number
Advantages and disadvantages of a binary search
- Much more efficient than a linear search
- But the list must be ordered
How does a linear search work
Starts at the beginning of the data set and compares each number to the one it is searching for
Advantages and disadvantages of a linear seach
- very simple
- Can be very inefficient if the number is at the end of a long list
- The data set does not have to be ordered
How does a bubble sort work
Starts at the beginning of the list and compares the number with the one next to it and makes a switch if it is larger.
it then moves to the second value and compares
It continues this until there is no more items to compare
After the first pass a second pass is completed to ensure it is all sorted
Advantages and disadvantages of a bubble sort
- It is popular and easy to implement
- Does not deal well with large lists
How does a merge sort work
- It uses a method called divide and conquer
- The list is repeatedly divided into two until all the items are separate
- Pairs of elements are then compared, this process is repeated until the list is recompiled
Advantages and disadvantages of a merge sort
- reliable efficiency
- Excellent choice for large datasets
- Not the most space efficient algorithm
How does an insertion sort work
- Compares values in turn
- Starts on the second value and compares if it is bigger than the first value
- If it is it moves onto the third value and compares if that is bigger than the second value
- If one value is greater it will then be compared with all the previous values
Advantages and disadvantages of an insertion sort
- Less complex and efficient as a merge sort
- More efficient than a bubble sort
- Works best with smaller data sets